Skip to content

Commit f065a43

Browse files
committed
Refactor math types
1 parent 3e35e65 commit f065a43

File tree

3 files changed

+316
-36
lines changed

3 files changed

+316
-36
lines changed

Sources/GameMath/2D Types (New)/Vector2n.swift

Lines changed: 130 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public struct Position2n<Scalar: Vector2n.ScalarType>: Vector2n {
2828
}
2929
}
3030
extension Position2n: AdditiveArithmetic where Scalar: AdditiveArithmetic { }
31+
extension Position2n: ExpressibleByIntegerLiteral where Scalar: FixedWidthInteger & _ExpressibleByBuiltinIntegerLiteral & ExpressibleByIntegerLiteral { }
32+
extension Position2n: ExpressibleByFloatLiteral where Scalar: FloatingPoint & _ExpressibleByBuiltinFloatLiteral & ExpressibleByFloatLiteral { }
3133
extension Position2n: Equatable where Scalar: Equatable { }
3234
extension Position2n: Hashable where Scalar: Hashable { }
3335
extension Position2n: Comparable where Scalar: Comparable { }
@@ -50,6 +52,8 @@ public struct Size2n<Scalar: Vector2n.ScalarType>: Vector2n {
5052
public static var one: Self { .init(x: 1, y: 1) }
5153
}
5254
extension Size2n: AdditiveArithmetic where Scalar: AdditiveArithmetic { }
55+
extension Size2n: ExpressibleByIntegerLiteral where Scalar: FixedWidthInteger & _ExpressibleByBuiltinIntegerLiteral & ExpressibleByIntegerLiteral { }
56+
extension Size2n: ExpressibleByFloatLiteral where Scalar: FloatingPoint & _ExpressibleByBuiltinFloatLiteral & ExpressibleByFloatLiteral { }
5357
extension Size2n: Equatable where Scalar: Equatable { }
5458
extension Size2n: Hashable where Scalar: Hashable { }
5559
extension Size2n: Comparable where Scalar: Comparable { }
@@ -74,6 +78,42 @@ extension Vector2n where Scalar: BinaryInteger {
7478
)
7579
}
7680

