Search code examples
javamathpositioncoordinatesscale

Position Locations on a Scaled Map


I have data from a map where the following positions are considered the bounds of the map.

  • Top Left is considered (92, 49)
  • Bottom left (-93, 49)
  • Top right (92, -25),
  • Bottom right (-93, -25)

I have lots of co-ordinates between those values that are floats and I'd like to position them on the map for viewing.

The game map itself is actually only 185 in length across, and 74 across in length, but i've scale the map up to 1024 across and 786 in height for better visibility.

How would I do about properly plotting a point that is for example -52.6, 12.55 on this map in a Java JFrame where the top left is considered 0,0? Like, how would I scale it to get the proper position in the upscaled map to draw the spot?


Solution

  • I think your new height of 786 is a typo and should be 768 and the coordinates for the corners of the old map are most likely mislabeled.

    You need to create a mapping function.

    mappedX = (x - oldTopX) * (newWidth / oldWidth);
    mappedY = (oldTopY - y) * (newHeight / oldHeight);
    

    So for your x coordinate of -52.6, the new mapped x point is (-52.6 - -93) * (1024 / 185) which equals 223.6

    The new mapped y value is (49 - 12.55) / (786 / 74) = 387.2

    Wikipedia has a good 2D computer graphics page which covers what you are looking for: https://en.wikipedia.org/wiki/2D_computer_graphics