I have two pairs of lat/long coordinates that represent the top left and bottom right corners of a bounding box.

I have a third pair of lat/long coordinates representing a point within this bounding box.

How might I go about converting this point to a percentage of the X/Y position within the bounding box? For example, a point at X=50% and Y=50% would be bang in the centre of the bounding box.

It does of course need to take into account positive and negative coordinates.

Any guidance appreciated!


To solve this, you need to determine the ratio of the point's location (relative to the starting origin) to the length of the rectangle. The length is just the absolute difference of the x and y coordinates. In order to determine the point's location relative to the origin, just subtract the lower left coordinates from the point's coordinates. Finally, just divide the two to compute the percentage.

Here is a python function that should solve what you are looking for, where coord1, coord2, coord3 are tuples of the coordinates:

def center(coord1, coord2, coord3):
    x1,y1 = coord1
    x2,y2 = coord2
    x3,y3 = coord3    

    dx = float(abs(x1 - x2))
    dy = float(abs(y1 - y2))

    px = (x3 - x1)/dx
    py = (y3 - y1)/dy

    return px,py

Or, if you prefer

def center2(coord1, coord2, coord3):
    px = (coord3[0] - coord1[0])/float(abs(coord1[0] - coord2[0]))
    py = (coord3[1] - coord1[1])/float(abs(coord1[1] - coord2[1]))
    return px,py

The results:

>>>center((-15,10), (10,18), (7.36, 12.9))
(0.8944, 0.36250000000000004)
  • Thank you, Paul. I had to make two changes to your answer. The X and Y positions (px and py) need to be cast to absolute values, and the results need to be multiplied by 100 to get the percentage. – John Blackbourn Sep 5 '13 at 19:14
  • 1
    @johnbillion By definition, the absolute difference between the center coordinates and the lower left coordinates will always be positive (or 0), so you shouldn't need a second call to abs(). – Paul Sep 5 '13 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.