-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.swift
113 lines (89 loc) · 2.85 KB
/
Vector.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
import Foundation
public protocol Vector: Equatable, CustomStringConvertible, CustomDebugStringConvertible {
associatedtype Scalar
static func +(lhs: Self, rhs: Self) -> Self
static func -(lhs: Self, rhs: Self) -> Self
static func *(lhs: Self, rhs: Scalar) -> Self
static func *(lhs: Scalar, rhs: Self) -> Self
static func /(lhs: Self, rhs: Scalar) -> Self
static prefix func +(value: Self) -> Self
static prefix func -(value: Self) -> Self
static func +=(lhs: inout Self, rhs: Self)
static func -=(lhs: inout Self, rhs: Self)
static func *=(lhs: inout Self, rhs: Scalar)
static func /=(lhs: inout Self, rhs: Scalar)
var squareLength: Scalar { get }
var length: Scalar { get }
func squareDistance(from: Self) -> Scalar
func distance(from: Self) -> Scalar
var unit: Self { get }
func dotProduct(with: Self) -> Scalar
func cos(from: Self) -> Scalar
func angle(from: Self) -> Scalar
static var zero: Self { get }
}
extension Vector where Scalar : FloatingPoint {
public static func *(lhs: Scalar, rhs: Self) -> Self {
return rhs * lhs
}
public static prefix func +(value: Self) -> Self {
return value
}
public static prefix func -(value: Self) -> Self {
return value * -1
}
public static func +=(lhs: inout Self, rhs: Self) {
lhs = lhs + rhs
}
public static func -=(lhs: inout Self, rhs: Self) {
lhs = lhs - rhs
}
public static func *=(lhs: inout Self, rhs: Scalar) {
lhs = lhs * rhs
}
public static func /=(lhs: inout Self, rhs: Scalar) {
lhs = lhs / rhs
}
public var squareLength: Scalar {
return dotProduct(with: self)
}
public var length: Scalar {
return sqrt(squareLength)
}
public func squareDistance(from vector: Self) -> Scalar {
return (self - vector).squareLength
}
public func distance(from vector: Self) -> Scalar {
return sqrt(squareDistance(from: vector))
}
public var unit: Self {
return self / length
}
public func cos(from v: Self) -> Scalar {
return dotProduct(with: v) / sqrt(squareLength * v.squareLength)
}
}
extension Vector where Scalar == Float {
public func angle(from v: Self) -> Scalar {
let cosValue = cos(from: v)
if cosValue < -1.0 {
return .pi
} else if cosValue > 1.0 {
return 0
} else {
return acos(cosValue)
}
}
}
extension Vector where Scalar == Double {
public func angle(from v: Self) -> Scalar {
let cosValue = cos(from: v)
if cosValue < -1.0 {
return .pi
} else if cosValue > 1.0 {
return 0
} else {
return acos(cosValue)
}
}
}