I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges will have slopes of 0 or infinity.
I've tried to implement what is mentioned in this question but I am not having very much luck.
My current implementation does the following:
// Gets all the vertices for Rectangle 1 and stores them in an array -> arrRect1
// point 1 x: arrRect1, point 1 y: arrRect1 and so on...
// Gets all the vertices for Rectangle 2 and stores them in an array -> arrRect2
// rotated edge of point a, rect 1
int rot_x, rot_y;
rot_x = -arrRect1;
rot_y = arrRect1;
// point on rotated edge
int pnt_x, pnt_y;
pnt_x = arrRect1;
pnt_y = arrRect1;
// test point, a from rect 2
int tst_x, tst_y;
tst_x = arrRect2;
tst_y = arrRect2;
value = (rot_x * (tst_x - pnt_x)) + (rot_y * (tst_y - pnt_y));
cout << "Value: " << value;
if (RectA.Left < RectB.Right && RectA.Right > RectB.Left && RectA.Top < RectB.Bottom && RectA.Bottom > RectB.Top )
or, using Cartesian coordinates...
if (RectA.X1 < RectB.X2 && RectA.X2 > RectB.X1 && RectA.Y1 < RectB.Y2 && RectA.Y2 > RectB.Y1)
Say you have Rect A, and Rect B. Proof is by contradiction. Any one of four conditions guarantees that no overlap can exist:
So condition for Non-Overlap is
Cond1 Or Cond2 Or Cond3 Or Cond4
Therefore, a sufficient condition for Overlap is the opposite (De Morgan)
Not Cond1 And Not Cond2 And Not Cond3 And Not Cond4
This is equivalent to:
RectA.Left < RectB.Right], and
RectA.Right > RectB.Left], and
RectA.Top > RectB.Bottom], and
RectA.Bottom < RectB.Top]
Note 1: It is fairly obvious this same principle can be extended to any number of dimensions.
Note 2: It should also be fairly obvious to count overlaps of just one pixel, change the
< and/or the
> on that boundary to a
<= or a