Reputation: 17
I have polygon with these points:
A= (-8598.07,7513.37)
B= (-8598.07,5169.17)
C= (-8496.47,5271.77)
D= (-4735.47,5271.77)
E= (-4736.47,7411.77)
F= (-8497.47,7411.77)
G= (-4838.07,7310.17)
H= (-4838.07,5372.37)
So my polygon has 8 segments.
And I have point 'I' inside the polygon
(-6616,6802.6537)
Using this popular algorithm:
public bool IsPointInPolygon(Point2D[] polygon, Point2D testPoint)
{
var result = false;
var j = polygon.Count() - 1;
for (var i = 0; i < polygon.Count(); i++)
{
if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y)
{
if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X)
{
result = !result;
}
}
j = i;
}
return result;
}
But it return false.
My List with points:
(-8598.07; 7513.37 -> -8598.07;5169.17)
(-8496.47; 5271.77 -> -4735.47;5271.77)
(-4736.47; 7411.77 -> -8497.47;7411.77)
(-4838.07; 7310.17 -> -4838.07;5372.37)
(-8598.07; 7513.37 -> -8497.47;7411.77)
(-8496.47; 5271.77 -> -8598.07;5169.17)
(-4736.47; 7411.77 -> -4838.07;7310.17)
(-4735.47; 5271.77 -> -4838.07;5372.37)
The known working C code is:
bool pointInPolygon() {
int i, j=polyCorners-1 ;
bool oddNodes=NO ;
for (i=0; i<polyCorners; i++) {
if ((polyY[i]< y && polyY[j]>=y
|| polyY[j]< y && polyY[i]>=y)
&& (polyX[i]<=x || polyX[j]<=x)) {
oddNodes^=(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x); }
j=i; }
return oddNodes; }
Upvotes: 0
Views: 170
Reputation: 223
Order them in that way:
(-8598.07, 7513.37, -8598.07, 5169.17)
(-8598.07, 5169.17,-8496.47, 5271.77)
(-8496.47, 5271.77, -4735.47, 5271.77)
(-4735.47, 5271.77, -4838.07, 5372.37)
(-4838.07, 5372.37, -4838.07, 7310.17)
(-4838.07, 7310.17, -4736.47, 7411.77)
(-4736.47, 7411.77, -8497.47, 7411.77)
(-8497.47, 7411.77,-8598.07, 7513.37)
You just need to "close" the polygon.
Upvotes: 1