0
votes

I'm having a lot of trouble figuring out what is wrong with my code. Actually, I'm having a very difficult time solving the problem of two rectangles overlapping. The following code should, theoretically, work for the following rectangles:

Rect1: (2.5, 4) width = 2.5, height = 43

Rect2: (1.5, 5) width = 0.5, height = 3

Keep in mind I can't use the Rectangle class to solve this problem. What I've done is calculated the x-values for the left and right edges and the y-values for the top and bottom edges of both rectangles.

I'm first considering -- and I know this does not cover all possible cases -- the scenario in which r2 is within r1.

Note that (x1, y1) and (x2, y2) signify the centers of rectangles 1 and 2, respectively.

right1 = x1 + w1/2;
left1 = x1 - w1/2;
bottom1 = y1 - h1/2;
top1 = y1 + h1/2;

right2 = x2 + w2/2;
left2 = x2 - w2/2;
bottom2 = y2 - h2/2;
top2 = y2 + h2/2;


overlap = (  (right2 < right1 && right2 > left1) &&
(bottom2 > bottom1 && bottom2 < top1) &&
(left2 > left1 && left2 < right1) &&
(top2 < top1 && top2 > bottom1) );

Again, I realize this scenario is not all-encompassing. But even at this point with testing if one rectangle is within another using the above Rect1 and Rect2 values for input, overlap evaluates to false...but it shouldn't -- I've done the math and suggests that the code should work. What did I do wrong?

1
"Keep in mind I can't use the Rectangle class to solve this problem" not for submission, but you can use it to compare your code to a known working implementation. This might help you to figure out the problem. - Andy Turner
Your variable names are quite sporadic/confusing. Maybe if you went through them and picked better names, you'd spot any slight calculation problems. - byxor
@BrandonIbbotson What's so confusing about my variable names? Left/right/top/bottom signify the edges of the rectangles, and the calculations for these values are shown above in the code block. X1, Y1, X2, Y2 are the centers, W1, H1, W2, H2 are the widths and heights, respectively. - AleksandrH
Do respect what @BrandonIbbotson has said. A rectangle is fully specified by two coordinates (x1, y1) and (x2, y2). That ought to be the notation you adopt and is the natural representation in a coordinate system that needs to know the location of the rectangles contained within it. Widths and heights are more to do with a coordinate system with respect to a specific rectangle. The "fully contained" case is then extremely simple to formulate. Coming from a general relativity background, I've learnt that an appropriate coordinate system is absolutely critical! - Bathsheba
Ah I see, I thought that they were 2 dimensional co-ordinates for some reason. Still, x1 and w2 etc look a bit strange. You should ask yourself things like... "Why is right1 set to x1 + w1/2? Shouldn't it just be x1 + w1?" With clearer variable names this process will be much more obvious. - byxor

1 Answers

0
votes

Rectangles will be overlapping if there is some intersection between both their horizontal and vertical sides - so basically it's a case of checking whether two lines overlap for each dimension.

The check whether a line x1 -> x2 overlaps a line x3 -> x4 is whether the first line starts before the end of the second and ends after its start, or in code:

x1 <= x4 && x2 >= x3

To translate this to rectangles, apply the test to both dimensions:

(x1 <= x4 && x2 >= x3) && (y1 <= y4 && y2 >= y3)

..where one rectangle is x1,y1 -> x2,y2 and the other is x3,y3 -> x4,y4 (easily calculated from a start position and a width).