3
3
func ClosestPairOf( points: [ Point ] ) -> ( minimum: Double , firstPoint: Point , secondPoint: Point ) {
4
4
var innerPoints = mergeSort ( points, sortAccording : true )
5
5
let result = ClosestPair ( & innerPoints, innerPoints. count)
6
- return ( result. minValue , result. firstPoint, result. secondPoint)
6
+ return ( result. minimumValue , result. firstPoint, result. secondPoint)
7
7
}
8
8
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 )
10
10
{
11
11
// Brute force if only 3 points (To end recursion)
12
12
if n <= 3
13
13
{
14
14
var i = 0 , j = i+ 1
15
- var minDist = Double . infinity
15
+ var minimumDistance = Double . infinity
16
16
var newFirst : Point ? = nil
17
17
var newSecond : Point ? = nil
18
18
while i< n
19
19
{
20
20
j = i+ 1
21
21
while j < n
22
22
{
23
- if dist ( p [ i] , p [ j] ) <= minDist
23
+ if distance ( p [ i] , p [ j] ) <= minimumDistance
24
24
{
25
- minDist = dist ( p [ i] , p [ j] )
25
+ minimumDistance = distance ( p [ i] , p [ j] )
26
26
newFirst = p [ i]
27
27
newSecond = p [ j]
28
28
}
@@ -31,7 +31,7 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
31
31
i+= 1
32
32
33
33
}
34
- return ( minDist , newFirst ?? Point ( 0 , 0 ) , newSecond ?? Point ( 0 , 0 ) )
34
+ return ( minimumDistance , newFirst ?? Point ( 0 , 0 ) , newSecond ?? Point ( 0 , 0 ) )
35
35
}
36
36
37
37
@@ -54,24 +54,24 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
54
54
55
55
// Recurse on the left and right part of the array.
56
56
let valueFromLeft = ClosestPair ( & leftSide, mid)
57
- let minLeft : Double = valueFromLeft. minValue
57
+ let minimumLeft : Double = valueFromLeft. minimumValue
58
58
let valueFromRight = ClosestPair ( & rightSide, n- mid)
59
- let minRight : Double = valueFromRight. minValue
59
+ let minimumRight : Double = valueFromRight. minimumValue
60
60
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
63
63
64
64
var first : Point
65
65
var second : Point
66
66
67
67
// Get the minimum between the left and the right.
68
- if minLeft < minRight {
69
- min = minLeft
68
+ if minimumLeft < minimumRight {
69
+ minimum = minimumLeft
70
70
first = valueFromLeft. firstPoint
71
71
second = valueFromLeft. secondPoint
72
72
}
73
73
else {
74
- min = minRight
74
+ minimum = minimumRight
75
75
first = valueFromRight. firstPoint
76
76
second = valueFromRight. secondPoint
77
77
}
@@ -82,11 +82,11 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
82
82
83
83
var strip = [ Point] ( )
84
84
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.
86
86
var i = 0 , j = 0
87
87
while i< n
88
88
{
89
- if abs ( p [ i] . x - line) < min
89
+ if abs ( p [ i] . x - line) < minimum
90
90
{
91
91
strip. append ( p [ i] )
92
92
j+= 1
@@ -97,19 +97,19 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
97
97
98
98
i= 0
99
99
var x = i+ 1
100
- var temp = min
100
+ var temp = minimum
101
101
var tempFirst : Point = Point ( 0 , 0 )
102
102
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.
104
104
while i< j
105
105
{
106
106
x = i+ 1
107
107
while x < j
108
108
{
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
111
111
{
112
- temp = dist ( strip [ i] , strip [ x] )
112
+ temp = distance ( strip [ i] , strip [ x] )
113
113
tempFirst = strip [ i]
114
114
tempSecond = strip [ x]
115
115
}
@@ -118,13 +118,13 @@ func ClosestPair(_ p : inout [Point],_ n : Int) -> (minValue: Double,firstPoint:
118
118
i+= 1
119
119
}
120
120
121
- if temp < min
121
+ if temp < minimum
122
122
{
123
- min = temp;
123
+ minimum = temp;
124
124
first = tempFirst
125
125
second = tempSecond
126
126
}
127
- return ( min , first, second)
127
+ return ( minimum , first, second)
128
128
}
129
129
130
130
@@ -201,7 +201,7 @@ struct Point
201
201
}
202
202
}
203
203
// Get the distance between two points a, b.
204
- func dist ( _ a: Point , _ b: Point ) -> Double
204
+ func distance ( _ a: Point , _ b: Point ) -> Double
205
205
{
206
206
let equation : Double = ( ( ( a. x- b. x) * ( a. x- b. x) ) ) + ( ( ( a. y- b. y) * ( a. y- b. y) ) )
207
207
return equation. squareRoot ( )
0 commit comments