diff --git a/TMI-practicum/SweepLineEffAlgorithm.cs b/TMI-practicum/SweepLineEffAlgorithm.cs index f60ca73..ff080e5 100644 --- a/TMI-practicum/SweepLineEffAlgorithm.cs +++ b/TMI-practicum/SweepLineEffAlgorithm.cs @@ -29,16 +29,20 @@ namespace TMI_practicum switch (e.Type) { case Event.EventType.Start: - upperActive.Add(new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, hash), e.Circle); - bottomActive.Add(new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, hash), + upperActive.Add( + new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, hash), e.Circle); + bottomActive.Add( + new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, hash), e.Circle); - + CheckIntersections(upperActive, e, intersections, true); CheckIntersections(bottomActive, e, intersections, false); break; - case Event.EventType.End: - upperActive.Remove(new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, hash)); - bottomActive.Remove(new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, hash)); + case Event.EventType.End: + upperActive.Remove(new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, + hash)); + bottomActive.Remove(new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, + hash)); break; default: throw new ArgumentOutOfRangeException(); @@ -48,16 +52,18 @@ namespace TMI_practicum return intersections; } - - private static void CheckIntersections(CircleTree active, Event e, Stack intersections, bool upper) + + private static void CheckIntersections(CircleTree active, Event e, + Stack intersections, bool upper) { - Circle other = null; try { - other = upper? active.FindSuccessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Upper, e.GetHashCode())) - : - active.FindPredecessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Bottom, e.GetHashCode())); + other = upper + ? active.FindSuccessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Upper, + e.GetHashCode())) + : active.FindPredecessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Bottom, + e.GetHashCode())); } catch (KeyNotFoundException) { @@ -68,7 +74,6 @@ namespace TMI_practicum var intersects = e.Circle.FindIntersections(other); if (intersects != null) { - foreach (var intersection in intersects) { if (upper && e.Circle.Y == intersection.Y) continue; @@ -155,7 +160,7 @@ namespace TMI_practicum { return _identifier.GetHashCode() ^ _type.GetHashCode(); } - + public enum SegmentType { Bottom, @@ -163,15 +168,17 @@ namespace TMI_practicum } } } + public static class EnumerableExtensions { public static T MinOrDefault(this IEnumerable sequence) { - return sequence.Any() ? sequence.Min() : default(T); + return sequence.Any() ? sequence.Min() : default; } + public static T MaxOrDefault(this IEnumerable sequence) { - return sequence.Any() ? sequence.Max() : default(T); + return sequence.Any() ? sequence.Max() : default; } } } \ No newline at end of file