From eb2e9172a9e4c3ff0d1fdf4c2b593f5899a3fee6 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Tue, 21 May 2019 20:50:35 +0200 Subject: [PATCH] update: --- TMI-practicum/Circle.cs | 6 +++--- TMI-practicum/Program.cs | 26 +++++++++++++++++++++++++- TMI-practicum/SweepLineEffAlgorithm.cs | 17 ++++++----------- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/TMI-practicum/Circle.cs b/TMI-practicum/Circle.cs index 7c6f20f..73d15c4 100644 --- a/TMI-practicum/Circle.cs +++ b/TMI-practicum/Circle.cs @@ -32,8 +32,8 @@ namespace TMI_practicum double px = X + a * (c1.X - X) / d; double py = Y + a * (c1.Y - Y) / d; - double htemp = Math.Round(R * R - a * a, 15); - if (htemp == 0) + double htemp = R * R - a * a; + if (Math.Round(htemp, 15) == 0) { intersections = new Intersection[1]; intersections[0] = new Intersection(Math.Round(px, 15), Math.Round(py, 15)); @@ -41,7 +41,7 @@ namespace TMI_practicum else { intersections = new Intersection[2]; - double h = Math.Round(Math.Sqrt(htemp), 15); + double h = Math.Sqrt(htemp); intersections[0] = new Intersection(Math.Round(px + h * (c1.Y - Y) / d, 15), Math.Round(py - h * (c1.X - X) / d, 15)); intersections[1] = new Intersection(Math.Round(px - h * (c1.Y - Y) / d, 15), Math.Round(py + h * (c1.X - X) / d, 15)); } diff --git a/TMI-practicum/Program.cs b/TMI-practicum/Program.cs index 7a8e72a..39337e2 100644 --- a/TMI-practicum/Program.cs +++ b/TMI-practicum/Program.cs @@ -78,10 +78,12 @@ namespace TMI_practicum solved.Contains(new Intersection(intersection.X, intersection.Y + 10e-15)) || solved.Contains(new Intersection(intersection.X, intersection.Y - 10e-15)) ) continue; + + Console.WriteLine(InCircles(circles)); failed = true; Console.WriteLine("Not found: {0}\t{1}", intersection.X, intersection.Y); - Console.WriteLine("corrrect"); + Console.WriteLine("correct"); foreach (var intersection1 in correctSol) { Console.WriteLine("{0}\t{1}", intersection1.X, intersection1.Y); @@ -203,5 +205,27 @@ namespace TMI_practicum sw.WriteLine("\r\n{0}", time); } } + + + private static int InCircles(IList circles) + { + int count = 0; + + foreach (var toCheck in circles) + { + foreach (var circle in circles) + { + if (circle == toCheck) continue; + + if (circle.R > toCheck.R && circle.Distance(toCheck) < circle.R && + circle.FindIntersections(toCheck) == null) + { + count++; + } + } + } + + return count; + } } } \ No newline at end of file diff --git a/TMI-practicum/SweepLineEffAlgorithm.cs b/TMI-practicum/SweepLineEffAlgorithm.cs index 8437405..39e7508 100644 --- a/TMI-practicum/SweepLineEffAlgorithm.cs +++ b/TMI-practicum/SweepLineEffAlgorithm.cs @@ -117,12 +117,6 @@ namespace TMI_practicum if (intersects == null) return; foreach (var intersection in intersects) { -// if (upper) -// { -// if (intersection.Y < circle.Y) continue; -// } -// else if (intersection.Y > circle.Y) continue; - if (intersections.Contains(intersection)) continue; events.Add(new Event(Event.EventType.Intersection, intersection.X, circle, intersection.Y, new[] {new C5.KeyValuePair(segment, circle), neighbour})); @@ -197,7 +191,7 @@ namespace TMI_practicum { get { - if (_lastX.Equals(_sweepline)) return _lastY; + if (_lastX == _sweepline) return _lastY; _lastX = _sweepline; _lastY = CalculateY(_circle, Type); return _lastY; @@ -218,10 +212,10 @@ namespace TMI_practicum public int CompareTo(SegmentKey other) { - if (_identifier.Equals(other._identifier)) return Type.CompareTo(other.Type); - if (!Y.Equals(other.Y)) return Y.CompareTo(other.Y); + if (_identifier == other._identifier) return Type.CompareTo(other.Type); + if (Y != other.Y) return Y.CompareTo(other.Y); - return Type.Equals(other.Type) ? _identifier.CompareTo(other._identifier) : Type.CompareTo(other.Type); + return Type == other.Type ? _identifier.CompareTo(other._identifier) : Type.CompareTo(other.Type); } public override bool Equals(object obj) @@ -229,7 +223,7 @@ namespace TMI_practicum if (obj == null || GetType() != obj.GetType()) return false; SegmentKey r = (SegmentKey) obj; - return _identifier.Equals(r._identifier) && Type.Equals(r.Type); + return _identifier == r._identifier && Type == r.Type; } public override int GetHashCode() @@ -246,6 +240,7 @@ namespace TMI_practicum private static double CalculateY(Circle circle, SegmentKey.SegmentType type) { + if (_sweepline > circle.X + circle.R || _sweepline < circle.X - circle.R) return circle.Y; return type == SegmentKey.SegmentType.Upper ? Math.Round(Math.Sqrt(circle.R * circle.R - Math.Pow(_sweepline - circle.X, 2)) + circle.Y, 14) : Math.Round(-Math.Sqrt(circle.R * circle.R - Math.Pow(_sweepline - circle.X, 2)) + circle.Y, 14);