From 4fc945a47d0cd71ae30181bd5db8a8a2da81ee4a Mon Sep 17 00:00:00 2001 From: Andrey Volodin Date: Fri, 13 Nov 2020 13:51:16 +0300 Subject: [PATCH] add few additions to Vec2 --- Sources/Vector2+simd.swift | 54 +++++++++++++++++++++++++++++++++++++- Sources/Vector4.swift | 4 +++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Sources/Vector2+simd.swift b/Sources/Vector2+simd.swift index 95c7750..d6a11f3 100644 --- a/Sources/Vector2+simd.swift +++ b/Sources/Vector2+simd.swift @@ -7,7 +7,7 @@ import simd @frozen -public struct Vector2f { +public struct Vector2f: Codable { internal var d: SIMD2 public var x: Float { get { return d.x } set { d.x = newValue } } @@ -46,6 +46,10 @@ public struct Vector2f { public init(_ x: Float, _ y: Float) { self.d = SIMD2(x, y) } + + public init(_ simdV: SIMD2) { + self.d = simdV + } } public extension Vector2f { @@ -78,6 +82,14 @@ public extension Vector2f { //MARK: - functions + func distance(to other: Vector2f) -> Float { + return (self - other).length + } + + func angle(with other: Vector2f) -> Float { + return atan2f(other.y - self.y, other.x - self.x) + } + func dot(_ x: Vector2f) -> Float { return simd.dot(d, x.d) } @@ -90,8 +102,24 @@ public extension Vector2f { return unsafeBitCast(simd.mix(d, to.d, t: factor), to: Vector2f.self) } + func min() -> Float { + return self.d.min() + } + + func max() -> Float { + return self.d.max() + } + + mutating func round(_ rule: FloatingPointRoundingRule) { + self.d.round(rule) + } + //MARK: - operators + static func +(lhs: Vector2f, rhs: Float) -> Vector2f { + return unsafeBitCast(lhs.d + rhs, to: Vector2f.self) + } + static func +(lhs: Vector2f, rhs: Vector2f) -> Vector2f { return unsafeBitCast(lhs.d + rhs.d, to: Vector2f.self) } @@ -112,6 +140,18 @@ public extension Vector2f { return unsafeBitCast(lhs.d * rhs.d, to: Vector2f.self) } + static func /(lhs: Vector2f, rhs: Float) -> Vector2f { + return unsafeBitCast(lhs.d / rhs, to: Vector2f.self) + } + + static func /(lhs: Float, rhs: Vector2f) -> Vector2f { + return unsafeBitCast(lhs / rhs.d, to: Vector2f.self) + } + + static func /(lhs: Vector2f, rhs: Vector2f) -> Vector2f { + return unsafeBitCast(lhs.d / rhs.d, to: Vector2f.self) + } + static func *(lhs: Matrix4x4f, rhs: Vector2f) -> Vector2f { let res = lhs.d * Vector4f(rhs).d return Vector2f(res.x, res.y) @@ -125,6 +165,18 @@ public extension Vector2f { static func *=(lhs: inout Vector2f, rhs: Float) { lhs.d *= rhs } + + static func /=(lhs: inout Vector2f, rhs: Float) { + lhs.d /= rhs + } + + static func *=(lhs: inout Vector2f, rhs: Vector2f) { + lhs.d *= rhs.d + } + + static func /=(lhs: inout Vector2f, rhs: Vector2f) { + lhs.d /= rhs.d + } } extension Vector2f: Equatable { diff --git a/Sources/Vector4.swift b/Sources/Vector4.swift index af716fd..c1ab010 100644 --- a/Sources/Vector4.swift +++ b/Sources/Vector4.swift @@ -7,6 +7,10 @@ public typealias vec4 = Vector4f public extension Vector4f { //MARK: - initializers + init(_ v1: Vector2f, _ v2: Vector2f) { + self.init(v1.x, v1.y, v2.x, v2.y) + } + init(_ v: Vector2f, _ z: Float = 0.0, _ w: Float = 1.0) { self.init(v.x, v.y, z, w) }