Hello. I have a task and it says that I gotta find the area of the overlap of 2 rectangles by entering their bottom left coordinate /x,y/, height and weight. Can't use structures and classes. I just don't know how to find the area by javing that info. Thanks.
You can calculate the (x,y) coordinates of all of the other corners of the rectangle using the bottom-left (x,y) coordinate and the width & height of the rectangle.
So now you have the coordinates of each corner of your two rectangles R1 and R2. Two of the corners of the rectangle in the overlap region, R3, will be shared with corners of R1 & R2. In the example below:
Yes, I did them but now I am confused about the intersecting area, I mean the area of the place where the 2 rectangles overlap because we can have many diff cases.
So the width of R3 is either r2 - l1 or r1 - l2. You could calculate both of these: the one that is +ve will be the correct value; the one that is -ve will be the incorrect value. You can do the same for the height.
There are other cases beyond just these 4, but getting your program working for these 4 would be a good place to start.
left=max(bottomLeftX1, bottomLeftX2);
right=min(upperRightX1, upperRightX2);
bottom=max(bottomLeftY1, bottomLeftY2);
top=min(upperRightY1, upperRightY2);
if ( right > left && top > bottom ) area = (right-left)*(top-bottom);
else area = 0.0;