判断点是否在多边形内(C#版本)
////// 判断点是否在闭合多线段内
///
/// 待判断点
/// 目标多边形
/// 在多边形内则返回1
private double min(double x,double y)
{
if (x > y)
return y;
else
return x;
}
private double max(double x, double y)
{
if (x > y)
return x;
else
return y;
}
public int PtinPolygon(Point3d pt,Point3d[] ptPolygon)
{
int nCount = ptPolygon.Length;
// 记录是否在多边形的边上
bool isBeside = false;
// 多边形外接矩形
double maxx,maxy,minx,miny;
if (nCount > 0)
{
maxx = ptPolygon.X;
minx = ptPolygon.X;
maxy = ptPolygon.Y;
miny = ptPolygon.Y;
for (int j = 1; j = maxx)
maxx = ptPolygon.X;
else if (ptPolygon.X = maxy)
maxy = ptPolygon.Y;
else if (ptPolygon.Ymaxx) || (pt.Xmaxy) || (pt.Y = min(p1.X,p2.X) && pt.Xmax(p1.Y, p2.Y))
continue;
// 求交点的X坐标
double x = (double)(pt.Y - p1.Y) * (double)(p2.X - p1.X) / (double)(p2.Y - p1.Y) + p1.X;
if (x > pt.X)
nCross++;// 只统计单边交点
else if (x == pt.X)
isBeside = true;
}
if (isBeside)
return 0;//多边形上
else if (nCross % 2 == 1)
return 1;// 多边形内
return -1; // 多边形外
}
该贴已经同步到 ,你直接用系统api吧。
能具体说一下吗? 用lz的方法 在有些时候判别出现了问题
你看下这样代码应该好点
public int ptInPolygon1(Point3d pt, Polyline pPolyline)
{
int count = pPolyline.NumberOfVertices;
//构建多边形外接矩形
double maxx = double.MinValue, maxy = double.MinValue, minx = double.MaxValue, miny = double.MaxValue;
for (int i = 0; imaxx)
{
maxx = pPolyline.GetPoint3dAt(i).X;
}
if (pPolyline.GetPoint3dAt(i).Y > maxy)
{
maxy = pPolyline.GetPoint3dAt(i).Y;
}
if (pPolyline.GetPoint3dAt(i).Xmaxx || pt.Y > maxy || pt.X = 1)
{
for (int n = 0; npt.X)
{
crossCount++;
Circle pCircle = new Circle(crossPt, Vector3d.ZAxis, 2);
pBlockTableRecord.AppendEntity(pCircle);
tran.AddNewlyCreatedDBObject(pCircle, true);
}
}
}
Circle circle = new Circle(pt, Vector3d.ZAxis, 2);
pBlockTableRecord.AppendEntity(circle);
tran.AddNewlyCreatedDBObject(circle, true);
pBlockTableRecord.AppendEntity(line1);
tran.AddNewlyCreatedDBObject(line1, true);
tran.Commit();
}
return crossCount % 2;
} 东西不错,顶了,感谢分享
页:
[1]