Skip to content

Commit c074449

Browse files
committed
Added Equatable, Hashable to Pair, Size and Rect.
Added some geometry helpers for Rect.
1 parent f9ea3b6 commit c074449

File tree

1 file changed

+74
-10
lines changed

1 file changed

+74
-10
lines changed

Sources/SwiftPlot/Pair.swift

Lines changed: 74 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public struct Pair<T,U> {
88
self.y = y
99
}
1010
}
11+
extension Pair: Equatable where T: Equatable, U: Equatable {}
12+
extension Pair: Hashable where T: Hashable, U: Hashable {}
1113

1214
public typealias Point = Pair<Float,Float>
1315

@@ -19,7 +21,7 @@ public func + (lhs: Point, rhs: Point) -> Point {
1921
return Point(lhs.x + rhs.x, lhs.y + rhs.y)
2022
}
2123

22-
public struct Size {
24+
public struct Size: Hashable {
2325
public var width: Float
2426
public var height: Float
2527

@@ -34,7 +36,7 @@ extension Size {
3436

3537
/// A Rectangle in a bottom-left coordinate space.
3638
///
37-
public struct Rect {
39+
public struct Rect: Hashable {
3840
public var origin: Point
3941
public var size: Size
4042

@@ -54,6 +56,8 @@ extension Rect {
5456
return Rect(origin: normalizedOrigin, size: normalizedSize)
5557
}
5658

59+
// Coordinate accessors.
60+
5761
public var minX: Float {
5862
return normalized.origin.x
5963
}
@@ -79,22 +83,64 @@ extension Rect {
7983
let norm = normalized
8084
return norm.origin.y + norm.size.height
8185
}
82-
83-
public var width: Float {
84-
return size.width
85-
}
86-
87-
public var height: Float {
88-
return size.height
86+
87+
public var center: Point {
88+
return Point(midX, midY)
8989
}
90-
90+
9191
public init(size: Size, centeredOn center: Point) {
9292
self = Rect(
9393
origin: Point(center.x - size.width/2, center.y - size.height/2),
9494
size: size
9595
)
9696
}
97+
98+
// Size accessors.
99+
100+
public var width: Float {
101+
get { return size.width }
102+
set { size.width = newValue }
103+
}
97104

105+
public var height: Float {
106+
get { return size.height }
107+
set { size.height = newValue }
108+
}
109+
110+
// Rounding.
111+
112+
/// Returns a version of this `Rect` rounded by `roundOutwards`
113+
public var roundedOutwards: Rect {
114+
var rect = self
115+
rect.roundOutwards()
116+
return rect
117+
}
118+
119+
/// Rounds this `Rect` to integer coordinates, by rounding all points away from the center.
120+
public mutating func roundOutwards() {
121+
origin.x.round(.down)
122+
origin.y.round(.down)
123+
size.width.round(.up)
124+
size.height.round(.up)
125+
}
126+
127+
/// Returns a version of this `Rect` rounded by `roundInwards`
128+
public var roundedInwards: Rect {
129+
var rect = self
130+
rect.roundInwards()
131+
return rect
132+
}
133+
134+
/// Rounds this `Rect` to integer coordinates, by rounding all points towards the center.
135+
public mutating func roundInwards() {
136+
origin.x.round(.up)
137+
origin.y.round(.up)
138+
size.width.round(.down)
139+
size.width.round(.down)
140+
}
141+
142+
// Subtraction operations.
143+
98144
mutating func contract(by distance: Float) {
99145
origin.x += distance
100146
origin.y += distance
@@ -108,4 +154,22 @@ extension Rect {
108154
size.width -= dx
109155
size.height -= dy
110156
}
157+
158+
// Other Utilities.
159+
160+
/// The range of Y coordinates considered inside this `Rect`.
161+
/// If a coordinate `y` is inside this Range, it means that `minY < y < maxY`,
162+
/// - i.e., it is _within_ and not _on_ the bounds of the `Rect`.
163+
internal var internalYCoordinates: Range<Float> {
164+
let norm = normalized
165+
return norm.origin.y.nextUp..<norm.origin.y.nextUp + norm.size.height
166+
}
167+
168+
/// The range of X coordinates considered inside this `Rect`.
169+
/// If a coordinate `x` is inside this Range, it means that `minX < x < maxX`
170+
/// - i.e., it is _within_ and not _on_ the bounds of the `Rect`
171+
internal var internalXCoordinates: Range<Float> {
172+
let norm = normalized
173+
return norm.origin.x.nextUp..<norm.origin.x.nextUp + norm.size.width
174+
}
111175
}

0 commit comments

Comments
 (0)