/
CGPoint+Extensions.swift
245 lines (214 loc) · 6.4 KB
/
CGPoint+Extensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
* Copyright (c) 2013-2014 Razeware LLC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import CoreGraphics
import SpriteKit
public extension CGPoint {
/**
* Creates a new CGPoint given a CGVector.
*/
public init(vector: CGVector) {
self.init(x: vector.dx, y: vector.dy)
}
/**
* Given an angle in radians, creates a vector of length 1.0 and returns the
* result as a new CGPoint. An angle of 0 is assumed to point to the right.
*/
public init(angle: CGFloat) {
self.init(x: cos(angle), y: sin(angle))
}
/**
* Adds (dx, dy) to the point.
*/
public mutating func offset(dx: CGFloat, dy: CGFloat) -> CGPoint {
x += dx
y += dy
return self
}
/**
* Returns the length (magnitude) of the vector described by the CGPoint.
*/
public func length() -> CGFloat {
return sqrt(x*x + y*y)
}
/**
* Returns the squared length of the vector described by the CGPoint.
*/
public func lengthSquared() -> CGFloat {
return x*x + y*y
}
/**
* Normalizes the vector described by the CGPoint to length 1.0 and returns
* the result as a new CGPoint.
*/
func normalized() -> CGPoint {
let len = length()
return len>0 ? self / len : CGPoint.zero
}
/**
* Normalizes the vector described by the CGPoint to length 1.0.
*/
public mutating func normalize() -> CGPoint {
self = normalized()
return self
}
/**
* Calculates the distance between two CGPoints. Pythagoras!
*/
public func distanceTo(_ point: CGPoint) -> CGFloat {
return (self - point).length()
}
/**
* Returns the angle in radians of the vector described by the CGPoint.
* The range of the angle is -π to π; an angle of 0 points to the right.
*/
public var angle: CGFloat {
return atan2(y, x)
}
}
/**
* Adds two CGPoint values and returns the result as a new CGPoint.
*/
public func + (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x + right.x, y: left.y + right.y)
}
/**
* Increments a CGPoint with the value of another.
*/
public func += (left: inout CGPoint, right: CGPoint) {
left = left + right
}
/**
* Adds a CGVector to this CGPoint and returns the result as a new CGPoint.
*/
public func + (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x + right.dx, y: left.y + right.dy)
}
/**
* Increments a CGPoint with the value of a CGVector.
*/
public func += (left: inout CGPoint, right: CGVector) {
left = left + right
}
/**
* Subtracts two CGPoint values and returns the result as a new CGPoint.
*/
public func - (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x - right.x, y: left.y - right.y)
}
/**
* Decrements a CGPoint with the value of another.
*/
public func -= (left: inout CGPoint, right: CGPoint) {
left = left - right
}
/**
* Subtracts a CGVector from a CGPoint and returns the result as a new CGPoint.
*/
public func - (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x - right.dx, y: left.y - right.dy)
}
/**
* Decrements a CGPoint with the value of a CGVector.
*/
public func -= (left: inout CGPoint, right: CGVector) {
left = left - right
}
/**
* Multiplies two CGPoint values and returns the result as a new CGPoint.
*/
public func * (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x * right.x, y: left.y * right.y)
}
/**
* Multiplies a CGPoint with another.
*/
public func *= (left: inout CGPoint, right: CGPoint) {
left = left * right
}
/**
* Multiplies the x and y fields of a CGPoint with the same scalar value and
* returns the result as a new CGPoint.
*/
public func * (point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x * scalar, y: point.y * scalar)
}
/**
* Multiplies the x and y fields of a CGPoint with the same scalar value.
*/
public func *= (point: inout CGPoint, scalar: CGFloat) {
point = point * scalar
}
/**
* Multiplies a CGPoint with a CGVector and returns the result as a new CGPoint.
*/
public func * (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x * right.dx, y: left.y * right.dy)
}
/**
* Multiplies a CGPoint with a CGVector.
*/
public func *= (left: inout CGPoint, right: CGVector) {
left = left * right
}
/**
* Divides two CGPoint values and returns the result as a new CGPoint.
*/
public func / (left: CGPoint, right: CGPoint) -> CGPoint {
return CGPoint(x: left.x / right.x, y: left.y / right.y)
}
/**
* Divides a CGPoint by another.
*/
public func /= (left: inout CGPoint, right: CGPoint) {
left = left / right
}
/**
* Divides the x and y fields of a CGPoint by the same scalar value and returns
* the result as a new CGPoint.
*/
public func / (point: CGPoint, scalar: CGFloat) -> CGPoint {
return CGPoint(x: point.x / scalar, y: point.y / scalar)
}
/**
* Divides the x and y fields of a CGPoint by the same scalar value.
*/
public func /= (point: inout CGPoint, scalar: CGFloat) {
point = point / scalar
}
/**
* Divides a CGPoint by a CGVector and returns the result as a new CGPoint.
*/
public func / (left: CGPoint, right: CGVector) -> CGPoint {
return CGPoint(x: left.x / right.dx, y: left.y / right.dy)
}
/**
* Divides a CGPoint by a CGVector.
*/
public func /= (left: inout CGPoint, right: CGVector) {
left = left / right
}
/**
* Performs a linear interpolation between two CGPoint values.
*/
public func lerp(start: CGPoint, end: CGPoint, t: CGFloat) -> CGPoint {
return start + (end - start) * t
}