本文详解如何用纯 java 逻辑(不依赖 math 类)准确计算两个轴对齐矩形的并集面积,并清晰分离输出交集与并集面积,附可运行代码与常见逻辑陷阱分析。
本文详解如何用纯 java 逻辑(不依赖 math 类)准确计算两个轴对齐矩形的并集面积,并清晰分离输出交集与并集面积,附可运行代码与常见逻辑陷阱分析。
在二维平面中,两个矩形的并集面积(Union Area)等于各自面积之和减去它们的交集面积(Intersection Area):
[
\text{Union} = \text{Area}_1 + \text{Area}_2 - \text{Intersection}
]
关键难点在于鲁棒地计算交集面积——当两矩形不重叠时,交集应为 0;若仅边或角接触(无内部重叠),交集仍为 0。原代码中 overlapCalculator 存在多处逻辑错误:变量命名混淆(如误用 overLapArea = Area - w1 * x2)、未处理无重叠情形、X/Y 重叠判断条件冗余且易漏边界,导致结果严重失真(如输入 0 0 5 10 0 0 10 5 错误输出 100)。
✅ 正确解法:分离计算交集宽高,再求积
核心思想是分别计算两矩形在 X 轴和 Y 轴上的重叠长度,二者乘积即为交集面积:
- X 轴重叠宽度 = max(0, min(R1.right, R2.right) - max(R1.left, R2.left))
- Y 轴重叠高度 = max(0, min(R1.bottom, R2.bottom) - max(R1.top, R2.top))
由于题目约定“左上角坐标 + 宽高”,需注意:
- 矩形 R1 的右边界 = x1 + w1,下边界 = y1 + h1(Y 轴向下增长)
- 同理,R2 右边界 = x2 + w2,下边界 = y2 + h2
以下是修正后的完整实现(含输入、计算与清晰输出):
import java.util.Scanner;
public class UnionRectangleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("- What is the area of the union of two rectangles R1 and R2, where top left corner of R1 is (X1,Y1) and its size is (W1,H1), and top left corner of R2 is (X2,Y2) and its size is (W2,H2)?");
System.out.print("Please enter X1, Y1, W1, H1, X2, Y2, W2, H2: ");
int x1 = sc.nextInt(), y1 = sc.nextInt(), w1 = sc.nextInt(), h1 = sc.nextInt();
int x2 = sc.nextInt(), y2 = sc.nextInt(), w2 = sc.nextInt(), h2 = sc.nextInt();
int area1 = w1 * h1;
int area2 = w2 * h2;
int intersectionArea = calculateIntersectionArea(x1, y1, w1, h1, x2, y2, w2, h2);
int unionArea = area1 + area2 - intersectionArea;
System.out.printf("Result: Intersection area is %d thus the total area of the union is %d.\n",
intersectionArea, unionArea);
sc.close();
}
// 纯逻辑计算交集面积(不使用 Math.max/min)
public static int calculateIntersectionArea(int x1, int y1, int w1, int h1,
int x2, int y2, int w2, int h2) {
// 计算左右边界
int left1 = x1, right1 = x1 + w1;
int left2 = x2, right2 = x2 + w2;
// X 轴重叠宽度:重叠区间长度,无重叠则为 0
int overlapX = 0;
int leftOverlap = (left1 > left2) ? left1 : left2; // max(left1, left2)
int rightOverlap = (right1 < right2) ? right1 : right2; // min(right1, right2)
if (rightOverlap > leftOverlap) {
overlapX = rightOverlap - leftOverlap;
}
// 计算上下边界(Y 轴:top = y, bottom = y + h)
int top1 = y1, bottom1 = y1 + h1;
int top2 = y2, bottom2 = y2 + h2;
// Y 轴重叠高度
int overlapY = 0;
int topOverlap = (top1 > top2) ? top1 : top2; // max(top1, top2)
int bottomOverlap = (bottom1 < bottom2) ? bottom1 : bottom2; // min(bottom1, bottom2)
if (bottomOverlap > topOverlap) {
overlapY = bottomOverlap - topOverlap;
}
return overlapX * overlapY;
}
}⚠️ 关键注意事项
- 边界处理:仅当 rightOverlap > leftOverlap 且 bottomOverlap > topOverlap 时才存在正面积交集;等号成立(边重合)不算重叠,返回 0。
- 无 Math 类限制:本实现完全避免 Math.max()/Math.min(),改用三元运算符模拟,符合题目约束。
- 整数安全:所有变量为 int,输入保证非负宽高,无需额外校验(实际项目中建议增加 w1 >= 0 && h1 >= 0 等防御性检查)。
-
验证示例:输入 0 0 5 10 0 0 10 5
- R1:(0,0) → (5,10);R2:(0,0) → (10,5)
- X 重叠:max(0,0)=0, min(5,10)=5 → 5
- Y 重叠:max(0,0)=0, min(10,5)=5 → 5
- 交集 = 5×5 = 25,并集 = 50 + 50 − 25 = 75 ✅
✅ 总结
计算矩形并集面积的本质是精确建模几何重叠。与其堆砌嵌套 if-else 判断方位,不如统一采用“边界取交”范式:先求重叠区间的左右/上下界,再通过差值计算长度。该方法逻辑简洁、可读性强、不易出错,是图形计算中的标准实践。掌握此思路后,可轻松扩展至多个矩形并集(如扫描线算法)或带旋转矩形(需坐标变换),为后续计算几何打下坚实基础。
立即学习“Java免费学习笔记(深入)”;









