3

I have defined a rectangle on a map with 4 nodes.

Each one of the nodes is a pair (X,Y)

X: latitude

Y: longitude

X,Y:geographic coordinates (with double values)

I would like to check if a point (X,Y) is inside that rectangle.

That point is going to be the current position of the user (The current position comes from the GPS output of the mobile)

Is there a specific mathematic formula for that? How can I find if a specific point belongs to a rectangle?

Cause I would like to implement it in C#.

programmer
  • 4,571
  • 13
  • 49
  • 59

3 Answers3

7

According to this

http://msdn.microsoft.com/en-us/library/system.device.location.geocoordinate(v=vs.110).aspx

The longitude can range from -180.0 to 180.0; so you have to be accurate with -180/180 since -180 logitude is equal to 180 one; just imagine a rectangle

  (15, 178, 25, -178)

the point (20, 179) should be within the rectangle and (20, 177) should be not; that's why RectangleF.Contains() could be incorrect in some cases;

// Just to show the idea with 180 latitude;
// First 4 parameters could be crammed into RectangleF
// And last 2 parameters into PointF
public static Boolean WithinRectangle(Double latitudeNorth,
                                      Double longitudeWest,  
                                      Double latitudeSouth,
                                      Double longitudeEast,
                                      Double latitude,
                                      Double longitude) {
  if (latitude > latitudeNorth)
    return false;
  else if (latitude < latitudeSouth)
    return false;

  if (longitudeEast >= longitudeWest)
    return ((longitude >= longitudeWest) && (longitude <= longitudeEast))
  else
    return (longitude >= longitudeWest) || (longitude <= longitudeEast);

  return false;
}
StanleyH
  • 2,734
  • 1
  • 14
  • 8
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
4

You can create Rectangle structure for those 4 points and then use Rectangle.Contains Method (Point) to check if the point exist in the Rectangle.

For floating point use RectangleF and its RectangleF.Contains Method (PointF)

I would also suggest you to look at SharpMap which is an open source project for GIS applications. This has classes like Point, Line, Polygon, BoundingBox etc, and all of them have methods like Intersect, GetBoundingBox, ToWKT, etc. They are really useful for spatial projects.

live2
  • 3,771
  • 2
  • 37
  • 46
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Does this function work with geographic coordinates?Cause, I have double values. – programmer Apr 15 '14 at 13:29
  • Its basically the same but I would recommend `RectangleF`. Depending on the coordinate system in use the floating point part of the number could be critical. – drew_w Apr 15 '14 at 13:30
  • I'm using Unity3d, so I guess I'll have to draw a rectangle on the map and use the Contains method afterwards – programmer Apr 15 '14 at 13:31
  • 1
    @programmer, for that use [`RectangaleF`](http://msdn.microsoft.com/en-us/library/system.drawing.rectanglef(v=vs.110).aspx) – Habib Apr 15 '14 at 13:31
  • 1
    @programmer, you don't have to draw the rectangle, you can just create it in memory and then check for contains. – Habib Apr 15 '14 at 13:33
  • Well I used the Rect method of UnityEngine namespace instead of the .net RectangleF method. I noticed that when the sides are not parallel to the axis X and Y, the Contains method doesn't return the right output (true or false). It returns true even if the point is not inside the rectangle. So what shall I do? Shall I use a method for polygon with 4 nodes? – programmer Apr 16 '14 at 10:07
  • What if the rectangle is across the prime meridian? Wouldn't creating a simple rectangle using those points make it wrap the other way around the globe? – adam0101 Jun 03 '16 at 14:08
1

Since the rectangle's sides were not parallel to X and Y axis, I thought that it was better to look for the PIP (Point in Polygon) problem, something more general that a rectangle.

So, I tested the code in the following post and it seemed to work for polygon with geo WGS84 coordinates.

https://stackoverflow.com/a/7739297/805660

thanks for your previous answers

Community
  • 1
  • 1
programmer
  • 4,571
  • 13
  • 49
  • 59