7
7
import simd
8
8
9
9
@frozen
10
- public struct Vector2f {
10
+ public struct Vector2f : Codable {
11
11
internal var d : SIMD2 < Float >
12
12
13
13
public var x : Float { get { return d. x } set { d. x = newValue } }
@@ -46,6 +46,10 @@ public struct Vector2f {
46
46
public init ( _ x: Float , _ y: Float ) {
47
47
self . d = SIMD2 < Float > ( x, y)
48
48
}
49
+
50
+ public init ( _ simdV: SIMD2 < Float > ) {
51
+ self . d = simdV
52
+ }
49
53
}
50
54
51
55
public extension Vector2f {
@@ -78,6 +82,14 @@ public extension Vector2f {
78
82
79
83
//MARK: - functions
80
84
85
+ func distance( to other: Vector2f ) -> Float {
86
+ return ( self - other) . length
87
+ }
88
+
89
+ func angle( with other: Vector2f ) -> Float {
90
+ return atan2f ( other. y - self . y, other. x - self . x)
91
+ }
92
+
81
93
func dot( _ x: Vector2f ) -> Float {
82
94
return simd. dot ( d, x. d)
83
95
}
@@ -90,8 +102,24 @@ public extension Vector2f {
90
102
return unsafeBitCast ( simd. mix ( d, to. d, t: factor) , to: Vector2f . self)
91
103
}
92
104
105
+ func min( ) -> Float {
106
+ return self . d. min ( )
107
+ }
108
+
109
+ func max( ) -> Float {
110
+ return self . d. max ( )
111
+ }
112
+
113
+ mutating func round( _ rule: FloatingPointRoundingRule ) {
114
+ self . d. round ( rule)
115
+ }
116
+
93
117
//MARK: - operators
94
118
119
+ static func + ( lhs: Vector2f , rhs: Float ) -> Vector2f {
120
+ return unsafeBitCast ( lhs. d + rhs, to: Vector2f . self)
121
+ }
122
+
95
123
static func + ( lhs: Vector2f , rhs: Vector2f ) -> Vector2f {
96
124
return unsafeBitCast ( lhs. d + rhs. d, to: Vector2f . self)
97
125
}
@@ -112,6 +140,18 @@ public extension Vector2f {
112
140
return unsafeBitCast ( lhs. d * rhs. d, to: Vector2f . self)
113
141
}
114
142
143
+ static func / ( lhs: Vector2f , rhs: Float ) -> Vector2f {
144
+ return unsafeBitCast ( lhs. d / rhs, to: Vector2f . self)
145
+ }
146
+
147
+ static func / ( lhs: Float , rhs: Vector2f ) -> Vector2f {
148
+ return unsafeBitCast ( lhs / rhs. d, to: Vector2f . self)
149
+ }
150
+
151
+ static func / ( lhs: Vector2f , rhs: Vector2f ) -> Vector2f {
152
+ return unsafeBitCast ( lhs. d / rhs. d, to: Vector2f . self)
153
+ }
154
+
115
155
static func * ( lhs: Matrix4x4f , rhs: Vector2f ) -> Vector2f {
116
156
let res = lhs. d * Vector4f( rhs) . d
117
157
return Vector2f ( res. x, res. y)
@@ -125,6 +165,18 @@ public extension Vector2f {
125
165
static func *= ( lhs: inout Vector2f , rhs: Float ) {
126
166
lhs. d *= rhs
127
167
}
168
+
169
+ static func /= ( lhs: inout Vector2f , rhs: Float ) {
170
+ lhs. d /= rhs
171
+ }
172
+
173
+ static func *= ( lhs: inout Vector2f , rhs: Vector2f ) {
174
+ lhs. d *= rhs. d
175
+ }
176
+
177
+ static func /= ( lhs: inout Vector2f , rhs: Vector2f ) {
178
+ lhs. d /= rhs. d
179
+ }
128
180
}
129
181
130
182
extension Vector2f : Equatable {
0 commit comments