I studied this question, and followed the answer to implement my own version in Java. I think it is close... but still incorrect. Could you please give me some suggestion on the error?
The full source code can be found here:
// Determine if it is inside
boolean isInside = ((r1x1 >= r2x1) && (r1x2 >= r2x2)
&& (r1y1 >= r2y1) && (r1y2 <= r2y2));
// Determine if it is overlap
boolean isOverLap = (!(r1x1 >= r2x2) && !(r1x2 <= r2x2)
&& !(r1y2 >= r2y1) && !(r1y1 <= r2y2));
// Determine if it is NOT overlap
boolean isNotOverLap = ((r1x1 >= r2x2) || (r1x2 <= r2x2)
|| (r1y2 >= r2y1) || (r1y1 <= r2y2));
According to the textbook I am studying, this is supposed to be: r2 overlap r1
. But my program output r2 does not overlap r1
.
Enter the r1's center x, y coordinates, width and height
1 2 3 5.5
Enter the r2's center x, y coordinates, width and height
3 4 4.5 5
Rectangle 1: (-0.50, 4.75), (2.50, -0.75)
Rectangle 2: (0.75, 6.50), (5.25, 1.50)
r2 does not overlap r1