Skip to content

Commit c4c6355

Browse files
rjallanrealcompnerd
authored andcommitted
Add contains() functions to Rect
1 parent fe3a308 commit c4c6355

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Sources/SwiftWin32/CG/Rect.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,19 @@ public struct Rect {
211211
return !intersection(rect).isEmpty
212212
}
213213

214+
/// Returns whether a rectangle contains a specified point.
215+
public func contains(_ point: Point) -> Bool {
216+
guard !self.isNull else { return false }
217+
let standardized: Rect = self.standardized
218+
return standardized.minX...standardized.maxX ~= point.x
219+
&& standardized.minY...standardized.maxY ~= point.y
220+
}
221+
222+
/// Returns whether the first rectangle contains the second rectangle.
223+
public func contains(_ rect2: Rect) -> Bool {
224+
return self == self.union(rect2)
225+
}
226+
214227
/// Returns whether a rectangle has zero width or height, or is a null
215228
/// rectangle.
216229
public var isEmpty: Bool {

Tests/CoreGraphicsTests/CoreGraphicsTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,43 @@ final class CoreGraphicsTests: XCTestCase {
230230
XCTAssertEqual(Rect.null.union(Rect.infinite), Rect.infinite)
231231
}
232232

233+
func testRectContains() {
234+
let r1: Rect = Rect(x: 0, y: 0, width: 100, height: 100)
235+
let r2: Rect = Rect(x: 25, y: 25, width: 50, height: 50)
236+
let r3: Rect = Rect(x: 75, y: 75, width: 50, height: 50)
237+
let r4: Rect = Rect(x: 125, y: 125, width: 50, height: 50)
238+
let r5: Rect = Rect(x: 75, y: 75, width: -50, height: -50)
239+
240+
241+
XCTAssertTrue(r1.contains(r2))
242+
XCTAssertFalse(r2.contains(r1))
243+
244+
XCTAssertFalse(r1.contains(r3))
245+
XCTAssertFalse(r3.contains(r1))
246+
247+
XCTAssertFalse(r1.contains(r4))
248+
XCTAssertFalse(r4.contains(r1))
249+
250+
XCTAssertTrue(r1.contains(r5))
251+
XCTAssertFalse(r5.contains(r1))
252+
253+
XCTAssertFalse(Rect.null.contains(r1))
254+
XCTAssertTrue(r1.contains(Rect.null))
255+
XCTAssertTrue(Rect.null.contains(Rect.null))
256+
XCTAssertFalse(Rect.null.contains(Rect.zero))
257+
258+
XCTAssertFalse(Rect.zero.contains(r1))
259+
XCTAssertTrue(r1.contains(Rect.zero))
260+
XCTAssertTrue(Rect.zero.contains(Rect.zero))
261+
XCTAssertTrue(Rect.zero.contains(Rect.null))
262+
263+
XCTAssertTrue(Rect.infinite.contains(Rect.infinite))
264+
XCTAssertFalse(r1.contains(Rect.infinite))
265+
XCTAssertTrue(Rect.infinite.contains(Rect.zero))
266+
XCTAssertTrue(Rect.infinite.contains(Rect.null))
267+
268+
}
269+
233270
func testRectNonstandardEquality() {
234271
let r1: Rect = Rect(x: 0, y: 0, width: 10, height: 10)
235272
let r2: Rect = Rect(x: 10, y: 10, width: -10, height: -10)
@@ -257,6 +294,7 @@ final class CoreGraphicsTests: XCTestCase {
257294
("testRectIntersection", testRectIntersection),
258295
("testRectIntersects", testRectIntersects),
259296
("testRectUnion", testRectUnion),
297+
("testRectContains", testRectContains),
260298
("testRectNonstandardEquality", testRectNonstandardEquality),
261299
]
262300
}

0 commit comments

Comments
 (0)