/// <summary> /// 求最近点 /// </summary> public WPoint2D ClosestPointTo(WPoint2D P) { WVector2D v; /////P1到P的向量 WVector2D d; /////P1到P2向量 double dotProduct; WVector2D alongVector; double L; WPoint2D Pn = Vertexs[0]; double DisMin = double.MaxValue; WPoint2D PointMin = Vertexs[0]; double Dis; for (int i = 0; i < Vertexs.Count - 1; i++) { v = Vertexs[i].VectorTo(P); d = Vertexs[i].VectorTo(Vertexs[i + 1]); L = d.Length; d = d.Normalize(); dotProduct = v.DotProduct(d); ///// if (dotProduct < 0) { dotProduct = 0; } if (dotProduct > L) { dotProduct = L; } alongVector = dotProduct * d; Pn = Vertexs[i] + alongVector; Dis = P.DistanceTo(Pn); if (Dis < DisMin) { DisMin = Dis; PointMin = Pn; } } return(Pn); }
/// <summary> /// 得到点到线的距离 /// </summary> public override double GetDistance(WPoint2D P) { return(P.DistanceTo(this.ClosestPointTo(P, false))); }