Question

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#.

Was it helpful?

Solution 3

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

OTHER TIPS

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;
}

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top