点是否在多边形内

来源:互联网 发布:java office转换pdf 编辑:程序博客网 时间:2024/04/29 06:49
bool CBSArxCalculate::IsPtInPolygon(const AcGePoint3d& ptTest, const AcGePoint3dArray& ptsBorder)
{
int nCross = 0;
int nCount = ptsBorder.length();
for (int i = 0; i < nCount; i++)
{
AcGePoint3d pt1 = ptsBorder[i];
AcGePoint3d pt2 = ptsBorder[(i + 1) % nCount];


if (pt1.y == pt2.y)//平行于X轴
{
continue;
}


if (ptTest.y < min(pt1.y, pt2.y) || ptTest.y > max(pt1.y, pt2.y) )//没有交点
{
continue;



/*解方程


y1 = a * x1 + b;


y2 = a * x2 + b;


得出直线方程为:y = a * x + b


联合y = y0可得交点x坐标


*/
double x = (ptTest.y - pt1.y) * (pt2.x - pt1.x) / (pt2.y - pt1.y) + pt1.x;
if (x > ptTest.x)


{
nCross++;
}
}
return (nCross % 2 == 1);
}
0 0
原创粉丝点击