diff --git a/Region.cs b/Region.cs index 09b643c..c2c486e 100644 --- a/Region.cs +++ b/Region.cs @@ -151,38 +151,33 @@ namespace Boprs // Lexico-graphically sort the vertices. vertices.Sort(); - // Sweepline to keep track of edges. - SweepLine sweepLine = new SweepLine(); - bool[] processed = new bool[vertices.Count]; List contours = new List(); for(int i = 0; i < vertices.Count; i++) { - SweepVertex acVx = vertices[i]; - - if (acVx.IsLeft()) - { - sweepLine.Add(acVx.Edge, false); - } - else - { - sweepLine.Remove(acVx.Edge); - } - + // If we already traversed this vertex, skip it. if (processed[i]) { continue; } + // Otherwise this is an edge on an indiscovered contour, create it. Contour contour = new Contour(); contours.Add(contour); - SweepEdge prev = sweepLine.PrevEdge(acVx.Edge); - bool shouldBeCW = prev == null ? false : prev.TransitionInside; + // acVx should be guaranteed to be the lowest and leftest vertex of the new polygon. + // Thus it cannot be vertical. + // if it is transition inside, it is a positive contour and should wind counter-clockwise, + // if it is transition outside, it is a negative contour and should wind clockwise. + SweepVertex acVx = vertices[i]; + bool shouldBeCW = !acVx.Edge.TransitionInside; Point2d target = acVx.Point; + + // Traverse connected edges, and mark taversed vertices as processed, + // until we loop back to the first discovered vertex on the contour. int pos = i; processed[i] = true; contour.Vertices.Add(vertices[pos].Point); @@ -200,6 +195,7 @@ namespace Boprs } processed[vertices.IndexOf(vertices[pos].OtherVertex())] = true; + // If the contour is a hole and winds ccw, or positive and winds cw, flip it. if (shouldBeCW != contour.IsClockwise()) { contour.Reverse();