public static PointOnPathSegment findSelfIntersection(Path path, int fromIndex, Vector2 normal) { // start at segment after next segment int prev_i = nextI(path, fromIndex); int i = nextI(path, prev_i); //Debug.Log(normal); while (i != fromIndex) { PointOnPathSegment pops = raySegmentIntersection(IP2Vector2(path[prev_i]), IP2Vector2(path[i]), IP2Vector2(path[fromIndex]), normal); //Debug.Log(" ---X " + inter); if (pops.point.X != -999999 && pops.point.Y != -999999) { // We have found an intersection point! pops.index = i; return(pops); } prev_i = i; i = nextI(path, i); } return(new PointOnPathSegment(-1, new IntPoint())); }
/// <summary> /// Splits the into convex paths. /// If the Path has a concave angle, the bisector is extended to find an intersection /// with line sgements further allong. /// Once the intersection is found two paths are generated and returned. // This is run recursively. /// </summary> /// <returns>A set of convex paths.</returns> /// <param name="path">Path.</param> public static Paths splitIntoConvexPaths(Path path, int gener = 0, int part = 0) { Paths returnPaths = new Paths(); if (gener > 25) { returnPaths.Add(path); return(returnPaths); } // Debug.Log("["+gener+"]["+part+"] * * * * * * * * * * * * splitIntoConvexPaths * * * * * * * * * * * *"); // Pather.printPath(path); for (int i = 0; i < path.Count; i++) { VertexProperties vps = getVertexProperties(path, i); //Debug.Log(bevelAng); if (vps.bevelAngle < 0f) { Vector2 nBisector = -vps.bisector; //Debug.Log("CONCAVE FOUND at (" + i + "): " + path[i] +" this is concave! bisector=" + nBisector); PointOnPathSegment pops = findSelfIntersection(path, i, nBisector); //Debug.Log("Intersection Before: "+pops.index+", (" + pops.point.X+", "+pops.point.Y+")"); if (pops.index >= 0) { // split paths at this index // A Path a = new Path(); a.Add(path[i]); if (pops.t < .2f) { a.Add(path[prevI(path, pops.index)]); } else if (pops.t < .9f) { a.Add(pops.point); } int j = pops.index; while (j != i) { a.Add(path[j]); j = nextI(path, j); } // B Path b = new Path(); //Debug.Log (gener + ":"+part+ "; t="+pops.t + ", index="+pops.index); if (pops.t < .9f) { if (pops.t > .2f) { b.Add(pops.point); } } else { b.Add(path[pops.index]); } b.Add(path[i]); j = nextI(path, i); while (j != pops.index) { b.Add(path[j]); j = nextI(path, j); } //Debug.Log("["+gener+"] ADD A %%%%"); returnPaths.AddRange(splitIntoConvexPaths(a, gener + 1, 1)); //Debug.Log("["+gener+"] ADD A %%%% -- END"); //Debug.Log("["+gener+"] ADD B %%%%"); returnPaths.AddRange(splitIntoConvexPaths(b, gener + 1, 2)); //Debug.Log("["+gener+"] ADD B %%%% -- END"); //printPaths(returnPaths); } // if (gener == 0) // printPaths(returnPaths); return(returnPaths); } } //Debug.Log("!!! ["+gener+"] No concave found.............."); returnPaths.Add(path); return(returnPaths); }