81+
@inlinable
82+
public init<T: Vector2n>(_ vector2n: T) where T.Scalar: BinaryFloatingPoint {
83+
self.init(
84+
x: Scalar(vector2n.x),
85+
y: Scalar(vector2n.y)
86+
)
87+
}
88+
89+
@inlinable
90+
public init<T: Vector2n>(truncatingIfNeeded vector2n: T) where T.Scalar: BinaryInteger {
91+
self.init(
92+
x: Scalar(truncatingIfNeeded: vector2n.x),
93+
y: Scalar(truncatingIfNeeded: vector2n.y)
94+
)
95+
}
96+
}
97+
98+
extension Vector2n where Scalar: BinaryFloatingPoint {
99+
@inlinable
100+
public init(_ vector2n: some Vector2n<Scalar>) {
101+
self.init(
102+
x: vector2n.x,
103+
y: vector2n.y
104+
)
105+
}
106+
107+
@inlinable
108+
public init<T: Vector2n>(_ vector2n: T) where T.Scalar: BinaryInteger {
109+
self.init(
110+
x: Scalar(vector2n.x),
111+
y: Scalar(vector2n.y)
112+
)
113+
}
114+
}
115+
116+
extension Vector2n where Scalar: BinaryInteger {
77117
@inlinable
78118
public init(_ vector2: Vector2Counterpart) {
79119
self.init(
@@ -97,14 +137,6 @@ extension Vector2n where Scalar: BinaryInteger {
97137
}
98138

99139
extension Vector2n where Scalar: BinaryFloatingPoint {
100-
@inlinable
101-
public init(_ vector2n: some Vector2n<Scalar>) {
102-
self.init(
103-
x: vector2n.x,
104-
y: vector2n.y
105-
)
106-
}
107-
108140
@inlinable
109141
public init(_ vector2: Vector2Counterpart) {
110142
self.init(
@@ -127,31 +159,88 @@ extension Vector2n where Scalar: BinaryFloatingPoint {
127159
}
128160
}
129161

162+
public extension Vector2n where Scalar: FixedWidthInteger & _ExpressibleByBuiltinIntegerLiteral & ExpressibleByIntegerLiteral {
163+
typealias IntegerLiteralType = Scalar
164+
init(integerLiteral value: IntegerLiteralType) {
165+
self.init(x: value, y: value)
166+
}
167+
}
168+
169+
public extension Vector2n where Scalar: FloatingPoint & _ExpressibleByBuiltinFloatLiteral & ExpressibleByFloatLiteral {
170+
typealias FloatLiteralType = Scalar
171+
init(floatLiteral value: FloatLiteralType) {
172+
self.init(x: value, y: value)
173+
}
174+
}
175+
130176
public extension Vector2n where Scalar: AdditiveArithmetic {
177+
@inlinable
178+
static func + (lhs: Self, rhs: some Vector2n<Scalar>) -> Self {
179+
return Self(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
180+
}
181+
131182
@inlinable
132183
static func - (lhs: Self, rhs: some Vector2n<Scalar>) -> Self {
133184
return Self(x: lhs.x - rhs.x, y: lhs.y - rhs.y)
134185
}
135186

187+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
136188
@inlinable
137-
static func + (lhs: Self, rhs: some Vector2n<Scalar>) -> Self {
138-
return Self(x: lhs.x + rhs.x, y: lhs.y + rhs.y)
189+
static func + (lhs: Self, rhs: Scalar) -> Self {
190+
return Self(x: lhs.x + rhs, y: lhs.y + rhs)
139191
}
140192

193+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
141194
@inlinable
142-
static var zero: Self {Self(x: .zero, y: .zero)}
195+
static func - (lhs: Self, rhs: Scalar) -> Self {
196+
return Self(x: lhs.x - rhs, y: lhs.y - rhs)
197+
}
198+
199+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
200+
@inlinable
201+
static var zero: Self {Self(x: Scalar.zero, y: Scalar.zero)}
143202
}
144203

145204
public extension Vector2n where Scalar: Numeric {
146205
@inlinable
147206
static func * (lhs: Self, rhs: some Vector2n<Scalar>) -> Self {
148207
return Self(x: lhs.x * rhs.x, y: lhs.y * rhs.y)
149208
}
150-
209+
151210
@inlinable
152211
static func *= (lhs: inout Self, rhs: some Vector2n<Scalar>) {
153212
lhs = lhs * rhs
154213
}
214+
215+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
216+
@inlinable
217+
static func * (lhs: Self, rhs: Scalar) -> Self {
218+
return Self(x: lhs.x * rhs, y: lhs.y * rhs)
219+
}
220+
221+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
222+
@inlinable
223+
static func *= (lhs: inout Self, rhs: Scalar) {
224+
lhs = lhs * rhs
225+
}
226+
227+
@inlinable
228+
init?<T: Vector2n>(exactly vector2n: T) where T.Scalar: BinaryInteger {
229+
guard let x = Scalar(exactly: vector2n.x), let y = Scalar(exactly: vector2n.y) else {
230+
return nil
231+
}
232+
self.init(x: x, y: y)
233+
}
234+
}
235+
236+
public extension Vector2n where Scalar: SignedNumeric {
237+
prefix static func - (operand: Self) -> Self {
238+
return Self(x: -operand.x, y: -operand.y)
239+
}
240+
241+
mutating func negate() -> Self {
242+
return Self(x: -x, y: -y)
243+
}
155244
}
156245

157246
public extension Vector2n where Scalar: FloatingPoint {
@@ -165,6 +254,18 @@ public extension Vector2n where Scalar: FloatingPoint {
165254
lhs = lhs / rhs
166255
}
167256

257+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
258+
@inlinable
259+
static func / (lhs: Self, rhs: Scalar) -> Self {
260+
return Self(x: lhs.x / rhs, y: lhs.y / rhs)
261+
}
262+
263+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
264+
@inlinable
265+
static func /= (lhs: inout Self, rhs: Scalar) {
266+
lhs = lhs / rhs
267+
}
268+
168269
@inlinable
169270
static var nan: Self {Self(x: .nan, y: .nan)}
170271

@@ -182,6 +283,18 @@ public extension Vector2n where Scalar: FixedWidthInteger {
182283
static func /= (lhs: inout Self, rhs: some Vector2n<Scalar>) {
183284
lhs = lhs / rhs
184285
}
286+
287+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
288+
@inlinable
289+
static func / (lhs: Self, rhs: Scalar) -> Self {
290+
return Self(x: lhs.x / rhs, y: lhs.y / rhs)
291+
}
292+
293+
@_disfavoredOverload // <- Tell the compiler to prefer using integer literals
294+
@inlinable
295+
static func /= (lhs: inout Self, rhs: Scalar) {
296+
lhs = lhs / rhs
297+
}
185298
}
186299

187300
public extension Vector2n where Scalar: Comparable {
@@ -201,6 +314,11 @@ public extension Vector2n where Scalar: Comparable {
201314
}
202315
}
203316

317+
@inlinable
318+
public func abs<T: Vector2n>(_ vector: T) -> T where T.Scalar : Comparable, T.Scalar : SignedNumeric {
319+
return T(x: abs(vector.x), y: abs(vector.y))
320+
}
321+
204322
extension Vector2n where Scalar: Equatable {
205323
@inlinable
206324
public static func == (lhs: Self, rhs: some Vector2n<Scalar>) -> Bool {

0 commit comments

Comments
 (0)