|
| 1 | +/* |
| 2 | + * Copyright © 2025 Dustin Collins (Strega's Gate) |
| 3 | + * All Rights Reserved. |
| 4 | + * |
| 5 | + * http://stregasgate.com |
| 6 | + */ |
| 7 | + |
| 8 | +public protocol Vector2n<Scalar> { |
| 9 | + typealias ScalarType = Numeric & SIMDScalar |
| 10 | + associatedtype Scalar: ScalarType |
| 11 | + associatedtype Vector2Counterpart: GameMath.Vector2 |
| 12 | + |
| 13 | + var x: Scalar {get set} |
| 14 | + var y: Scalar {get set} |
| 15 | + init(x: Scalar, y: Scalar) |
| 16 | +} |
| 17 | + |
| 18 | +public typealias Position2i = Position2n<Int32> |
| 19 | +public typealias Position2f = Position2n<Float32> |
| 20 | +public struct Position2n<Scalar: Vector2n.ScalarType>: Vector2n { |
| 21 | + public typealias Vector2Counterpart = Position2 |
| 22 | + public var x: Scalar |
| 23 | + public var y: Scalar |
| 24 | + |
| 25 | + public init(x: Scalar, y: Scalar) { |
| 26 | + self.x = x |
| 27 | + self.y = y |
| 28 | + } |
| 29 | +} |
| 30 | +extension Position2n: AdditiveArithmetic where Scalar: AdditiveArithmetic { } |
| 31 | +extension Position2n: Equatable where Scalar: Equatable { } |
| 32 | +extension Position2n: Hashable where Scalar: Hashable { } |
| 33 | +extension Position2n: Comparable where Scalar: Comparable { } |
| 34 | +extension Position2n: Sendable where Scalar: Sendable { } |
| 35 | +extension Position2n: Codable where Scalar: Codable { } |
| 36 | +extension Position2n: BinaryCodable { } |
| 37 | + |
| 38 | +public typealias Size2i = Size2n<Int32> |
| 39 | +public typealias Size2f = Size2n<Float32> |
| 40 | +public struct Size2n<Scalar: Vector2n.ScalarType>: Vector2n { |
| 41 | + public typealias Vector2Counterpart = Size2 |
| 42 | + public var x: Scalar |
| 43 | + public var y: Scalar |
| 44 | + |
| 45 | + public init(x: Scalar, y: Scalar) { |
| 46 | + self.x = x |
| 47 | + self.y = y |
| 48 | + } |
| 49 | + |
| 50 | + public static var one: Self { .init(x: 1, y: 1) } |
| 51 | +} |
| 52 | +extension Size2n: AdditiveArithmetic where Scalar: AdditiveArithmetic { } |
| 53 | +extension Size2n: Equatable where Scalar: Equatable { } |
| 54 | +extension Size2n: Hashable where Scalar: Hashable { } |
| 55 | +extension Size2n: Comparable where Scalar: Comparable { } |
| 56 | +extension Size2n: Sendable where Scalar: Sendable { } |
| 57 | +extension Size2n: Codable where Scalar: Codable { } |
| 58 | +extension Size2n: BinaryCodable { } |
| 59 | +public extension Size2n { |
| 60 | + @inlinable var width: Scalar { get{self.x} set{self.x = newValue} } |
| 61 | + @inlinable var height: Scalar { get{self.y} set{self.y = newValue} } |
| 62 | + |
| 63 | + @inlinable init(width: Scalar, height: Scalar) { |
| 64 | + self.init(x: width, y: height) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public extension Vector2n where Scalar: BinaryInteger { |
| 69 | + @inlinable |
| 70 | + @_disfavoredOverload |
| 71 | + static func castInit<T1: BinaryInteger, T2: BinaryInteger>(width: T1, height: T2) -> Self { |
| 72 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 73 | + } |
| 74 | + |
| 75 | + @inlinable |
| 76 | + @_disfavoredOverload |
| 77 | + static func castInit<T1: BinaryFloatingPoint, T2: BinaryFloatingPoint>(width: T1, height: T2) -> Self { |
| 78 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 79 | + } |
| 80 | + |
| 81 | + @inlinable |
| 82 | + @_disfavoredOverload |
| 83 | + static func castInit<T1: BinaryInteger, T2: BinaryFloatingPoint>(width: T1, height: T2) -> Self { |
| 84 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 85 | + } |
| 86 | + |
| 87 | + @inlinable |
| 88 | + @_disfavoredOverload |
| 89 | + static func castInit<T1: BinaryFloatingPoint, T2: BinaryInteger>(width: T1, height: T2) -> Self { |
| 90 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +public extension Vector2n where Scalar: BinaryFloatingPoint { |
| 95 | + @inlinable |
| 96 | + @_disfavoredOverload |
| 97 | + static func castInit<T1: BinaryInteger, T2: BinaryInteger>(width: T1, height: T2) -> Self { |
| 98 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 99 | + } |
| 100 | + |
| 101 | + @inlinable |
| 102 | + @_disfavoredOverload |
| 103 | + static func castInit<T1: BinaryFloatingPoint, T2: BinaryFloatingPoint>(width: T1, height: T2) -> Self { |
| 104 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 105 | + } |
| 106 | + |
| 107 | + @inlinable |
| 108 | + @_disfavoredOverload |
| 109 | + static func castInit<T1: BinaryInteger, T2: BinaryFloatingPoint>(width: T1, height: T2) -> Self { |
| 110 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 111 | + } |
| 112 | + |
| 113 | + @inlinable |
| 114 | + @_disfavoredOverload |
| 115 | + static func castInit<T1: BinaryFloatingPoint, T2: BinaryInteger>(width: T1, height: T2) -> Self { |
| 116 | + return .init(x: Scalar(width), y: Scalar(height)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +extension Vector2n where Scalar: BinaryInteger { |
| 121 | + @inlinable |
| 122 | + public init(_ vector2n: some Vector2n<Scalar>) { |
| 123 | + self.init( |
| 124 | + x: vector2n.x, |
| 125 | + y: vector2n.y |
| 126 | + ) |
| 127 | + } |
| 128 | + |
| 129 | + @inlinable |
| 130 | + public init(_ vector2: Vector2Counterpart) { |
| 131 | + self.init( |
| 132 | + x: Scalar(vector2.x), |
| 133 | + y: Scalar(vector2.y) |
| 134 | + ) |
| 135 | + } |
| 136 | + |
| 137 | + @inlinable |
| 138 | + public init(_ vector2: Vector2Counterpart, roundingRule: FloatingPointRoundingRule) { |
| 139 | + self.init( |
| 140 | + x: Scalar(vector2.x.rounded(roundingRule)), |
| 141 | + y: Scalar(vector2.y.rounded(roundingRule)) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + @inlinable |
| 146 | + public var vector2: Vector2Counterpart { |
| 147 | + return Vector2Counterpart(Float(x), Float(y)) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +extension Vector2n where Scalar: BinaryFloatingPoint { |
| 152 | + @inlinable |
| 153 | + public init(_ vector2n: some Vector2n<Scalar>) { |
| 154 | + self.init( |
| 155 | + x: vector2n.x, |
| 156 | + y: vector2n.y |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + @inlinable |
| 161 | + public init(_ vector2: Vector2Counterpart) { |
| 162 | + self.init( |
| 163 | + x: Scalar(vector2.x), |
| 164 | + y: Scalar(vector2.y) |
| 165 | + ) |
| 166 | + } |
| 167 | + |
| 168 | + @inlinable |
| 169 | + public init(_ vector2: Vector2Counterpart, roundingRule: FloatingPointRoundingRule) { |
| 170 | + self.init( |
| 171 | + x: Scalar(vector2.x.rounded(roundingRule)), |
| 172 | + y: Scalar(vector2.y.rounded(roundingRule)), |
| 173 | + ) |
| 174 | + } |
| 175 | + |
| 176 | + @inlinable |
| 177 | + public var vector2: Vector2Counterpart { |
| 178 | + return Vector2Counterpart(Float(x), Float(y)) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +public extension Vector2n where Scalar: AdditiveArithmetic { |
| 183 | + @inlinable |
| 184 | + static func - (lhs: Self, rhs: some Vector2n<Scalar>) -> Self { |
| 185 | + return Self(x: lhs.x - rhs.x, y: lhs.y - rhs.y) |
| 186 | + } |
| 187 | + |
| 188 | + @inlinable |
| 189 | + static func + (lhs: Self, rhs: some Vector2n<Scalar>) -> Self { |
| 190 | + return Self(x: lhs.x + rhs.x, y: lhs.y + rhs.y) |
| 191 | + } |
| 192 | + |
| 193 | + @inlinable |
| 194 | + static var zero: Self {Self(x: .zero, y: .zero)} |
| 195 | +} |
| 196 | + |
| 197 | +public extension Vector2n where Scalar: Numeric { |
| 198 | + @inlinable |
| 199 | + static func * (lhs: Self, rhs: some Vector2n<Scalar>) -> Self { |
| 200 | + return Self(x: lhs.x * rhs.x, y: lhs.y * rhs.y) |
| 201 | + } |
| 202 | + |
| 203 | + @inlinable |
| 204 | + static func *= (lhs: inout Self, rhs: some Vector2n<Scalar>) { |
| 205 | + lhs = lhs * rhs |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +extension Vector2n where Scalar: Comparable { |
| 210 | + public static func < (lhs: Self, rhs: some Vector2n<Scalar>) -> Bool { |
| 211 | + return lhs.x < rhs.x && lhs.y < rhs.y |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +extension Vector2n where Scalar: Equatable { |
| 216 | + public static func == (lhs: Self, rhs: some Vector2n<Scalar>) -> Bool { |
| 217 | + return lhs.x == rhs.x && lhs.y == rhs.y |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +extension Vector2n where Scalar: Hashable { |
| 222 | + public func hash(into hasher: inout Hasher) { |
| 223 | + hasher.combine(x) |
| 224 | + hasher.combine(y) |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +public extension Vector2n where Scalar: FloatingPoint { |
| 229 | + @inlinable |
| 230 | + static func / (lhs: Self, rhs: some Vector2n<Scalar>) -> Self { |
| 231 | + return Self(x: lhs.x / rhs.x, y: lhs.y / rhs.y) |
| 232 | + } |
| 233 | + |
| 234 | + @inlinable |
| 235 | + static func /= (lhs: inout Self, rhs: some Vector2n<Scalar>) { |
| 236 | + lhs = lhs / rhs |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +public extension Vector2n where Scalar: FixedWidthInteger { |
| 241 | + @inlinable |
| 242 | + static func / (lhs: Self, rhs: some Vector2n<Scalar>) -> Self { |
| 243 | + return Self(x: lhs.x / rhs.x, y: lhs.y / rhs.y) |
| 244 | + } |
| 245 | + |
| 246 | + @inlinable |
| 247 | + static func /= (lhs: inout Self, rhs: some Vector2n<Scalar>) { |
| 248 | + lhs = lhs / rhs |
| 249 | + } |
| 250 | +} |
0 commit comments