Skip to content

Commit ccf9c46

Browse files
committed
add more extensions and tools
1 parent 00d3b3b commit ccf9c46

12 files changed

+1353
-4
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2013-2014 Razeware LLC
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
import CoreGraphics
24+
25+
/** The value of π as a CGFloat */
26+
let π = CGFloat(Double.pi)
27+
28+
public extension CGFloat {
29+
/**
30+
* Converts an angle in degrees to radians.
31+
*/
32+
func degreesToRadians() -> CGFloat {
33+
return π * self / 180.0
34+
}
35+
36+
/**
37+
* Converts an angle in radians to degrees.
38+
*/
39+
func radiansToDegrees() -> CGFloat {
40+
return self * 180.0 / π
41+
}
42+
43+
/**
44+
* Ensures that the float value stays between the given values, inclusive.
45+
*/
46+
func clamped(_ v1: CGFloat, _ v2: CGFloat) -> CGFloat {
47+
let min = v1 < v2 ? v1 : v2
48+
let max = v1 > v2 ? v1 : v2
49+
return self < min ? min : (self > max ? max : self)
50+
}
51+
52+
/**
53+
* Ensures that the float value stays between the given values, inclusive.
54+
*/
55+
mutating func clamp(_ v1: CGFloat, _ v2: CGFloat) -> CGFloat {
56+
self = clamped(v1, v2)
57+
return self
58+
}
59+
60+
/**
61+
* Returns 1.0 if a floating point value is positive; -1.0 if it is negative.
62+
*/
63+
func sign() -> CGFloat {
64+
return (self >= 0.0) ? 1.0 : -1.0
65+
}
66+
67+
/**
68+
* Returns a random floating point number between 0.0 and 1.0, inclusive.
69+
*/
70+
static func random() -> CGFloat {
71+
return CGFloat.random(in: 0.0...1.0)
72+
}
73+
74+
/**
75+
* Returns a random floating point number in the range min...max, inclusive.
76+
*/
77+
static func random(min: CGFloat, max: CGFloat) -> CGFloat {
78+
assert(min < max)
79+
return CGFloat.random() * (max - min) + min
80+
}
81+
82+
/**
83+
* Randomly returns either 1.0 or -1.0.
84+
*/
85+
static func randomSign() -> CGFloat {
86+
return (arc4random_uniform(2) == 0) ? 1.0 : -1.0
87+
}
88+
}
89+
90+
/**
91+
* Returns the shortest angle between two angles. The result is always between
92+
* -π and π.
93+
*/
94+
public func shortestAngleBetween(_ angle1: CGFloat, angle2: CGFloat) -> CGFloat {
95+
let twoπ = π * 2.0
96+
var angle = (angle2 - angle1).truncatingRemainder(dividingBy: twoπ)
97+
if (angle >= π) {
98+
angle = angle - twoπ
99+
}
100+
if (angle <= -π) {
101+
angle = angle + twoπ
102+
}
103+
return angle
104+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright (c) 2013-2014 Razeware LLC
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
import CoreGraphics
24+
import SpriteKit
25+
26+
public extension CGVector {
27+
/**
28+
* Creates a new CGVector given a CGPoint.
29+
*/
30+
init(point: CGPoint) {
31+
self.init(dx: point.x, dy: point.y)
32+
}
33+
34+
/**
35+
* Given an angle in radians, creates a vector of length 1.0 and returns the
36+
* result as a new CGVector. An angle of 0 is assumed to point to the right.
37+
*/
38+
init(angle: CGFloat) {
39+
self.init(dx: cos(angle), dy: sin(angle))
40+
}
41+
42+
/**
43+
* Adds (dx, dy) to the vector.
44+
*/
45+
mutating func offset(dx: CGFloat, dy: CGFloat) -> CGVector {
46+
self.dx += dx
47+
self.dy += dy
48+
return self
49+
}
50+
51+
/**
52+
* Returns the length (magnitude) of the vector described by the CGVector.
53+
*/
54+
func length() -> CGFloat {
55+
return sqrt(dx*dx + dy*dy)
56+
}
57+
58+
/**
59+
* Returns the squared length of the vector described by the CGVector.
60+
*/
61+
func lengthSquared() -> CGFloat {
62+
return dx*dx + dy*dy
63+
}
64+
65+
/**
66+
* Normalizes the vector described by the CGVector to length 1.0 and returns
67+
* the result as a new CGVector.
68+
public */
69+
func normalized() -> CGVector {
70+
let len = length()
71+
return len>0 ? self / len : CGVector.zero
72+
}
73+
74+
/**
75+
* Normalizes the vector described by the CGVector to length 1.0.
76+
*/
77+
mutating func normalize() -> CGVector {
78+
self = normalized()
79+
return self
80+
}
81+
82+
/**
83+
* Calculates the distance between two CGVectors. Pythagoras!
84+
*/
85+
func distanceTo(_ vector: CGVector) -> CGFloat {
86+
return (self - vector).length()
87+
}
88+
89+
/**
90+
* Returns the angle in radians of the vector described by the CGVector.
91+
* The range of the angle is -π to π; an angle of 0 points to the right.
92+
*/
93+
var angle: CGFloat {
94+
return atan2(dy, dx)
95+
}
96+
}
97+
98+
/**
99+
* Adds two CGVector values and returns the result as a new CGVector.
100+
*/
101+
public func + (left: CGVector, right: CGVector) -> CGVector {
102+
return CGVector(dx: left.dx + right.dx, dy: left.dy + right.dy)
103+
}
104+
105+
/**
106+
* Increments a CGVector with the value of another.
107+
*/
108+
public func += (left: inout CGVector, right: CGVector) {
109+
left = left + right
110+
}
111+
112+
/**
113+
* Subtracts two CGVector values and returns the result as a new CGVector.
114+
*/
115+
public func - (left: CGVector, right: CGVector) -> CGVector {
116+
return CGVector(dx: left.dx - right.dx, dy: left.dy - right.dy)
117+
}
118+
119+
/**
120+
* Decrements a CGVector with the value of another.
121+
*/
122+
public func -= (left: inout CGVector, right: CGVector) {
123+
left = left - right
124+
}
125+
126+
/**
127+
* Multiplies two CGVector values and returns the result as a new CGVector.
128+
*/
129+
public func * (left: CGVector, right: CGVector) -> CGVector {
130+
return CGVector(dx: left.dx * right.dx, dy: left.dy * right.dy)
131+
}
132+
133+
/**
134+
* Multiplies a CGVector with another.
135+
*/
136+
public func *= (left: inout CGVector, right: CGVector) {
137+
left = left * right
138+
}
139+
140+
/**
141+
* Multiplies the x and y fields of a CGVector with the same scalar value and
142+
* returns the result as a new CGVector.
143+
*/
144+
public func * (vector: CGVector, scalar: CGFloat) -> CGVector {
145+
return CGVector(dx: vector.dx * scalar, dy: vector.dy * scalar)
146+
}
147+
148+
/**
149+
* Multiplies the x and y fields of a CGVector with the same scalar value.
150+
*/
151+
public func *= (vector: inout CGVector, scalar: CGFloat) {
152+
vector = vector * scalar
153+
}
154+
155+
/**
156+
* Divides two CGVector values and returns the result as a new CGVector.
157+
*/
158+
public func / (left: CGVector, right: CGVector) -> CGVector {
159+
return CGVector(dx: left.dx / right.dx, dy: left.dy / right.dy)
160+
}
161+
162+
/**
163+
* Divides a CGVector by another.
164+
*/
165+
public func /= (left: inout CGVector, right: CGVector) {
166+
left = left / right
167+
}
168+
169+
/**
170+
* Divides the dx and dy fields of a CGVector by the same scalar value and
171+
* returns the result as a new CGVector.
172+
*/
173+
public func / (vector: CGVector, scalar: CGFloat) -> CGVector {
174+
return CGVector(dx: vector.dx / scalar, dy: vector.dy / scalar)
175+
}
176+
177+
/**
178+
* Divides the dx and dy fields of a CGVector by the same scalar value.
179+
*/
180+
public func /= (vector: inout CGVector, scalar: CGFloat) {
181+
vector = vector / scalar
182+
}
183+
184+
/**
185+
* Performs a linear interpolation between two CGVector values.
186+
*/
187+
public func lerp(start: CGVector, end: CGVector, t: CGFloat) -> CGVector {
188+
return start + (end - start) * t
189+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2013-2014 Razeware LLC
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
* THE SOFTWARE.
21+
*/
22+
23+
import CoreGraphics
24+
25+
public extension Int {
26+
/**
27+
* Ensures that the integer value stays with the specified range.
28+
*/
29+
func clamped(_ range: Range<Int>) -> Int {
30+
return (self < range.lowerBound) ? range.lowerBound : ((self >= range.upperBound) ? range.upperBound - 1: self)
31+
}
32+
33+
func clamped(_ range: ClosedRange<Int>) -> Int {
34+
return (self < range.lowerBound) ? range.lowerBound : ((self > range.upperBound) ? range.upperBound: self)
35+
}
36+
37+
/**
38+
* Ensures that the integer value stays with the specified range.
39+
*/
40+
mutating func clamp(_ range: Range<Int>) -> Int {
41+
self = clamped(range)
42+
return self
43+
}
44+
45+
mutating func clamp(_ range: ClosedRange<Int>) -> Int {
46+
self = clamped(range)
47+
return self
48+
}
49+
50+
/**
51+
* Ensures that the integer value stays between the given values, inclusive.
52+
*/
53+
func clamped(_ v1: Int, _ v2: Int) -> Int {
54+
let min = v1 < v2 ? v1 : v2
55+
let max = v1 > v2 ? v1 : v2
56+
return self < min ? min : (self > max ? max : self)
57+
}
58+
59+
/**
60+
* Ensures that the integer value stays between the given values, inclusive.
61+
*/
62+
mutating func clamp(_ v1: Int, _ v2: Int) -> Int {
63+
self = clamped(v1, v2)
64+
return self
65+
}
66+
67+
/**
68+
* Returns a random integer in the specified range.
69+
*/
70+
static func random(_ range: Range<Int>) -> Int {
71+
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound - 1))) + range.lowerBound
72+
}
73+
74+
static func random(_ range: ClosedRange<Int>) -> Int {
75+
return Int(arc4random_uniform(UInt32(range.upperBound - range.lowerBound))) + range.lowerBound
76+
}
77+
78+
/**
79+
* Returns a random integer between 0 and n-1.
80+
*/
81+
static func random(_ n: Int) -> Int {
82+
return Int(arc4random_uniform(UInt32(n)))
83+
}
84+
85+
/**
86+
* Returns a random integer in the range min...max, inclusive.
87+
*/
88+
static func random(min: Int, max: Int) -> Int {
89+
assert(min < max)
90+
return Int(arc4random_uniform(UInt32(max - min + 1))) + min
91+
}
92+
}

0 commit comments

Comments
 (0)