@@ -159,6 +159,50 @@ final class CoreGraphicsTests: XCTestCase {
159
159
Rect ( x: - 28.0 , y: - 28.0 , width: 24.0 , height: 24.0 ) )
160
160
}
161
161
162
+ func testRectIntersection( ) {
163
+ let r1 : Rect = Rect ( x: 0 , y: 0 , width: 100 , height: 100 )
164
+ let r2 : Rect = Rect ( x: 25 , y: 25 , width: 50 , height: 50 )
165
+ let r3 : Rect = Rect ( x: 75 , y: 75 , width: 50 , height: 50 )
166
+ let r4 : Rect = Rect ( x: 125 , y: 125 , width: 50 , height: 50 )
167
+ let r5 : Rect = Rect ( x: 75 , y: 75 , width: - 50 , height: - 50 )
168
+
169
+ // concentric overlap
170
+ XCTAssertEqual ( r1. intersection ( r2) ,
171
+ Rect ( x: 25 , y: 25 , width: 50 , height: 50 ) )
172
+
173
+ // communitivity
174
+ XCTAssertEqual ( r1. intersection ( r2) , r2. intersection ( r1) )
175
+
176
+ // partial overlap
177
+ XCTAssertEqual ( r1. intersection ( r3) ,
178
+ Rect ( x: 75 , y: 75 , width: 25 , height: 25 ) )
179
+
180
+ // no overlap
181
+ XCTAssertTrue ( r1. intersection ( r4) . isNull)
182
+
183
+ // non-standard
184
+ XCTAssertEqual ( r5. intersection ( r1) ,
185
+ Rect ( x: 25 , y: 25 , width: 50 , height: 50 ) )
186
+ XCTAssertEqual ( r1. intersection ( r5) ,
187
+ Rect ( x: 25 , y: 25 , width: 50 , height: 50 ) )
188
+ }
189
+
190
+ func testRectIntersects( ) {
191
+ let r1 : Rect = Rect ( x: 0 , y: 0 , width: 100 , height: 100 )
192
+ let r2 : Rect = Rect ( x: 25 , y: 25 , width: 50 , height: 50 )
193
+ let r3 : Rect = Rect ( x: 75 , y: 75 , width: 50 , height: 50 )
194
+ let r4 : Rect = Rect ( x: 125 , y: 125 , width: 50 , height: 50 )
195
+
196
+ XCTAssertTrue ( r1. intersects ( r2) )
197
+ XCTAssertTrue ( r2. intersects ( r1) )
198
+
199
+ XCTAssertTrue ( r1. intersects ( r3) )
200
+ XCTAssertTrue ( r3. intersects ( r1) )
201
+
202
+ XCTAssertFalse ( r1. intersects ( r4) )
203
+ XCTAssertFalse ( r4. intersects ( r1) )
204
+ }
205
+
162
206
static var allTests = [
163
207
( " testAffineTransformIdentity " , testAffineTransformIdentity) ,
164
208
( " testAffineTransformIdentityIsIdentity " , testAffineTransformIdentityIsIdentity) ,
@@ -170,5 +214,7 @@ final class CoreGraphicsTests: XCTestCase {
170
214
( " testRectStandardizing " , testRectStandardizing) ,
171
215
( " testRectIntegral " , testRectIntegral) ,
172
216
( " testRectInsetBy " , testRectInsetBy) ,
217
+ ( " testRectIntersection " , testRectIntersection) ,
218
+ ( " testRectIntersects " , testRectIntersects) ,
173
219
]
174
220
}
0 commit comments