/// <summary> /// Splits the path by the supplied by line /// </summary> /// <param name="path">Path to split</param> /// <param name="cutter">Cutter to cut the path by</param> /// <returns>An enumeration of parts, or a single part if not split occurred</returns> public static IEnumerable <ILucidLine> Split(IEnumerable <IPoint> path, ILucidLine cutter) { var current = new LucidLine(); current.AddVertex(path.First()); foreach (var vertices in path.SlidingWindow(2)) { var line = new AlgebraicLine(vertices); var lineExtent = CalculateExtent(line); foreach (var cutterLine in cutter.Vertices.SlidingWindow(2)) { var byLine = new AlgebraicLine(cutterLine); var intersection = Intersection(line, byLine); if (intersection != null && Intersects(intersection, lineExtent) && Intersects(intersection, CalculateExtent(byLine))) { current.AddVertex(intersection); yield return(current); current = new LucidLine(); current.AddVertex(intersection); break; } } current.AddVertex(line.End); } yield return(current); }
/// <summary> /// Splits the path at the given point if the point lies on the path. /// </summary> /// <param name="path">Path to split</param> /// <param name="splitPoint">Point at which to cut the path</param> /// <returns>An enumeration of parts, or a single part if no split occurred</returns> public static IEnumerable <ILucidLine> Split(IEnumerable <IPoint> path, IPoint splitPoint) { var current = new LucidLine(); current.AddVertex(path.First()); foreach (var vertices in path.SlidingWindow(2)) { var line = new AlgebraicLine(vertices); var lineExtent = CalculateExtent(line); if (Intersects(splitPoint, lineExtent)) { current.AddVertex(splitPoint); yield return(current); current = new LucidLine(); current.AddVertex(splitPoint); } current.AddVertex(line.End); } yield return(current); }
/// <summary> /// Returns the point of intersection or null if they are parallel /// </summary> public static IPoint Intersection(AlgebraicLine first, AlgebraicLine second) { var delta = (first.DeltaY * second.DeltaX) - (second.DeltaY * first.DeltaX); if (delta == 0) { return(null); } double x = (second.DeltaX * first.C - first.DeltaX * second.C) / delta; double y = (first.DeltaY * second.C - second.DeltaY * first.C) / delta; return(new LucidVertex() { X = x, Y = y }); }