77import simd
88
99@frozen
10- public struct Vector2f {
10+ public struct Vector2f : Codable {
1111 internal var d : SIMD2 < Float >
1212
1313 public var x : Float { get { return d. x } set { d. x = newValue } }
@@ -46,6 +46,10 @@ public struct Vector2f {
4646 public init ( _ x: Float , _ y: Float ) {
4747 self . d = SIMD2 < Float > ( x, y)
4848 }
49+
50+ public init ( _ simdV: SIMD2 < Float > ) {
51+ self . d = simdV
52+ }
4953}
5054
5155public extension Vector2f {
@@ -78,6 +82,14 @@ public extension Vector2f {
7882
7983 //MARK: - functions
8084
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+
8193 func dot( _ x: Vector2f ) -> Float {
8294 return simd. dot ( d, x. d)
8395 }
@@ -90,8 +102,24 @@ public extension Vector2f {
90102 return unsafeBitCast ( simd. mix ( d, to. d, t: factor) , to: Vector2f . self)
91103 }
92104
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+
93117 //MARK: - operators
94118
119+ static func + ( lhs: Vector2f , rhs: Float ) -> Vector2f {
120+ return unsafeBitCast ( lhs. d + rhs, to: Vector2f . self)
121+ }
122+
95123 static func + ( lhs: Vector2f , rhs: Vector2f ) -> Vector2f {
96124 return unsafeBitCast ( lhs. d + rhs. d, to: Vector2f . self)
97125 }
@@ -112,6 +140,18 @@ public extension Vector2f {
112140 return unsafeBitCast ( lhs. d * rhs. d, to: Vector2f . self)
113141 }
114142
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+
115155 static func * ( lhs: Matrix4x4f , rhs: Vector2f ) -> Vector2f {
116156 let res = lhs. d * Vector4f( rhs) . d
117157 return Vector2f ( res. x, res. y)
@@ -125,6 +165,18 @@ public extension Vector2f {
125165 static func *= ( lhs: inout Vector2f , rhs: Float ) {
126166 lhs. d *= rhs
127167 }
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+ }
128180}
129181
130182extension Vector2f : Equatable {
0 commit comments