Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4339376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:12:29+00:00 2026-05-21T11:12:29+00:00

I need some math advice… Or, at least I think i do. I have

  • 0

I need some math advice… Or, at least I think i do.

I have a set of elements in my database which represents objects (like phyisical objects) with a position (x,y,z). These positions can be totally random, like object A has a position of 1532,3512,1500 and B has a position of 2421,4612,1234.

I’m trying to create a graphical map (using CSS) of the objects in 2D. So only the X and Y coords are being used. The map I want to draw on has a specific width and height. Now here’s the tricky part: I want the map to scale with the size of the map. Like, when there are 2 objects where one is at coord: 1,1 and the other on coord 3,3, I want object one to be in the top left corner of the map and the other in the bottom right.

So, here’s an example of my elements (objects):

database elements:
  - [1] => x: 1254, y: 1642
  - [2] => x: 2311, y: 2361
  - [3] => x: 1732, y: 2351
  - [4] => x: 1436, y: 3323

First, I take the minimum and maximum values of these elements to create a formula which lets the coords start at 0,0 and go from there.

so, let’s say the minumum x-value is 1254 in this example and Y is 1642.
I go in a for loop and do the following:

foreach( $this->db as $item )
{
    $x = $this->value_x_max - $item['x'];
    $y = $this->value_y_max - $item['y'];
}

The map size is 720×480 pixels. How do I create a formula which spreads out my objects across the map. It is important that the objects don’t go outside the map boundaries but also needs to be spread out. So if 2 objects have positions 1,1 and 1,2 for example, they need to be in the top left and bottom left corners.

Can anyone help me out with this?

Any help and advise appriciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T11:12:30+00:00Added an answer on May 21, 2026 at 11:12 am

    Try this, but be aware, it’s just a realy quick and dirty raw sketch of what came to my mind. But maybe it’s enough to give you a jump start:

    <?php
    $elements = array(
        array('x' => 1254, 'y' => 1642),
        array('x' => 2311, 'y' => 2361),
        array('x' => 1732, 'y' => 2351),
        array('x' => 1436, 'y' => 3323),
    );
    
    $mapWidth = 720;
    $mapHeight = 480;
    $blockWidth = 10;
    $blockHeight = 10;
    
    $minimumX = $minimumY = $maximumX = $maximumY = null;
    foreach ($elements as $element) {
        if ($minimumX === null || $element['x'] < $minimumX) {
            $minimumX = $element['x'];
        }
        if ($minimumY === null || $element['y'] < $minimumY) {
            $minimumY = $element['y'];
        }
        if ($maximumX === null || $element['x'] > $maximumX) {
            $maximumX = $element['x'];
        }
        if ($maximumY === null || $element['y'] > $maximumY) {
            $maximumY = $element['y'];
        }
    }
    
    foreach ($elements as $i => $element) {
        $elements[$i]['x'] -= $minimumX;
        $elements[$i]['y'] -= $minimumY;
    }
    
    $fx = ($mapWidth - $blockWidth) / ($maximumX - $minimumX);
    $fy = ($mapHeight - $blockHeight) / ($maximumY - $minimumY);
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>2D</title>
            <style type="text/css">
                body {
                    background: black;
                }
    
                #map {
                    position: relative;
    
                    margin: 20px auto;
    
                    width: <?php echo $mapWidth; ?>px;
                    height: <?php echo $mapHeight; ?>px;
    
                    background: grey;
                }
    
                .dot {
                    position: absolute;
    
                    width: <?php echo $blockWidth; ?>px;
                    height: <?php echo $blockHeight; ?>px;
    
                    background: lightblue;
                }
            </style>
        </head>
    
        <body>
            <div id="map">
                <?php
                    foreach ($elements as $element) {
                        printf(
                            '<div class="dot" style="left: %.1fpx; top: %.1fpx;"></div>',
                            $element['x'] * $fx,
                            $element['y'] * $fy
                        );
                    }
                ?>
            </div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a input tag which is non editable, but some times i need
I need to do some large integer math. Are there any classes or structs
I need some advice as to how I easily can separate test runs for
I need some help from the shell-script gurus out there. I have a .txt
I have a Excel Spreadsheet with lab data which looks like this: µg/L (ppb)
I'm in the situation where i need to do some math related stuff in
I need some input. Say you have an int-based Enum with values 1 through
I have to work with some (semi-)automatical verification software (CBMC (link) ) which is
I need some info on how to use margins and how exactly padding works.
I need some software to explore and modify some SQLite databases. Does anything similar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.