private void AddTriangle(float angle1, float angle2, Segment segment, Segment previous) { Vector2 delta1 = new Vector2(Mathf.Cos(angle1), Mathf.Sin(angle1)); Vector2 delta2 = new Vector2(Mathf.Cos(angle2), Mathf.Sin(angle2)); Vector2 p1 = Center; Vector2 p2 = p1 + delta1; Vector2 p3 = new Vector2(0.0f, 0.0f); Vector2 p4 = new Vector2(0.0f, 0.0f); Gizmos.color = Color.blue; if (segment != null) { // Stop the triangle at the intersecting segment p3 = segment.Start.Point; p4 = segment.End.Point; } else { // Stop the triangle at a fixed distance; this probably is // not what we want, but it never gets used in the demo p3 = p1 + delta1 * 500; p4 = p1 + delta2 * 500; } Vector2 pBegin = ShadowMathUtils.LineIntersection(p3, p4, p1, p2); p2 = p1 + delta2; Vector2 pEnd = ShadowMathUtils.LineIntersection(p3, p4, p1, p2); if (_drawGizmos) { Gizmos.color = Color.green; Gizmos.DrawLine(p1, pBegin); Gizmos.DrawLine(p2, pEnd); //Gizmos.DrawLine(pBegin, pEnd); Gizmos.color = Color.yellow; Gizmos.DrawSphere(pBegin, 0.1f); Gizmos.color = Color.blue; Gizmos.DrawSphere(pEnd, 0.1f); } if (previous != null && ShadowMathUtils.Approximately(segment.Slope, previous.Slope) && ShadowMathUtils.Approximately(Output.Last(), pBegin)) { // It's a continuation of the previous segment! Output.RemoveAt(Output.Count - 1); Output.Add(pEnd); } else { Output.Add(pBegin); Output.Add(pEnd); } }
private void SplitSegmentsThatOverlap() { // O(N^2) Queue <Segment> open = new Queue <Segment>(Segments); Segments.Clear(); while (open.Any()) { if (Segments.Count > 300) { Debug.Log("Too many segment splits!"); return; } Segment segment = open.Dequeue(); bool splitFree = true; foreach (Segment testing in Segments) { Segment.IntersectionResult intersection = Segment.IsInFrontOf(segment, testing, Center); if (intersection == Segment.IntersectionResult.Intersects) { // Do they *really* overlap, there seems to be some issues with the IsInFrontOf method. if (ShadowMathUtils.Approximately(testing.Start.Point, segment.Start.Point) || ShadowMathUtils.Approximately(testing.End.Point, segment.End.Point) || ShadowMathUtils.Approximately(testing.End.Point, segment.Start.Point) || ShadowMathUtils.Approximately(testing.Start.Point, segment.End.Point)) { //intersection = Segment.IsInFrontOf(segment, testing, Center); //Debug.Log("They're the same point!"+intersection); continue; } // segment && testing overlap, we must split one of them but both must go back on the queue Segments.Remove(testing); open.Enqueue(testing); Vector2 position = ShadowMathUtils.LineIntersection( segment.Start.Point, segment.End.Point, testing.Start.Point, testing.End.Point); if (_drawGizmos) { Gizmos.color = Color.red; Gizmos.DrawSphere(position, 0.2f); } // Split segments and add both parts back to the queue Split(segment, position, open); splitFree = false; break; } } if (splitFree) { Segments.Add(segment); } } }