@@ -111,4 +111,22 @@ class BoundingBoxTests: XCTestCase {
111
111
XCTAssertEqual ( result, BoundingBox ( p1: CGPoint ( x: 1.0 , y: 1.0 ) , p2: CGPoint ( x: 3.0 , y: 5.0 ) ) )
112
112
}
113
113
114
+ func testIntersection( ) {
115
+ let box1 = BoundingBox ( p1: CGPoint ( x: 0 , y: 0 ) , p2: CGPoint ( x: 3 , y: 2 ) )
116
+ let box2 = BoundingBox ( p1: CGPoint ( x: 2 , y: 1 ) , p2: CGPoint ( x: 4 , y: 5 ) ) // overlaps box1
117
+ let box3 = BoundingBox ( p1: CGPoint ( x: 2 , y: 4 ) , p2: CGPoint ( x: 4 , y: 5 ) ) // does not overlap box1
118
+ let box4 = BoundingBox ( p1: CGPoint ( x: 3 , y: 0 ) , p2: CGPoint ( x: 5 , y: 2 ) ) // overlaps box1 exactly on x edge
119
+ let box5 = BoundingBox ( p1: CGPoint ( x: 0 , y: 2 ) , p2: CGPoint ( x: 3 , y: 4 ) ) // overlaps box1 exactly on y edge
120
+ let box6 = BoundingBox ( p1: CGPoint ( x: 0 , y: 0 ) , p2: CGPoint ( x: - 5 , y: - 5 ) ) // overlaps box1 only at (0,0)
121
+ let expectedBox = BoundingBox ( p1: CGPoint ( x: 2 , y: 1 ) , p2: CGPoint ( x: 3 , y: 2 ) )
122
+ XCTAssertEqual ( box1. intersection ( box2) , expectedBox)
123
+ XCTAssertEqual ( box2. intersection ( box1) , expectedBox)
124
+ XCTAssertEqual ( box1. intersection ( box3) , BoundingBox . empty)
125
+ XCTAssertEqual ( box1. intersection ( BoundingBox . empty) , BoundingBox . empty)
126
+ XCTAssertEqual ( BoundingBox . empty. intersection ( box1) , BoundingBox . empty)
127
+ XCTAssertEqual ( BoundingBox . empty. intersection ( BoundingBox . empty) , BoundingBox . empty)
128
+ XCTAssertEqual ( box1. intersection ( box4) , BoundingBox ( p1: CGPoint ( x: 3 , y: 0 ) , p2: CGPoint ( x: 3 , y: 2 ) ) )
129
+ XCTAssertEqual ( box1. intersection ( box5) , BoundingBox ( p1: CGPoint ( x: 0 , y: 2 ) , p2: CGPoint ( x: 3 , y: 2 ) ) )
130
+ XCTAssertEqual ( box1. intersection ( box6) , BoundingBox ( p1: CGPoint . zero, p2: CGPoint . zero) )
131
+ }
114
132
}
0 commit comments