Skip to content

Commit dc89024

Browse files
authored
Merge pull request #3 from JulioBBL/closest-pair-impprovements
improves readability
2 parents 885a606 + 83a1b66 commit dc89024

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

Closest Pair/ClosestPair.playground/Contents.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
func ClosestPairOf(points: [Point]) -> (minimum:Double, firstPoint:Point, secondPoint:Point) {
44
var innerPoints = mergeSort(points, sortAccording : true)
55
let result = ClosestPair(&innerPoints, innerPoints.count)
6-
return (result.minValue, result.firstPoint, result.secondPoint)
6+
return (result.minimumValue, result.firstPoint, result.secondPoint)
77
}
88

9-
func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint: Point,secondPoint: Point)
9+
func ClosestPair(_ p : inout [Point],_ n : Int) -> (minimumValue: Double,firstPoint: Point,secondPoint: Point)
1010
{
1111
// Brute force if only 3 points (To end recursion)
1212
if n <= 3
1313
{
1414
var i=0, j = i+1
15-
var minDist = Double.infinity
15+
var minimumDistance = Double.infinity
1616
var newFirst:Point? = nil
1717
var newSecond:Point? = nil
1818
while i<n
1919
{
2020
j = i+1
2121
while j < n
2222
{
23-
if dist(p[i], p[j]) <= minDist
23+
if distance(p[i], p[j]) <= minimumDistance
2424
{
25-
minDist = dist(p[i], p[j])
25+
minimumDistance = distance(p[i], p[j])
2626
newFirst = p[i]
2727
newSecond = p[j]
2828
}
@@ -31,7 +31,7 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
3131
i+=1
3232

3333
}
34-
return (minDist, newFirst ?? Point(0,0), newSecond ?? Point(0,0))
34+
return (minimumDistance, newFirst ?? Point(0,0), newSecond ?? Point(0,0))
3535
}
3636

3737

@@ -54,24 +54,24 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
5454

5555
// Recurse on the left and right part of the array.
5656
let valueFromLeft = ClosestPair(&leftSide, mid)
57-
let minLeft:Double = valueFromLeft.minValue
57+
let minimumLeft:Double = valueFromLeft.minimumValue
5858
let valueFromRight = ClosestPair(&rightSide, n-mid)
59-
let minRight:Double = valueFromRight.minValue
59+
let minimumRight:Double = valueFromRight.minimumValue
6060

61-
// Starting current min must be the largest possible to not affect the real calculations.
62-
var min = Double.infinity
61+
// Starting current minimum must be the largest possible to not affect the real calculations.
62+
var minimum = Double.infinity
6363

6464
var first:Point
6565
var second:Point
6666

6767
// Get the minimum between the left and the right.
68-
if minLeft < minRight {
69-
min = minLeft
68+
if minimumLeft < minimumRight {
69+
minimum = minimumLeft
7070
first = valueFromLeft.firstPoint
7171
second = valueFromLeft.secondPoint
7272
}
7373
else {
74-
min = minRight
74+
minimum = minimumRight
7575
first = valueFromRight.firstPoint
7676
second = valueFromRight.secondPoint
7777
}
@@ -82,11 +82,11 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
8282

8383
var strip = [Point]()
8484

85-
// If the value is less than the min distance away in X from the line then take it into consideration.
85+
// If the value is less than the minimum distance away in X from the line then take it into consideration.
8686
var i=0, j = 0
8787
while i<n
8888
{
89-
if abs(p[i].x - line) < min
89+
if abs(p[i].x - line) < minimum
9090
{
9191
strip.append(p[i])
9292
j+=1
@@ -97,19 +97,19 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
9797

9898
i=0
9999
var x = i+1
100-
var temp = min
100+
var temp = minimum
101101
var tempFirst:Point = Point(0,0)
102102
var tempSecond:Point = Point(0,0)
103-
// Get the values between the points in the strip but only if it is less min dist in Y.
103+
// Get the values between the points in the strip but only if it is less minimum distance in Y.
104104
while i<j
105105
{
106106
x = i+1
107107
while x < j
108108
{
109-
if (abs(strip[x].y - strip[i].y)) > min { break }
110-
if dist(strip[i], strip[x]) < temp
109+
if (abs(strip[x].y - strip[i].y)) > minimum { break }
110+
if distance(strip[i], strip[x]) < temp
111111
{
112-
temp = dist(strip[i], strip[x])
112+
temp = distance(strip[i], strip[x])
113113
tempFirst = strip[i]
114114
tempSecond = strip[x]
115115
}
@@ -118,13 +118,13 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
118118
i+=1
119119
}
120120

121-
if temp < min
121+
if temp < minimum
122122
{
123-
min = temp;
123+
minimum = temp;
124124
first = tempFirst
125125
second = tempSecond
126126
}
127-
return (min, first, second)
127+
return (minimum, first, second)
128128
}
129129

130130

@@ -201,7 +201,7 @@ struct Point
201201
}
202202
}
203203
// Get the distance between two points a, b.
204-
func dist(_ a: Point,_ b: Point) -> Double
204+
func distance(_ a: Point,_ b: Point) -> Double
205205
{
206206
let equation:Double = (((a.x-b.x)*(a.x-b.x))) + (((a.y-b.y)*(a.y-b.y)))
207207
return equation.squareRoot()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
2+
<playground version='5.0' target-platform='macos'>
33
<timeline fileName='timeline.xctimeline'/>
44
</playground>

0 commit comments

Comments
 (0)