From de4d08b93822de5424bb616aaf90519192fac073 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 15 May 2019 16:46:55 +0200 Subject: [PATCH] update --- TMI-practicum/Circle.cs | 10 ++++++---- TMI-practicum/SimpleAlgorithm.cs | 8 ++++++-- TMI-practicum/SweepLineAlgorithm.cs | 10 ++++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/TMI-practicum/Circle.cs b/TMI-practicum/Circle.cs index 415db43..73f3808 100644 --- a/TMI-practicum/Circle.cs +++ b/TMI-practicum/Circle.cs @@ -23,7 +23,7 @@ namespace TMI_practicum public IList FindIntersections(Circle c1) { - var intersections = new List(); + IList intersections; double d = Distance(c1); if (d > R + c1.R || d < Math.Abs(R - c1.R) || d == 0.0 && R - c1.R == 0.0) return null; @@ -35,13 +35,15 @@ namespace TMI_practicum double htemp = Math.Round(R * R - a * a, 15); if (htemp == 0) { - intersections.Add(new Intersection(px, py)); + intersections = new Intersection[1]; + intersections[0] = new Intersection(px, py); } else { + intersections = new Intersection[2]; double h = Math.Round(Math.Sqrt(htemp), 15); - intersections.Add(new Intersection(px + h * (c1.Y - Y) / d, py - h * (c1.X - X) / d)); - intersections.Add(new Intersection(px - h * (c1.Y - Y) / d, py + h * (c1.X - X) / d)); + intersections[0] = new Intersection(px + h * (c1.Y - Y) / d, py - h * (c1.X - X) / d); + intersections[1] = new Intersection(px - h * (c1.Y - Y) / d, py + h * (c1.X - X) / d); } return intersections; diff --git a/TMI-practicum/SimpleAlgorithm.cs b/TMI-practicum/SimpleAlgorithm.cs index 18048b6..afebc16 100644 --- a/TMI-practicum/SimpleAlgorithm.cs +++ b/TMI-practicum/SimpleAlgorithm.cs @@ -6,7 +6,7 @@ namespace TMI_practicum { public static IEnumerable Solve(IList circles) { - var intersections = new List(); + var intersections = new HashSet(); for (int i = 0; i < circles.Count - 1; i++) { @@ -15,7 +15,11 @@ namespace TMI_practicum var c1 = circles[i]; var c2 = circles[j]; var intersects = c1.FindIntersections(c2); - if (intersects != null) intersections.AddRange(c1.FindIntersections(c2)); + if (intersects == null) continue; + foreach (var intersect in intersects) + { + intersections.Add(intersect); + } } } diff --git a/TMI-practicum/SweepLineAlgorithm.cs b/TMI-practicum/SweepLineAlgorithm.cs index b35bcc0..13061ef 100644 --- a/TMI-practicum/SweepLineAlgorithm.cs +++ b/TMI-practicum/SweepLineAlgorithm.cs @@ -9,7 +9,7 @@ namespace TMI_practicum public static IEnumerable Solve(IList circles) { var events = new PriorityQueue(); - var intersections = new List(); + var intersections = new HashSet(); var active = new List(); foreach (var circle in circles) @@ -21,7 +21,6 @@ namespace TMI_practicum while (events.Count != 0) { Event e = events.Dequeue(); - switch (e.Type) { case Event.EventType.Start: @@ -39,7 +38,7 @@ namespace TMI_practicum return intersections; } - private static void CheckIntersections(IEnumerable active, Circle circle, List intersections) + private static void CheckIntersections(IEnumerable active, Circle circle, HashSet intersections) { foreach (var otherCircle in active) { @@ -48,7 +47,10 @@ namespace TMI_practicum var intersects = circle.FindIntersections(otherCircle); if (intersects == null) continue; - intersections.AddRange(intersects); + foreach (var intersection in intersects) + { + intersections.Add(intersection); + } } }