Files
voxelotl-engine/Sources/Voxelotl/Math/Rectangle.swift

58 lines
1.5 KiB
Swift
Raw Permalink Normal View History

2024-09-13 17:11:46 +10:00
public struct Rect<T: AdditiveArithmetic & Hashable>: Hashable {
public var x: T, y: T, w: T, h: T
2024-08-13 08:38:21 +10:00
2024-09-13 17:11:46 +10:00
public var origin: Point<T> {
2024-08-13 08:38:21 +10:00
get { .init(self.x, self.y) }
set(point) { self.x = point.x; self.y = point.y }
}
2024-09-13 17:11:46 +10:00
public var size: Size<T> {
2024-08-13 08:38:21 +10:00
get { .init(self.w, self.h) }
set(size) { self.w = size.w; self.h = size.h }
}
2024-09-13 17:11:46 +10:00
public static var zero: Self { .init(origin: .zero, size: .zero) }
2024-08-13 08:38:21 +10:00
init(x: T, y: T, width: T, height: T) {
self.x = x
self.y = y
self.w = width
self.h = height
}
2024-09-13 17:11:46 +10:00
public init(origin: Point<T>, size: Size<T>) {
2024-08-13 08:38:21 +10:00
self.x = origin.x
self.y = origin.y
self.w = size.w
self.h = size.h
}
@inline(__always) public static func == (lhs: Self, rhs: Self) -> Bool {
2024-08-13 08:38:21 +10:00
lhs.x == rhs.x && lhs.y == rhs.y && lhs.w == rhs.w && lhs.h == rhs.h
}
}
public extension Rect where T: AdditiveArithmetic {
2024-08-13 08:38:21 +10:00
var left: T { x }
var right: T { x + w }
var up: T { y }
var down: T { y + h }
}
2024-09-13 17:11:46 +10:00
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))
}
}