书上说,射线法和叉乘法感觉都不完整
下面我分享我写的基于AutoCAD BREP算法
var ed = acApp.Application.DocumentManager.MdiActiveDocument.Editor;var peo = new PromptEntityOptions("Select a PolyLine : ");peo.SetRejectMessage("Only PolyLine");peo.AddAllowedClass(typeof(Polyline), true);var per = ed.GetEntity(peo);if (per.Status != PromptStatus.OK) return;var ppo = new PromptPointOptions("指定测试点");var ppr = ed.GetPoint(ppo);if (ppr.Status != PromptStatus.OK) return;var rtn = "无法检测";using (var tr = per.ObjectId.Database.TransactionManager.StartTransaction()){var poly = tr.GetObject(per.ObjectId, OpenMode.ForRead) as Polyline;//一定要读,不要写//看点是否在曲线上/var ptOnPolyline = poly.GetClosestPointTo(ppr.Value, false);if (ptOnPolyline.DistanceTo(ppr.Value) < 0.001){acApp.Application.ShowAlertDialog(PointContainment.OnBoundary.ToString());return;}var pts = Enumerable .Range(0, poly.NumberOfVertices) .Select(i => poly.GetPoint3dAt(i));if (!poly.Closed && ! pts.FirstOrDefault().IsEqualTo(pts.LastOrDefault(),new Tolerance(0.0001,0.0001))){acApp.Application.ShowAlertDialog("多段线不封闭无法检测");return;}//转为面域var reg = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(new DBObjectCollection { poly }).Cast<Autodesk.AutoCAD.DatabaseServices.Region>().FirstOrDefault();using (var brep = new Brep(reg)){if (brep != null){using (var ent = brep.GetPointContainment(ppr.Value, out PointContainment resultt)){rtn = ent is Autodesk.AutoCAD.BoundaryRepresentation.Face ? PointContainment.Inside.ToString() : resultt.ToString();}}}}acApp.Application.ShowAlertDialog(rtn);