[英]C# Trouble with detection is point in polygon?
I have polygon with these points: 我有这些点的多边形:
A= (-8598.07,7513.37)
A =(-8598.07,7513.37)
B= (-8598.07,5169.17)
B =(-8598.07,5169.17)
C= (-8496.47,5271.77)
C =(-8496.47,5271.77)
D= (-4735.47,5271.77)
D =(-4735.47,5271.77)
E= (-4736.47,7411.77)
E =(-4736.47,7411.77)
F= (-8497.47,7411.77)
F =(-8497.47,7411.77)
G= (-4838.07,7310.17)
G =(-4838.07,7310.17)
H= (-4838.07,5372.37)
H =(-4838.07,5372.37)
So my polygon has 8 segments. 所以我的多边形有8个线段
And I have point 'I' inside the polygon 我在多边形内有点“我”
(-6616,6802.6537)
(-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. 但是它返回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: 已知的有效C代码为:
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; }
Order them in that way: 以这种方式订购它们:
(-8598.07, 7513.37, -8598.07, 5169.17)
(-8598.07,7513.37,-8598.07,5169.17)
(-8598.07, 5169.17,-8496.47, 5271.77)
(-8598.07,5169.17,-8496.47,5271.77)
(-8496.47, 5271.77, -4735.47, 5271.77)
(-8496.47,5271.77,-4735.47,5271.77)
(-4735.47, 5271.77, -4838.07, 5372.37)
(-4735.47,5271.77,-4838.07,5372.37)
(-4838.07, 5372.37, -4838.07, 7310.17)
(-4838.07,5372.37,-4838.07,7310.17)
(-4838.07, 7310.17, -4736.47, 7411.77)
(-4838.07,7310.17,-4736.47,7411.77)
(-4736.47, 7411.77, -8497.47, 7411.77)
(-4736.47,7411.77,-8497.47,7411.77)
(-8497.47, 7411.77,-8598.07, 7513.37)
(-8497.47,7411.77,-8598.07,7513.37)
You just need to "close" the polygon. 您只需要“关闭”多边形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.