I need to find an algorithm which determines a relationship between a square and rectangle. It must be able to determine if:
- The square is completely inside the rectangle
- The square is partially inside (overlaps) the rectangle
- Square’s corner only touches a rectangle’s corner
- Square’s edge is on the rectangle’s edge
And here are the inputs (given values) that will help us to extract a mathematical formula for each case:
- x coordinate of the center of the square = squareX
- y coordinate of the center of the square = squareY
- width of the square = squareW
- x coordinate of the center of the rectangle = recX
- y coordinate of the center of the rectangle = recY
- width of the rectangle = recW
- length of the rectangle = recL
P.S: Rectangle’s sizes are always bigger than the square’s width.
I will write the code in Java once we can extract an algorithm using mathematical operations.
Edit:
For the case of corners in touch, here is the code I wrote, and it works (Math.abs means the absolute value):
((Math.abs(Math.abs(recX-squareX)-(recW+squareW)/2))<=0.001) && ((Math.abs(Math.abs(recY-squareY)-(recL+squareW)/2))<=0.001)
updated for doubles