- 积分
- 222
- 明经币
- 个
- 注册时间
- 2012-7-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2012-7-25 10:31:31
|
显示全部楼层
用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; i < count; i++)
- {
- if (pPolyline.GetPoint3dAt(i).X > maxx)
- {
- maxx = pPolyline.GetPoint3dAt(i).X;
- }
- if (pPolyline.GetPoint3dAt(i).Y > maxy)
- {
- maxy = pPolyline.GetPoint3dAt(i).Y;
- }
- if (pPolyline.GetPoint3dAt(i).X < minx)
- {
- minx = pPolyline.GetPoint3dAt(i).X;
- }
- if (pPolyline.GetPoint3dAt(i).Y < miny)
- {
- miny = pPolyline.GetPoint3dAt(i).Y;
- }
- }
- if (pt.X > maxx || pt.Y > maxy || pt.X < minx || pt.Y < miny)
- {
- return -1;
- }
- Line line1 = new Line(new Point3d(maxx, pt.Y, 0), new Point3d(minx, pt.Y, 0));
- int crossCount = 0;
- using (Transaction tran = pDatabase.TransactionManager.StartTransaction())
- {
- BlockTableRecord pBlockTableRecord = tran.GetObject(pDatabase.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
- Point3dCollection crossPoint = new Point3dCollection();
- line1.IntersectWith(pPolyline, Intersect.OnBothOperands, crossPoint, 0, 0);
- if (crossPoint.Count >= 1)
- {
- for (int n = 0; n < crossPoint.Count; n++)
- {
- Point3d crossPt = crossPoint[n];
- if (crossPt.X > pt.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;
- }
|
|