测试结果:
主要思路: 假设有两条曲线分别是 c1 和 c2, 把 c1 按照 1 的距离划分我这里用变量 jd 表示, 得到一个曲线集合 coll, 然后遍历 coll, 得到 coll 中每一个曲线的两个端点, 再用这两个端点分别求离曲线 c2 的最短距离, 直接使用开发库的 GetClosestPointTo 方法就可以了, 直到遍历完整个 coll 集合就能得到最短距离和其对应的点.
主要代码得到曲线集合 coll:
- public List<Curve> GetCurves(Curve curve ,double jd)
- {
- List<Curve> lstCurves = new List<Curve>();
- double totalLength = curve.GetDistanceAtParameter(curve.EndParam);
- if (totalLength <jd)
- {
- lstCurves.Add(curve);
- return lstCurves;
- }
- double addLength = 0;
- Point3dCollection pt3dCol = new Point3dCollection();
- while (addLength < totalLength)
- {
- pt3dCol.Add(curve.GetPointAtDist(addLength));
- addLength += jd;
- }
- if (addLength != totalLength)
- pt3dCol.Add(curve.GetPointAtDist(totalLength));
- DBObjectCollection dbObjColl= curve.GetSplitCurves(pt3dCol);
- foreach (var item in dbObjColl)
- {
- lstCurves.Add((Curve)item);
- }
- dbObjColl.Dispose();
- return lstCurves;
- }
- View Code
主要代码得到最短距离和最近点:
- public Line GetMinLine(Curve curve1,Curve curve2,double jd)
- {
- List<Curve> lstCurves = GetCurves(curve1, jd);
- double minVal = double.MaxValue;
- Point3d ptMin1 = Point3d.Origin;
- Point3d ptMin2 = Point3d.Origin;
- foreach (var c in lstCurves)
- {
- Point3d pt1 = c.StartPoint;
- Point3d pt2 = c.EndPoint;
- var pt11=curve2.GetClosestPointTo(pt1, false);
- var pt22= curve2.GetClosestPointTo(pt2, false);
- var l1 = pt11.DistanceTo(pt1);
- var l2 = pt22.DistanceTo(pt2);
- if (l1 < minVal)
- {
- minVal = l1;
- ptMin1 = pt11;
- ptMin2 = pt1;
- }
- if (l2 < minVal)
- {
- minVal = l2;
- ptMin1 = pt22;
- ptMin2 = pt2;
- }
- }
- ed.WriteMessage("\n 最短距离:" + minVal + "\n");
- return new Line(ptMin1,ptMin2);
- }
- View Code
关于 GetClosestPointTo 介绍如下:
来源: https://www.cnblogs.com/HelloQLQ/p/12112982.html