Solved! Go to Solution.
public static class GeometryExtension { public static MapPoint MidPoint(this Polyline polyline) { var length = polyline.Length(); var m = length / 2.0; return polyline.LinearPosition(m); } // length of a multipath polyline public static double Length(this Polyline polyline) { return polyline.Paths.Sum((Func<PointCollection, double>)Length); } // length of one path private static double Length(this PointCollection points) { return points.Zip(points.Skip(1), Length).Sum(); } // length of a segment private static double Length(MapPoint point1, MapPoint point2) { return Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2)); } // Get a M position on a multipath polyline public static MapPoint LinearPosition(this Polyline polyline, double m) { PointCollection path1 = polyline.Paths.Where(path => DecrementTotal(path.Length(), ref m)) .DefaultIfEmpty(polyline.Paths.Last()) // for the case where m > polyline length .First(); return path1.LinearPosition(m); } // Get a M position on a polyline private static MapPoint LinearPosition(this IEnumerable<MapPoint> points, double m) { var seg = points.Zip(points.Skip(1), (pt1, pt2) => new Tuple<MapPoint, MapPoint>(pt1, pt2)) .Where(segment => DecrementTotal(Length(segment.Item1, segment.Item2), ref m)) .FirstOrDefault(); return seg == null ? points.Last() : seg.LinearPosition(m); } // Get a M position on a segment private static MapPoint LinearPosition(this Tuple<MapPoint, MapPoint> segment, double m) { var ratio = m / Length(segment.Item1, segment.Item2); return new MapPoint(segment.Item1.X + ratio * (segment.Item2.X - segment.Item1.X), segment.Item1.Y + ratio * (segment.Item2.Y - segment.Item1.Y)); } private static bool DecrementTotal(double l, ref double cumul) { if (l >= cumul) return true; cumul -= l; return false; } }
public static class GeometryExtension { public static MapPoint MidPoint(this Polyline polyline) { var length = polyline.Length(); var m = length / 2.0; return polyline.LinearPosition(m); } // length of a multipath polyline public static double Length(this Polyline polyline) { return polyline.Paths.Sum((Func<PointCollection, double>)Length); } // length of one path private static double Length(this PointCollection points) { return points.Zip(points.Skip(1), Length).Sum(); } // length of a segment private static double Length(MapPoint point1, MapPoint point2) { return Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2)); } // Get a M position on a multipath polyline public static MapPoint LinearPosition(this Polyline polyline, double m) { PointCollection path1 = polyline.Paths.Where(path => DecrementTotal(path.Length(), ref m)) .DefaultIfEmpty(polyline.Paths.Last()) // for the case where m > polyline length .First(); return path1.LinearPosition(m); } // Get a M position on a polyline private static MapPoint LinearPosition(this IEnumerable<MapPoint> points, double m) { var seg = points.Zip(points.Skip(1), (pt1, pt2) => new Tuple<MapPoint, MapPoint>(pt1, pt2)) .Where(segment => DecrementTotal(Length(segment.Item1, segment.Item2), ref m)) .FirstOrDefault(); return seg == null ? points.Last() : seg.LinearPosition(m); } // Get a M position on a segment private static MapPoint LinearPosition(this Tuple<MapPoint, MapPoint> segment, double m) { var ratio = m / Length(segment.Item1, segment.Item2); return new MapPoint(segment.Item1.X + ratio * (segment.Item2.X - segment.Item1.X), segment.Item1.Y + ratio * (segment.Item2.Y - segment.Item1.Y)); } private static bool DecrementTotal(double l, ref double cumul) { if (l >= cumul) return true; cumul -= l; return false; } }