@@ -3,12 +3,14 @@ import SpriteKit
33
44public extension CGVector {
55 // MARK: - Computed Property
6+
67 /// Returns the angle in radians of the vector described by the CGVector. The range of the angle is -π to π; an angle of 0 points to the right.
78 var angle : CGFloat {
89 return atan2 ( dy, dx)
910 }
10-
11+
1112 // MARK: - Initializers
13+
1214 /// Creates a new CGVector given a CGPoint.
1315 init ( point: CGPoint ) {
1416 self . init ( dx: point. x, dy: point. y)
@@ -20,57 +22,68 @@ public extension CGVector {
2022 }
2123
2224 // MARK: - Mutating Methods
25+
2326 // Adds (dx, dy) to the vector.
24- mutating func offset( dx: CGFloat , dy: CGFloat ) -> CGVector {
27+ @discardableResult
28+ mutating func offset( dx: CGFloat , dy: CGFloat ) -> Self {
2529 self . dx += dx
2630 self . dy += dy
2731 return self
2832 }
2933
3034 /// Normalizes the vector described by the CGVector to length 1.0.
31- mutating func normalize( ) -> CGVector {
35+ @discardableResult
36+ mutating func normalize( ) -> Self {
3237 self = normalized ( )
3338 return self
3439 }
3540
36- mutating func setMag( _ mag: CGFloat ) {
37- let normalizedVector = normalized ( )
38- self = CGVector ( dx: normalizedVector. dx * mag, dy: normalizedVector. dy * mag)
41+ /// Set Vector's Magnititude
42+ @discardableResult
43+ mutating func setMag( _ mag: CGFloat ) -> Self {
44+ let normal = normalized ( )
45+ self = CGVector ( dx: normal. dx * mag, dy: normal. dy * mag)
46+ return self
3947 }
40-
41- mutating func div( _ mag: CGFloat ) {
42- if mag == 0 {
43- return
48+
49+ /// Calculate average vector into N
50+ @discardableResult
51+ mutating func div( _ n: CGFloat ) -> Self {
52+ if n == 0 {
53+ return self
4454 }
45-
46- self = CGVector ( dx: self . dx/ mag, dy: dy/ mag)
47- }
48-
49- mutating func limit( _ mag: CGFloat ) {
50- let currentMag = sqrt ( dx * dx + dy * dy)
51- if currentMag > mag {
52- let ratio = mag / currentMag
55+ self = CGVector ( dx: dx / n, dy: dy / n)
56+ return self
57+ }
58+
59+ /// Limit the vector's length
60+ @discardableResult
61+ mutating func limit( _ length: CGFloat ) -> Self {
62+ let len = self . length ( )
63+ if len > length {
64+ let ratio = length / len
5365 self = CGVector ( dx: dx * ratio, dy: dy * ratio)
5466 }
67+
68+ return self
5569 }
5670
57- mutating func add( _ vector: CGVector ) {
71+ /// Add a vector to self
72+ @discardableResult
73+ mutating func add( _ vector: CGVector ) -> Self {
5874 self = CGVector ( dx: dx + vector. dx, dy: dy + vector. dy)
75+ return self
5976 }
60-
61- mutating func sub( _ vector: CGVector ) {
77+
78+ /// Subtraction a vector from self
79+ @discardableResult
80+ mutating func sub( _ vector: CGVector ) -> Self {
6281 self = CGVector ( dx: dx - vector. dx, dy: dy - vector. dy)
63- }
64-
65- static func add( _ a: CGVector , _ b: CGVector ) -> CGVector {
66- return CGVector ( dx: a. dx + b. dx, dy: a. dy + b. dy)
67- }
68-
69- static func sub( _ a: CGVector , _ b: CGVector ) -> CGVector {
70- return CGVector ( dx: a. dx - b. dx, dy: a. dy - b. dy)
82+ return self
7183 }
7284
7385 // MARK: - Normal Methods
86+
7487 /// Returns a copy of the vector.
7588 func copy( ) -> CGVector {
7689 return CGVector ( dx: dx, dy: dy)
@@ -97,10 +110,21 @@ public extension CGVector {
97110 return ( self - vector) . length ( )
98111 }
99112
100- // MARK: - Static Methods
113+ // MARK: - Static Methods, those methods can be replaced with opertator functions
114+
115+ /// Calculates the Sum of two vectors
116+ static func add( _ a: CGVector , _ b: CGVector ) -> CGVector {
117+ return CGVector ( dx: a. dx + b. dx, dy: a. dy + b. dy)
118+ }
119+
120+ /// Calculate the Subtraction of two vectors
121+ static func sub( _ a: CGVector , _ b: CGVector ) -> CGVector {
122+ return CGVector ( dx: a. dx - b. dx, dy: a. dy - b. dy)
123+ }
101124}
102125
103126// MARK: - Associated Functions
127+
104128/// Adds two CGVector values and returns the result as a new CGVector.
105129public func + ( left: CGVector , right: CGVector ) -> CGVector {
106130 return CGVector ( dx: left. dx + right. dx, dy: left. dy + right. dy)
0 commit comments