c++ 怎么判断1个点在一个不规则的四边形内

来源:百度知道 编辑:UC知道 时间:2024/05/21 13:42:36
已知一个不规则的四边形的四个点(x1,y1),(x2,y2),(x3,y3),(x4,y4).和任意一点(x,y).怎么判断(x,y)在这个四边形内?

C++
同事在VS 2005里面写的

在多边形内包含线上 返回true
不在 返回false

bool InPolygon(List<PointF>^ pointarray,PointF point)//pointarray 点的集合(可以是任意多边形) point 要判断的点
{
int n = pointarray->Count;//确定点的个数
int count = 0;
LineSegment line;
line.pt1=point;
line.pt2.Y=point.Y;
line.pt2.X=-INFINITY;
for(int i=0;i<n;i++)
{
LineSegment side;
side.pt1=pointarray[i];
side.pt2=pointarray[(i+1)%n];
if(Isonline(point,side))
{
return 1;
}
if(fabs(side.pt1.Y-side.pt2.Y)<ESP)
{
continue;
}
if(Isonline(side.pt1,line))
{
if(side.pt1.Y>side.pt2.Y) count++;
}
else if(Isonline(side.pt2,line))
{
if(side.pt2.Y>side.pt1.Y) count++;
}
else if(Intersect(line,side))
{
count++;
}
}
if(count%2==1){return 2;}
else {return 0;}

}

struct