|
| 1 | + |
| 2 | +/// A Rectangle in a bottom-left coordinate space. |
| 3 | +/// |
| 4 | +public struct Rect: Equatable, Hashable { |
| 5 | + public var origin: Point |
| 6 | + public var size: Size |
| 7 | + |
| 8 | + public init(origin: Point, size: Size) { |
| 9 | + self.origin = origin |
| 10 | + self.size = size |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +extension Rect { |
| 15 | + |
| 16 | + public static let empty = Rect(origin: .zero, size: .zero) |
| 17 | + |
| 18 | + public var normalized: Rect { |
| 19 | + let normalizedOrigin = Point(origin.x + (size.width < 0 ? size.width : 0), |
| 20 | + origin.y + (size.height < 0 ? size.height : 0)) |
| 21 | + let normalizedSize = Size(width: abs(size.width), height: abs(size.height)) |
| 22 | + return Rect(origin: normalizedOrigin, size: normalizedSize) |
| 23 | + } |
| 24 | + |
| 25 | + // Coordinate accessors. |
| 26 | + |
| 27 | + public var minX: Float { |
| 28 | + return normalized.origin.x |
| 29 | + } |
| 30 | + |
| 31 | + public var minY: Float { |
| 32 | + return normalized.origin.y |
| 33 | + } |
| 34 | + |
| 35 | + public var midX: Float { |
| 36 | + return origin.x + (size.width/2) |
| 37 | + } |
| 38 | + |
| 39 | + public var midY: Float { |
| 40 | + return origin.y + (size.height/2) |
| 41 | + } |
| 42 | + |
| 43 | + public var maxX: Float { |
| 44 | + let norm = normalized |
| 45 | + return norm.origin.x + norm.size.width |
| 46 | + } |
| 47 | + |
| 48 | + public var maxY: Float { |
| 49 | + let norm = normalized |
| 50 | + return norm.origin.y + norm.size.height |
| 51 | + } |
| 52 | + |
| 53 | + public var center: Point { |
| 54 | + return Point(midX, midY) |
| 55 | + } |
| 56 | + |
| 57 | + public init(size: Size, centeredOn center: Point) { |
| 58 | + self = Rect( |
| 59 | + origin: Point(center.x - (size.width/2), center.y - (size.height/2)), |
| 60 | + size: size |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + // Size accessors. |
| 65 | + |
| 66 | + public var width: Float { |
| 67 | + get { return size.width } |
| 68 | + set { size.width = newValue } |
| 69 | + } |
| 70 | + |
| 71 | + public var height: Float { |
| 72 | + get { return size.height } |
| 73 | + set { size.height = newValue } |
| 74 | + } |
| 75 | + |
| 76 | + // Rounding. |
| 77 | + |
| 78 | + /// Returns a version of this `Rect` rounded by `roundOutwards`. |
| 79 | + public var roundedOutwards: Rect { |
| 80 | + var rect = self |
| 81 | + rect.roundOutwards() |
| 82 | + return rect |
| 83 | + } |
| 84 | + |
| 85 | + /// Rounds this `Rect` to integer coordinates, by rounding all points away from the center. |
| 86 | + public mutating func roundOutwards() { |
| 87 | + origin.x.round(.down) |
| 88 | + origin.y.round(.down) |
| 89 | + size.width.round(.up) |
| 90 | + size.height.round(.up) |
| 91 | + } |
| 92 | + |
| 93 | + /// Returns a version of this `Rect` rounded by `roundInwards`. |
| 94 | + public var roundedInwards: Rect { |
| 95 | + var rect = self |
| 96 | + rect.roundInwards() |
| 97 | + return rect |
| 98 | + } |
| 99 | + |
| 100 | + /// Rounds this `Rect` to integer coordinates, by rounding all points towards the center. |
| 101 | + public mutating func roundInwards() { |
| 102 | + origin.x.round(.up) |
| 103 | + origin.y.round(.up) |
| 104 | + size.width.round(.down) |
| 105 | + size.height.round(.down) |
| 106 | + } |
| 107 | + |
| 108 | + // Subtraction operations. |
| 109 | + |
| 110 | + mutating func contract(by distance: Float) { |
| 111 | + origin.x += distance |
| 112 | + origin.y += distance |
| 113 | + size.width -= 2 * distance |
| 114 | + size.height -= 2 * distance |
| 115 | + } |
| 116 | + |
| 117 | + mutating func clampingShift(dx: Float = 0, dy: Float = 0) { |
| 118 | + origin.x += dx |
| 119 | + origin.y += dy |
| 120 | + size.width -= dx |
| 121 | + size.height -= dy |
| 122 | + } |
| 123 | + |
| 124 | + // Other Utilities. |
| 125 | + |
| 126 | + /// The range of Y coordinates considered inside this `Rect`. |
| 127 | + /// If a coordinate `y` is inside this Range, it means that `minY < y < maxY`, |
| 128 | + /// - i.e., it is _within_ and not _on_ the bounds of the `Rect`. |
| 129 | + internal var internalYCoordinates: Range<Float> { |
| 130 | + let norm = normalized |
| 131 | + return norm.origin.y.nextUp..<norm.origin.y.nextUp + norm.size.height |
| 132 | + } |
| 133 | + |
| 134 | + /// The range of X coordinates considered inside this `Rect`. |
| 135 | + /// If a coordinate `x` is inside this Range, it means that `minX < x < maxX` |
| 136 | + /// - i.e., it is _within_ and not _on_ the bounds of the `Rect` |
| 137 | + internal var internalXCoordinates: Range<Float> { |
| 138 | + let norm = normalized |
| 139 | + return norm.origin.x.nextUp..<norm.origin.x.nextUp + norm.size.width |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// An edge of a `Rect`. |
| 144 | +/// |
| 145 | +public enum RectEdge: Equatable, Hashable, CaseIterable { |
| 146 | + case left |
| 147 | + case right |
| 148 | + case top |
| 149 | + case bottom |
| 150 | + |
| 151 | + /// Whether or not this is a horizontal edge (i.e. top or bottom edge). |
| 152 | + /// |
| 153 | + public var isHorizontal: Bool { |
| 154 | + return self == .top || self == .bottom |
| 155 | + } |
| 156 | + |
| 157 | + /// Whether or not this is a vertical edge (i.e.left or right edge). |
| 158 | + /// |
| 159 | + public var isVertical: Bool { |
| 160 | + return self == .left || self == .right |
| 161 | + } |
| 162 | +} |
0 commit comments