3

Given a top-left long/lat and a bottom-right long/lat how can i find out if a given long/lat falls within the rectangle ?

Ideally i would be looking at something like

bool IsWithinArea(float topLeftLat,float topLeftLong,
   float bottomRightLat,float bottomRightLong,float testLat,float testLong)

Update

The one problem is that the rectangle created from the long/lat may be from a rotated map, so bottom right will not always be greater than top left...

Richard Friend
  • 15,800
  • 1
  • 42
  • 60
  • Homework? This is pretty trivial... – Jon Cage Mar 31 '11 at 11:55
  • 1
    No it is not homework, my mistake, i thought lonitude and latitude was some kind of voodoo, i didnt think it was this obvious. – Richard Friend Mar 31 '11 at 12:34
  • Since it Lat and Lon, I assume that you want to find out if certain (lat,lon) within a certain area in the surface of the earth. If so then any of the answers given below will not work. – Moshi Feb 28 '19 at 06:55

4 Answers4

5

We can make it more interesting than trivial checks:

return new Rect(topLeftLat, topLeftLong, bottomRightLat - topLeftLat, bottomRightLong - topLeftLong)
      .Contains(testLat, testLong);

P.S.: Rect.Contains(...) method

Snowbear
  • 16,924
  • 3
  • 43
  • 67
  • 1
    nict solution! But why do you substract the topLeft Corner from the bottom right? I would construct the rect using the top left and bottom right corner and not with the top left and the lengths of the edges... – hage Mar 31 '11 at 12:07
  • @stefanm It's a built-in .Net class, I've used constructor which was there, not my choice. – Snowbear Mar 31 '11 at 12:10
  • 1
    This would allocate a new Rect object every time you call your function. Watch out for memory or performance issues. – lzm Mar 31 '11 at 13:05
  • There is a static RectangleF.FromLTRB(,,,,) to avoid to calculate the width and height. – g1ga Jul 09 '14 at 16:06
2

Not sure if I'm thinking to simple

bool IsWithinArea(float topLeftLat, float topLeftLong, float bottomRightLat, float bottomRightLong, float testLat, float testLong)
{
    return (testLat >= topLeftLat && testLat <= bottomRightLat && testLong >= topLeftLong && testLong <= bottomRightLong);
}
Ralf de Kleine
  • 11,464
  • 5
  • 45
  • 87
  • That would only work if the rectangle is aligned with the x and y axes. It'd fail if the rectangle would be placed in a certain angle. – Jules Nov 06 '13 at 19:30
1

Assuming that Lat is the x coordinate und Long is the y coordinate and also assuming that the coordinate systems has its origin at the left top:

public bool IsWithinArea(float topLeftLat,float topLeftLong,
       float bottomRightLat,float bottomRightLong,float testLat,float testLong) {

          return (testLat >= topLeftLat && testLat <= bottomRightLat && testLong >= topLeftLong && testLong <= bottomRightLong);

    }
1

One approach would be to normalize your long/lat pairs to simple x/y coordinates. Then it should be a simple excersize to determine if a point falls within the rectangle.

long/lat to x/y conversion can be found here: Convert Lat/Longs to X/Y Co-ordinates

Community
  • 1
  • 1
erlando
  • 6,736
  • 4
  • 24
  • 29