/// <summary> /// 获取有效的折线 /// </summary> /// <param name="PL">折线PL</param> /// <returns>返回折线</returns> public static PolylineD?GetValidatePolyline(PolylineD PL) { List <PointD> pts = new List <PointD>(); for (Int32 i = 0; i < PL.Points.Count; ++i) { if (pts.Contains(PL.Points[i]) == false) { if (pts.Count > 2) { //斜率相等 if (LineAlgorithm.Gradient(new LineD(pts[pts.Count - 2], pts[pts.Count - 1])) == LineAlgorithm.Gradient(new LineD(pts[pts.Count - 1], PL.Points[i]))) { pts[pts.Count - 1] = PL.Points[i]; } else { pts.Add(PL.Points[i]); } } } } if (pts.Count < 3) { return(null); } return(new PolylineD(pts.ToArray())); }
/// <summary> /// 计算点P到折线PL的最近距离 /// </summary> /// <param name="P">点P</param> /// <param name="PL">折线PL</param> /// <returns>返回最近距离</returns> public static Double ClosestDistance(PointD P, PolylineD PL) { Double result = Double.MaxValue; foreach (LineD S in PL) { result = System.Math.Min(result, ClosestDistance(P, S)); } return(result); }
/// <summary> /// 计算偏移折线PL /// </summary> /// <param name="PL">折线PL</param> /// <param name="velocity">偏移速度</param> /// <returns>返回偏移折线</returns> public static PolylineD Offset(PolylineD PL, PointD velocity) { List <PointD> list = new List <PointD>(); for (Int32 i = 0; i < PL.Points.Count; ++i) { list.Add(PL.Points[i] + velocity); } return(new PolylineD(list.ToArray())); }
/// <summary> /// 判断折线PL是否在多边形内 /// </summary> /// <param name="PG">PG多边形</param> /// <param name="PL">PL折线</param> /// <returns>如果PL折线在区域内返回True,否则返回False.</returns> public static Boolean InPolygon(PolygonD PG, PolylineD PL) { foreach (LineD L in PL) { if (InPolygon(PG, L) == false) { return(false); } } return(true); }
/// <summary> /// 判断折线PL是否在圆内 /// </summary> /// <param name="C">圆C</param> /// <param name="PL">折线PL</param> /// <returns>如果在圆内返回True,否则返回False。</returns> public static Boolean InCircle(CircleD C, PolylineD PL) { if (PL.Points == null) { return(false); } for (Int32 i = 0; i < PL.Points.Count; ++i) { if (PointAlgorithm.Distance(PL.Points[i], C.Center) > C.Radius) { return(false); } } return(true); }
/// <summary> /// 折线是否在区域内 /// </summary> /// <param name="Rect">矩形区域</param> /// <param name="PL">折线PL</param> /// <returns> /// 返回True表示折线PL在区域内,返回False则不在区域内. /// </returns> public static Boolean InRectangle(RectangleD Rect, PolylineD PL) { if (PL.Points == null) { return(false); } for (Int32 i = 0; i < PL.Points.Count; ++i) { if (false == InRectangle(Rect, PL.Points[i])) { return(false); } } return(true); }
/// <summary> /// 获取线段L与折线PL的交点集合 /// </summary> /// <param name="L">线段L</param> /// <param name="PL">折线PL</param> /// <returns>返回交点集合,如果有无数个交点则返回null.</returns> public static PointD[] Intersection(LineD L, PolylineD PL) { List <PointD> result = new List <PointD>(); foreach (LineD S in PL) { PointD[] P = Intersection(L, S); if (P == null) { return(null); } if (P.Length == 0) { continue; } if (result.Contains(P[0]) == false) { result.Add((PointD)P[0]); } } return(result.ToArray()); }
/// <summary> /// 判断折线PL与线段L的交点个数 /// </summary> /// <param name="L">线段L</param> /// <param name="PL">折线PL</param> /// <returns>相交返回交点数目,否则返回0</returns> public static Int32?HasIntersection(LineD L, PolylineD PL) { Int32 count = 0; foreach (LineD S in PL) { Int32?result = HasIntersection(L, S); if (result == null) { return(null); } count += (Int32)result; } for (Int32 i = 1; i < PL.Points.Count - 1; ++i)//排除折线的开始和结束顶点 { if (OnLine(L, PL.Points[i]) == true) { count--; } } return(count); }