import Foundation #if USE_SIMD import simd #endif #if USE_SIMD public typealias Vector2 = SIMD2 #else public struct Vector2: Equatable { public typealias Scalar = T public var x, y: T public init() { self.x = 0; self.y = 0 } public init(_ x: T, _ y: T) { self.x = x; self.y = y } } public extension Vector2 { // constants @inline(__always) static var zero: Self { Self(0, 0) } @inline(__always) static var one: Self { Self(1, 1) } subscript(index: Int) -> T { get { switch index { case 0: x case 1: y default: fatalError() } } } // relational @inline(__always) static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y } @inline(__always) static func != (lhs: Self, rhs: Self) -> Bool { rhs.x != rhs.x || lhs.y != rhs.y } // arithmetic @inline(__always) static func + (lhs: Self, rhs: Self) -> Self { Self(lhs.x + rhs.x, lhs.y + rhs.y) } @inline(__always) static func - (lhs: Self, rhs: Self) -> Self { Self(lhs.x - rhs.x, lhs.y - rhs.y) } @inline(__always) static func * (lhs: Self, rhs: Self) -> Self { Self(lhs.x * rhs.x, lhs.y * rhs.y) } @inline(__always) static func / (lhs: Self, rhs: Self) -> Self { Self(lhs.x / rhs.x, lhs.y / rhs.y) } // scalar arithmetic @inline(__always) static func * (lhs: Self, rhs: T) -> Self { Self(lhs.x * rhs, lhs.y * rhs) } @inline(__always) static func / (lhs: Self, rhs: T) -> Self { Self(lhs.x / rhs, lhs.y / rhs) } @inline(__always) static func * (lhs: T, rhs: Self) -> Self { Self(lhs * rhs.x, lhs * rhs.y) } @inline(__always) static func / (lhs: T, rhs: Self) -> Self { Self(lhs / rhs.x, lhs / rhs.y) } // compound arithmetic @inline(__always) static func += (lhs: inout Self, rhs: Self) { lhs.x += rhs.x; lhs.y += rhs.y } @inline(__always) static func -= (lhs: inout Self, rhs: Self) { lhs.x -= rhs.x; lhs.y -= rhs.y } @inline(__always) static func *= (lhs: inout Self, rhs: Self) { lhs.x *= rhs.x; lhs.y *= rhs.y } @inline(__always) static func /= (lhs: inout Self, rhs: Self) { lhs.x /= rhs.x; lhs.y /= rhs.y } // compound scalar arithmetic @inline(__always) static func *= (lhs: inout Self, rhs: T) { lhs.x *= rhs; lhs.y *= rhs } @inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.x /= rhs; lhs.y /= rhs } // unary @inline(__always) static prefix func + (vec: Self) -> Self { vec } @inline(__always) static prefix func - (vec: Self) -> Self { Self(-vec.x, -vec.y) } } #endif public extension Vector2 where Scalar: FloatingPoint { @inline(__always) var len2: Scalar { x * x + y * y } @inline(__always) var len: Scalar { len2.squareRoot() } @inline(__always) var normalised: Self { let invLen = 1 / len; return self * invLen } @inline(__always) mutating func normalise() { let invLen = 1 / len; self *= invLen } @inline(__always) func dot(_ b: Self) -> Scalar { x * b.x + y * b.y } @inline(__always) func reflect(_ n: Self) -> Self { self - (n * 2 * self.dot(n)) } @inline(__always) func project(_ n: Self) -> Self { n * self.dot(n) } @inline(__always) func cross(_ b: Self) -> Scalar { x * b.y - y * b.x } @inline(__always) func lerp(_ b: Self, _ x: Scalar) -> Self { let invX = 1 - x return Self(self.x * invX + b.x * x, self.y * invX + b.y * x) } @inline(__always) func distance(_ b: Self) -> Scalar { return (b - self).len } } public typealias Vec2f = Vector2 public typealias Vec2d = Vector2