diff options
author | Toby Vincent <tobyv13@gmail.com> | 2021-10-08 18:31:37 -0500 |
---|---|---|
committer | Toby Vincent <tobyv13@gmail.com> | 2021-10-08 18:31:37 -0500 |
commit | cacd18172862482ad55782c772af2c4c108a0eee (patch) | |
tree | 6c76eea483bb40478004cf7ad68e9189982f8329 | |
parent | a6a6313f8f37e8e96d0090f5439dbea3df6a1ecf (diff) |
removed PointArray class
-rw-r--r-- | src/ClosestPair/ClosestPair.cs | 21 | ||||
-rw-r--r-- | src/ClosestPair/Extensions.cs | 15 | ||||
-rw-r--r-- | src/ClosestPair/Point.cs | 33 | ||||
-rw-r--r-- | src/ClosestPair/PointArray.cs | 33 | ||||
-rw-r--r-- | src/ClosestPair/PointPair.cs | 1 | ||||
-rw-r--r-- | src/Program/Program.cs | 3 |
6 files changed, 43 insertions, 63 deletions
diff --git a/src/ClosestPair/ClosestPair.cs b/src/ClosestPair/ClosestPair.cs index 2238275..fcc25b6 100644 --- a/src/ClosestPair/ClosestPair.cs +++ b/src/ClosestPair/ClosestPair.cs @@ -8,7 +8,7 @@ namespace ClosestPair { public (Point, Point) closestPair; - public static PointPair FindClosestPair(PointArray points) + public static PointPair FindClosestPair(Point[] points) { /* (Point, Point) ClosestPair(list Points) base function @@ -21,7 +21,10 @@ namespace ClosestPair // points[1] // points.SortedByX[1] // points.SortedByY[1] - return ClosestPairRecursive(points.SortedByX, points.SortedByY); + Point[] Px = points.SortByX(); + Point[] Py = points.SortByY(); + + return ClosestPairRecursive(Px, Py); } static PointPair ClosestPairRecursive(Point[] Px, Point[] Py) @@ -29,8 +32,6 @@ namespace ClosestPair if (Px.Length <= 3) return BruteForce(Px); - int middle = Px.Length / 2; - // Creates four lists, needs to be O(n log n) // lists: Qx, Qy (first half of Px and Py) // Rx, Ry (last half of Px and Py) @@ -50,7 +51,7 @@ namespace ClosestPair //Sx = points in Px within distance delta of L List<Point> Sx = new(); foreach (Point p in Px) - if (p.XDiff(L) < delta) + if (p.DistanceToX(L) < delta) Sx.Add(p); else if (Sx.Count > 0) break; @@ -58,17 +59,17 @@ namespace ClosestPair // Create Sy (Sx sorted by y) - PointArray S = new PointArray(Sx.ToArray(), isXSorted: true, isYSorted: false); + Point[] Sy = Sx.ToArray().SortByY(); // for each point s in Sy // compute distance from s // to each of next 15 points in Sy // Let (Point s0, Point s1) be minimum distance pair // All of this is O(n) - if (S.SortedByY.Length < 2) + if (Sy.Length < 2) return qPair.Distance < rPair.Distance ? qPair : rPair; - PointPair sPair = BruteForce(S.SortedByY, 7); + PointPair sPair = BruteForce(Sy, 7); if (sPair.Distance < delta) return sPair; @@ -85,12 +86,12 @@ namespace ClosestPair // return (Point r0, Point r1) } - static PointPair BruteForce(Point[] points) + static PointPair BruteForce(Point[] points) => BruteForce(points, points.Length); static PointPair BruteForce(Point[] points, int queueSize) { - PointPair closestPair = new(new Point(0, 0), new Point(int.MaxValue, int.MaxValue)); + PointPair closestPair = new(new(), new(int.MaxValue, int.MaxValue)); Queue<Point> pointQueue = new(); foreach (Point point in points) diff --git a/src/ClosestPair/Extensions.cs b/src/ClosestPair/Extensions.cs index 24bb43c..c117084 100644 --- a/src/ClosestPair/Extensions.cs +++ b/src/ClosestPair/Extensions.cs @@ -1,4 +1,7 @@ using System; +using System.Collections.Generic; +using ClosestPair; +using MergeSort; namespace Extensions { @@ -22,5 +25,17 @@ namespace Extensions Array.Copy(array, outArray, outArray.Length); return outArray; } + + public static Point[] SortByX(this Point[] points) + => MergeSort<Point>.Sort(points.Copy(), CompareX()); + + public static Point[] SortByY(this Point[] points) + => MergeSort<Point>.Sort(points.Copy(), CompareY()); + + static IComparer<Point> CompareX() + => Comparer<Point>.Create((p1, p2) => p1.X.CompareTo(p2.X)); + + static IComparer<Point> CompareY() + => Comparer<Point>.Create((p1, p2) => p1.Y.CompareTo(p2.Y)); } }
\ No newline at end of file diff --git a/src/ClosestPair/Point.cs b/src/ClosestPair/Point.cs index 9643e02..bab3126 100644 --- a/src/ClosestPair/Point.cs +++ b/src/ClosestPair/Point.cs @@ -3,10 +3,10 @@ using System.Collections.Generic; namespace ClosestPair { - public class Point + public struct Point { - public int X { get; set; } - public int Y { get; set; } + public int X { get; init; } + public int Y { get; init; } public Point(int x, int y) { @@ -16,23 +16,20 @@ namespace ClosestPair public Point(Point p) : this(p.X, p.Y) { } - public double DistanceTo(Point p) + public double DistanceTo(int x, int y) => Math.Sqrt( - Math.Pow(XDiff(p.X), 2) + - Math.Pow(YDiff(p.Y), 2) + Math.Pow(Math.Abs(X - x), 2) + + Math.Pow(Math.Abs(Y - y), 2) ); - - public double XDiff(int x) - => Math.Abs(X - x); - - public double YDiff(int y) - => Math.Abs(Y - y); - - public static IComparer<Point> CompareX() - => Comparer<Point>.Create((p1, p2) => p1.X.CompareTo(p2.X)); - - public static IComparer<Point> CompareY() - => Comparer<Point>.Create((p1, p2) => p1.Y.CompareTo(p2.Y)); + + public double DistanceTo(Point p) + => DistanceTo(p.X, p.Y); + + public double DistanceToX(int x) + => DistanceTo(x, Y); + + public double DistanceToY(int y) + => DistanceTo(X, y); public override string ToString() => $"({X}, {Y})"; } diff --git a/src/ClosestPair/PointArray.cs b/src/ClosestPair/PointArray.cs deleted file mode 100644 index 5c223dd..0000000 --- a/src/ClosestPair/PointArray.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Extensions; -using MergeSort; - -namespace ClosestPair -{ - public class PointArray - { - Point[] _points; - public Point[] SortedByX; - public Point[] SortedByY; - - public PointArray(Point[] array, bool isXSorted = false, bool isYSorted = false) - { - _points = new Point[array.Length]; - SortedByX = new Point[array.Length]; - SortedByY = new Point[array.Length]; - - _points = array.Copy(); - SortedByX = (isXSorted ? array.Copy() : SortByX(array)); - SortedByY = (isYSorted ? array.Copy() : SortByY(array)); - } - - public PointArray(string file) : this(PointFile.ReadFile(file), false, false) { } - - public Point this[int i] => _points[i]; - - public static Point[] SortByX(Point[] points) - => MergeSort<Point>.Sort(points.Copy(), Point.CompareX()); - - public static Point[] SortByY(Point[] points) - => MergeSort<Point>.Sort(points.Copy(), Point.CompareY()); - } -}
\ No newline at end of file diff --git a/src/ClosestPair/PointPair.cs b/src/ClosestPair/PointPair.cs index e35d7f1..945d9c2 100644 --- a/src/ClosestPair/PointPair.cs +++ b/src/ClosestPair/PointPair.cs @@ -14,6 +14,7 @@ namespace ClosestPair public override string ToString() => this.ToString(""); + public string ToString(string format) => $"Point1: {Point1} Point2: {Point2} Distance: {Distance.ToString(format)}"; } diff --git a/src/Program/Program.cs b/src/Program/Program.cs index 423263f..f4c83d1 100644 --- a/src/Program/Program.cs +++ b/src/Program/Program.cs @@ -18,8 +18,7 @@ namespace ClosestPair Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); - - PointArray points = new PointArray(inputFile.FullName); + Point[] points = PointFile.ReadFile(inputFile.FullName); PointPair closestPair = ClosestPair.FindClosestPair(points); stopWatch.Stop(); |