# looking for the area between two intersecting rectangles [closed]

i'm sincerely confusing myself here... which one actually returns the area of intersection between two rectangles? pleas explain (math frustrates me). any help would be appreciated.

Approach 1

double newX = Math.max(this.x, rect2.x);
double newY = Math.max(this.y, rect2.y);
return new Rect(
newX,
newY,
Math.min(this.x + this.width, rect2.x + rect2.width) - newX,
Math.min(this.y + this.height, rect2.y + rect2.height) - newY
);

Approach 2

double areaOfIntersection =
Math.max(0, Math.max(rect1x2, rect2x2) - Math.min(rect1x1, rect2x1))
* Math.max(0, Math.max(rect1y2, rect2y2) - Math.min(rect1y1, rect2y1));
-
This isn't StackOverflow. If you want to ask this question, write it out mathematically. Do not post code here. –  Alexander Gruber Mar 8 at 18:34
If you could put your code in a better format, it'd be easier to understand what's going on. –  MITjanitor Mar 8 at 18:36
@AlexanderGruber I don't think that do not post code here is an existing Math.SE rule. (If it is, I violated it a few times already). Computational mathematics, including algorithm implementation, is on topic here. Java notation in this question may be different from mathematical notation prevalent in the academic sector, but is logically equivalent. –  user53153 Mar 8 at 19:25
@5pm Apologies- I wasn't trying to cite any rules, just give him advice. (On second glance it does come off a bit unfriendly doesn't it?) It has a much better chance of getting answered written out in algebra rather than Java, especially since it's just basic geometry. I'm not a programmer and I simply can't help somebody when I can't read their question. –  Alexander Gruber Mar 8 at 20:47

## closed as off topic by Alexander Gruber♦, rschwieb, Dennis Gulko, Ron Gordon, Asaf KaragilaMar 8 at 20:13

Questions on Mathematics Stack Exchange are expected to relate to math within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

The two code fragments do different things and work with different ways of representing a rectangle.

• The first one defines rectangle in terms of lower left right corner (x,y), width, and height.
• The second gives the coordinates of lower left corner (x1,y1) and upper right corner (x2,y2).

The mathematically natural formula for the area of intersection is

$$A=\max(0,\min(x_2,x_2')- \max(x_1,x_1'))\cdot \max(0,\min(y_2,y_2')- \max(y_1,y_1'))$$ where I used primes to denote the coordinates of the second rectangle. Your Approach 2 attempts to implement this formula, but max and min are mistakenly interchanged. Should have been

double areaOfIntersection =
Math.max(0, Math.min(rect1x2, rect2x2) - Math.max(rect1x1, rect2x1))
* Math.max(0, Math.min(rect1y2, rect2y2) - Math.max(rect1y1, rect2y1));

The first code does not actually compute the area, it returns the rectangle of intersection itself as a Java object. If you just need the area, use the second code.

-