mathnet/mathnet-spatial

Order of points in polygon changes EnclosesPoint return value

lucasfolino opened this issue · 7 comments

When using the EnclosesPoint function the order of the points changes if the result is true or false. This is LinqPad Code:

var points = new List<Point2D>();
points.Add(new Point2D(12,14));
points.Add(new Point2D(97,14));
points.Add( new Point2D(12,23));
points.Add( new Point2D(97,23));
var polygon = new Polygon2D(points);

var p1 = new Point2D(54.5,18.5);

polygon.EnclosesPoint(p1).Dump();

result is false.

var points = new List<Point2D>();
points.Add(new Point2D(12,14));
points.Add( new Point2D(12,23));
points.Add(new Point2D(97,14));
points.Add( new Point2D(97,23));

var polygon = new Polygon2D(points);

var p1 = new Point2D(54.5,18.5);

polygon.EnclosesPoint(p1).Dump();

result is true

@lucasfolino Do you expect different result?

Maybe I misunderstand how the calculation is made. Why would the order of the points change if something is in the bounds or not? If I give you four points of a square should it matter which order those points are in? I would think the square is going to look the same and shouldn't change if something is within the bounds.

Have you tried to plot polygon from your points? It won't a square in both cases.

I understand it is not a square, I was using the square as an example. This would be a rectangle. Regardless of the shape of the points the points should enclose the point in. I was wondering why the order of the points when added to the list would change the result. What would be the best way to add the points to the list to make the result the most consistent? Would ordering by X lowest to largest be the best way to fix this.

Because this function works with polygon, not with unordered cloud of points. Draw a line through your points and you will see, that it is even not a rectangle!

So the point order in creation is the connection order of the lines? This would make sense and explain why I see what I see. I thought the points used for the polygon where the boundaries of the polygon not the draw order.

I understand it is not a square, I was using the square as an example. This would be a rectangle. Regardless of the shape of the points the points should enclose the point in.

it doesn't , as already mentioned above. Polygons from your example looks like:

Example

What would be the best way to add the points to the list to make the result the most consistent?

It's not clear what you are asking. May you give more information?

And just a note - there is a gitter chat