|
| 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 | +} |
0 commit comments