public void GetLine() { XYPolyline xyPolyline = new XYPolyline(); xyPolyline.Points.Add(new XYPoint(6,2)); xyPolyline.Points.Add(new XYPoint(2,2)); xyPolyline.Points.Add(new XYPoint(8,2)); xyPolyline.Points.Add(new XYPoint(8,4)); xyPolyline.Points.Add(new XYPoint(5,4)); xyPolyline.Points.Add(new XYPoint(9,7)); Assert.AreEqual(new XYLine(6,2,2,2),xyPolyline.GetLine(0)); Assert.AreEqual(new XYLine(2,2,8,2),xyPolyline.GetLine(1)); Assert.AreEqual(new XYLine(8,2,8,4),xyPolyline.GetLine(2)); Assert.AreEqual(new XYLine(8,4,5,4),xyPolyline.GetLine(3)); Assert.AreEqual(new XYLine(5,4,9,7),xyPolyline.GetLine(4)); }
/// <summary> /// Finds the shortest distance between any line segment of the polyline /// and the point. /// </summary> /// <param name="polyLine">PolyLine.</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the polyline and the point.</p> /// </returns> public static double CalculatePolylineToPointDistance(XYPolyline polyLine, IXYPoint point) { double dist = 0; int i = 0; while (i < polyLine.Points.Count - 1) { if (i == 0) { dist = CalculateLineToPointDistance(polyLine.GetLine(0), point); } else { dist = Math.Min(dist, CalculateLineToPointDistance(polyLine.GetLine(i), point)); } i++; } return(dist); }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line, polygon); } return(lengthInside); }
/// <summary> /// Does the same as CalculatePolylineToPointDistance but also returns the line number /// </summary> /// <param name="polyLine"></param> /// <param name="point"></param> /// <returns></returns> public static Tuple<int, double> GetClosetLine (XYPolyline polyLine, IXYPoint point) { double dist = double.MaxValue; int i = 0; int mini=0; while (i < polyLine.Points.Count - 1) { var newdist = CalculateLineToPointDistance(polyLine.GetLine(i), point); if(newdist<dist) { dist=newdist; mini =i; } i++; } return new Tuple<int, double>(mini, dist); }
/// <summary> /// Finds the shortest distance between any line segment of the polyline /// and the point. /// </summary> /// <param name="polyLine">PolyLine.</param> /// <param name="point">Point</param> /// <returns> /// <p>Length of the shortest path between the polyline and the point.</p> /// </returns> public static double CalculatePolylineToPointDistance (XYPolyline polyLine, IXYPoint point) { double dist = 0; int i = 0; while (i < polyLine.Points.Count - 1) { if (i == 0) { dist = CalculateLineToPointDistance (polyLine.GetLine(0), point); } else { dist = Math.Min(dist, CalculateLineToPointDistance (polyLine.GetLine(i), point)); } i++; } return dist; }
/// <summary> /// Calculates the length of polyline inside polygon. Lines segments on the edges of /// polygons are included with half their length. /// </summary> /// <param name="polyline">Polyline</param> /// <param name="polygon">Polygon</param> /// <returns> /// Length of polyline inside polygon. /// </returns> public static double CalculateLengthOfPolylineInsidePolygon(XYPolyline polyline, XYPolygon polygon) { double lengthInside = 0; int numberOfLineSegments = polyline.Points.Count - 1; for (int i = 0; i < numberOfLineSegments; i++) { XYLine line = new XYLine(polyline.GetLine(i)); lengthInside += CalculateLengthOfLineInsidePolygon(line,polygon); } return lengthInside; }