mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	expand 2d maths classes
This commit is contained in:
		@ -1,13 +1,46 @@
 | 
				
			|||||||
struct Extent<T: AdditiveArithmetic>: Equatable {
 | 
					public struct Extent<T: AdditiveArithmetic & Hashable>: Hashable {
 | 
				
			||||||
  var top: T, bottom: T, left: T, right: T
 | 
					  public var left: T, top: T, right: T, bottom: T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @inline(__always) static func == (lhs: Self, rhs: Self) -> Bool {
 | 
					  @inline(__always) public var topLeft: Point<T> { .init(left, top) }
 | 
				
			||||||
 | 
					  @inline(__always) public var topRight: Point<T> { .init(right, top) }
 | 
				
			||||||
 | 
					  @inline(__always) public var bottomLeft: Point<T> { .init(left, bottom) }
 | 
				
			||||||
 | 
					  @inline(__always) public var bottomRight: Point<T> { .init(right, bottom) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool {
 | 
				
			||||||
    lhs.left == rhs.left && lhs.right == rhs.right && lhs.top == rhs.top && lhs.bottom == rhs.bottom
 | 
					    lhs.left == rhs.left && lhs.right == rhs.right && lhs.top == rhs.top && lhs.bottom == rhs.bottom
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension Extent where T: Comparable {
 | 
					public extension Extent where T: Comparable {
 | 
				
			||||||
  var size: Size<T> { .init(
 | 
					  var size: Size<T> { .init(
 | 
				
			||||||
    right > left ? right - left : left - right,
 | 
					    right > left ? right - left : left - right,
 | 
				
			||||||
    bottom > top ? bottom - top : top - bottom) }
 | 
					    bottom > top ? bottom - top : top - bottom) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Extent where T: SIMDScalar {
 | 
				
			||||||
 | 
					  init(from: SIMD2<T>, to: SIMD2<T>) {
 | 
				
			||||||
 | 
					    self.left   = from.x
 | 
				
			||||||
 | 
					    self.top    = from.y
 | 
				
			||||||
 | 
					    self.right  = to.x
 | 
				
			||||||
 | 
					    self.bottom = to.y
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Extent where T: AdditiveArithmetic {
 | 
				
			||||||
 | 
					  init(_ rect: Rect<T>) {
 | 
				
			||||||
 | 
					    self.left   = rect.x
 | 
				
			||||||
 | 
					    self.top    = rect.y
 | 
				
			||||||
 | 
					    self.right  = rect.x + rect.w
 | 
				
			||||||
 | 
					    self.bottom = rect.y + rect.h
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Extent where T: Numeric {
 | 
				
			||||||
 | 
					  @inline(__always) static func * (lhs: Self, rhs: Size<T>) -> Self {
 | 
				
			||||||
 | 
					    .init(
 | 
				
			||||||
 | 
					      left:   lhs.left * rhs.w,
 | 
				
			||||||
 | 
					      top:    lhs.top * rhs.w,
 | 
				
			||||||
 | 
					      right:  lhs.right * rhs.h,
 | 
				
			||||||
 | 
					      bottom: lhs.bottom * rhs.h)
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,18 +1,32 @@
 | 
				
			|||||||
struct Point<T: AdditiveArithmetic>: Equatable {
 | 
					public struct Point<T: AdditiveArithmetic & Hashable>: Hashable {
 | 
				
			||||||
  var x: T, y: T
 | 
					  public var x: T, y: T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static var zero: Self { .init(.zero, .zero) }
 | 
					  public static var zero: Self { .init(.zero, .zero) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  init(_ x: T, _ y: T) {
 | 
					  public init(_ x: T, _ y: T) {
 | 
				
			||||||
    self.x = x
 | 
					    self.x = x
 | 
				
			||||||
    self.y = y
 | 
					    self.y = y
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @inline(__always) static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y }
 | 
					  public init(scalar value: T) {
 | 
				
			||||||
  @inline(__always) static func != (lhs: Self, rhs: Self) -> Bool { lhs.x != rhs.x || lhs.y != rhs.y }
 | 
					    self.x = value
 | 
				
			||||||
 | 
					    self.y = value
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.x == rhs.x && lhs.y == rhs.y }
 | 
				
			||||||
 | 
					  @inline(__always) public static func != (lhs: Self, rhs: Self) -> Bool { lhs.x != rhs.x || lhs.y != rhs.y }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension Point where T: AdditiveArithmetic {
 | 
					extension Point where T: BinaryInteger {
 | 
				
			||||||
 | 
					  init<O: BinaryInteger>(_ other: Point<O>) {
 | 
				
			||||||
 | 
					    self.init(T(other.x), T(other.y))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  init<O: BinaryFloatingPoint>(_ other: Point<O>) {
 | 
				
			||||||
 | 
					    self.init(T(other.x), T(other.y))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Point where T: AdditiveArithmetic {
 | 
				
			||||||
  @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) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -20,7 +34,34 @@ extension Point where T: AdditiveArithmetic {
 | 
				
			|||||||
  @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 }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension SIMD2 where Scalar: AdditiveArithmetic {
 | 
					public extension Point where T: Numeric {
 | 
				
			||||||
 | 
					  @inline(__always) static func * (lhs: Self, rhs: Self) -> Self { .init(lhs.x * rhs.x, lhs.y * rhs.y) }
 | 
				
			||||||
 | 
					  @inline(__always) static func * (lhs: Self, rhs: T) -> Self { .init(lhs.x * rhs, lhs.y * rhs) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @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: T) { lhs.x *= rhs; lhs.y *= rhs }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extension Point where T: FloatingPoint {
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.x / rhs.x, lhs.y / rhs.y) }
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: Self, rhs: Size<T>) -> Self { .init(lhs.x / rhs.w, lhs.y / rhs.h) }
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: Self, rhs: T) -> Self { .init(lhs.x / rhs, lhs.y / rhs) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @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: Size<T>) { lhs.x /= rhs.w; lhs.y /= rhs.h }
 | 
				
			||||||
 | 
					  @inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.x /= rhs; lhs.y /= rhs }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extension Point where T: BinaryFloatingPoint {
 | 
				
			||||||
 | 
					  init<O: BinaryInteger>(_ other: Point<O>) {
 | 
				
			||||||
 | 
					    self.init(T(other.x), T(other.y))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  init<O: BinaryFloatingPoint>(_ other: Point<O>) {
 | 
				
			||||||
 | 
					    self.init(T(other.x), T(other.y))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension SIMD2 where Scalar: AdditiveArithmetic {
 | 
				
			||||||
  init(_ point: Point<Scalar>) {
 | 
					  init(_ point: Point<Scalar>) {
 | 
				
			||||||
    self.init(point.x, point.y)
 | 
					    self.init(point.x, point.y)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,16 +1,16 @@
 | 
				
			|||||||
public struct Rect<T: AdditiveArithmetic>: Equatable {
 | 
					public struct Rect<T: AdditiveArithmetic & Hashable>: Hashable {
 | 
				
			||||||
  var x: T, y: T, w: T, h: T
 | 
					  public var x: T, y: T, w: T, h: T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  var origin: Point<T> {
 | 
					  public var origin: Point<T> {
 | 
				
			||||||
    get { .init(self.x, self.y) }
 | 
					    get { .init(self.x, self.y) }
 | 
				
			||||||
    set(point) { self.x = point.x; self.y = point.y }
 | 
					    set(point) { self.x = point.x; self.y = point.y }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  var size: Size<T> {
 | 
					  public var size: Size<T> {
 | 
				
			||||||
    get { .init(self.w, self.h) }
 | 
					    get { .init(self.w, self.h) }
 | 
				
			||||||
    set(size) { self.w = size.w; self.h = size.h }
 | 
					    set(size) { self.w = size.w; self.h = size.h }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static var zero: Self { .init(origin: .zero, size: .zero) }
 | 
					  public static var zero: Self { .init(origin: .zero, size: .zero) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  init(x: T, y: T, width: T, height: T) {
 | 
					  init(x: T, y: T, width: T, height: T) {
 | 
				
			||||||
    self.x = x
 | 
					    self.x = x
 | 
				
			||||||
@ -19,7 +19,7 @@ public struct Rect<T: AdditiveArithmetic>: Equatable {
 | 
				
			|||||||
    self.h = height
 | 
					    self.h = height
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  init(origin: Point<T>, size: Size<T>) {
 | 
					  public init(origin: Point<T>, size: Size<T>) {
 | 
				
			||||||
    self.x = origin.x
 | 
					    self.x = origin.x
 | 
				
			||||||
    self.y = origin.y
 | 
					    self.y = origin.y
 | 
				
			||||||
    self.w = size.w
 | 
					    self.w = size.w
 | 
				
			||||||
@ -37,3 +37,21 @@ public extension Rect where T: AdditiveArithmetic {
 | 
				
			|||||||
  var up: T { y }
 | 
					  var up: T { y }
 | 
				
			||||||
  var down: T { y + h }
 | 
					  var down: T { y + h }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Rect where T: BinaryInteger {
 | 
				
			||||||
 | 
					  init<O: BinaryInteger>(_ other: Rect<O>) {
 | 
				
			||||||
 | 
					    self.init(origin: Point<T>(other.origin), size: Size<T>(other.size))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  init<O: BinaryFloatingPoint>(_ other: Rect<O>) {
 | 
				
			||||||
 | 
					    self.init(origin: Point<T>(other.origin), size: Size<T>(other.size))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Rect where T: BinaryFloatingPoint {
 | 
				
			||||||
 | 
					  init<O: BinaryInteger>(_ other: Rect<O>) {
 | 
				
			||||||
 | 
					    self.init(origin: Point<T>(other.origin), size: Size<T>(other.size))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  init<O: BinaryFloatingPoint>(_ other: Rect<O>) {
 | 
				
			||||||
 | 
					    self.init(origin: Point<T>(other.origin), size: Size<T>(other.size))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,17 +1,47 @@
 | 
				
			|||||||
public struct Size<T: AdditiveArithmetic>: Equatable {
 | 
					public struct Size<T: AdditiveArithmetic & Hashable>: Hashable {
 | 
				
			||||||
  var w: T, h: T
 | 
					  public var w: T, h: T
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  static var zero: Self { .init(.zero, .zero) }
 | 
					  public static var zero: Self { .init(.zero, .zero) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  init(_ w: T, _ h: T) {
 | 
					  public init(_ w: T, _ h: T) {
 | 
				
			||||||
    self.w = w
 | 
					    self.w = w
 | 
				
			||||||
    self.h = h
 | 
					    self.h = h
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  public init(scalar value: T) {
 | 
				
			||||||
 | 
					    self.w = value
 | 
				
			||||||
 | 
					    self.h = value
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.w == rhs.w && lhs.h == rhs.h }
 | 
					  @inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool { lhs.w == rhs.w && lhs.h == rhs.h }
 | 
				
			||||||
  @inline(__always) public static func != (lhs: Self, rhs: Self) -> Bool { lhs.w != rhs.w || lhs.h != rhs.h }
 | 
					  @inline(__always) public static func != (lhs: Self, rhs: Self) -> Bool { lhs.w != rhs.w || lhs.h != rhs.h }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Size where T: AdditiveArithmetic {
 | 
				
			||||||
 | 
					  @inline(__always) static func + (lhs: Self, rhs: Self) -> Self { .init(lhs.w + rhs.w, lhs.h + rhs.h) }
 | 
				
			||||||
 | 
					  @inline(__always) static func - (lhs: Self, rhs: Self) -> Self { .init(lhs.w - rhs.w, lhs.h - rhs.h) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @inline(__always) static func += (lhs: inout Self, rhs: Self) { lhs.w += rhs.w; lhs.h += rhs.h }
 | 
				
			||||||
 | 
					  @inline(__always) static func -= (lhs: inout Self, rhs: Self) { lhs.w -= rhs.w; lhs.h -= rhs.h }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public extension Size where T: Numeric {
 | 
				
			||||||
 | 
					  @inline(__always) static func * (lhs: Self, rhs: Self) -> Self { .init(lhs.w * rhs.w, lhs.h * rhs.h) }
 | 
				
			||||||
 | 
					  @inline(__always) static func * (lhs: Self, rhs: T) -> Self { .init(lhs.w * rhs, lhs.h * rhs) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @inline(__always) static func *= (lhs: inout Self, rhs: Self) { lhs.w *= rhs.w; lhs.h *= rhs.h }
 | 
				
			||||||
 | 
					  @inline(__always) static func *= (lhs: inout Self, rhs: T) { lhs.w *= rhs; lhs.h *= rhs }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extension Size where T: FloatingPoint {
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.w / rhs.w, lhs.h / rhs.h) }
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: Self, rhs: T) -> Self { .init(lhs.w / rhs, lhs.h / rhs) }
 | 
				
			||||||
 | 
					  @inline(__always) static func / (lhs: T, rhs: Self) -> Self { .init(lhs / rhs.w, lhs / rhs.h) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @inline(__always) static func /= (lhs: inout Self, rhs: Self) { lhs.w /= rhs.w; lhs.h /= rhs.h }
 | 
				
			||||||
 | 
					  @inline(__always) static func /= (lhs: inout Self, rhs: T) { lhs.w /= rhs; lhs.h /= rhs }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension Size where T: BinaryInteger {
 | 
					extension Size where T: BinaryInteger {
 | 
				
			||||||
  static var one: Self { .init(T(1), T(1)) }
 | 
					  static var one: Self { .init(T(1), T(1)) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -30,8 +60,6 @@ extension Size where T: BinaryFloatingPoint {
 | 
				
			|||||||
  init<O: BinaryFloatingPoint>(_ other: Size<O>) {
 | 
					  init<O: BinaryFloatingPoint>(_ other: Size<O>) {
 | 
				
			||||||
    self.init(T(other.w), T(other.h))
 | 
					    self.init(T(other.w), T(other.h))
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  @inline(__always) public static func / (lhs: Self, rhs: Self) -> Self { .init(lhs.w / rhs.w, lhs.h / rhs.h) }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension SIMD2 where Scalar: AdditiveArithmetic {
 | 
					extension SIMD2 where Scalar: AdditiveArithmetic {
 | 
				
			||||||
@ -39,3 +67,7 @@ extension SIMD2 where Scalar: AdditiveArithmetic {
 | 
				
			|||||||
    self.init(size.w, size.h)
 | 
					    self.init(size.w, size.h)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extension Size where T: SIMDScalar & Numeric {
 | 
				
			||||||
 | 
					  @inline(__always) public static func * (lhs: Self, rhs: SIMD2<T>) -> Self { .init(lhs.w * rhs.x, lhs.h * rhs.y) }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user