How can I test if an oriented rectangle contains another oriented rectangle?
- by gronzzz
I have the following situation:
To detect whether is the red rectangle is inside orange area I use this function:
- (BOOL)isTile:(CGPoint)tile insideCustomAreaMin:(CGPoint)min max:(CGPoint)max {
if ((tile.x < min.x) ||
(tile.x > max.x) ||
(tile.y < min.y) ||
(tile.y > max.y)) {
NSLog(@" Object is out of custom area! ");
return NO;
}
return YES;
}
But what if I need to detect whether the red tile is inside of the blue rectangle?
I wrote this function which uses the world position:
- (BOOL)isTileInsidePlayableArea:(CGPoint)tile {
// get world positions from tiles
CGPoint rt = [[CoordinateFunctions shared] worldFromTile:ccp(24, 0)];
CGPoint lb = [[CoordinateFunctions shared] worldFromTile:ccp(24, 48)];
CGPoint worldTile = [[CoordinateFunctions shared] worldFromTile:tile];
return [self isTile:worldTile insideCustomAreaMin:ccp(lb.x, lb.y) max:ccp(rt.x, rt.y)];
}
How could I do this without converting to the global position of the tiles?