题目链接这里
思路:求交集再减并集
public class Solution {
public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
if(A>E)
{
swap(A, E);
swap(B,F);
swap(C, G);
swap(D, H);
}
int overlatpt=0;
if(C<=E||B>=H||D<=F)
{
overlatpt=0;
}
else
{
int up=Math.min(D, H);
int down=Math.max(B, F);
int length=up-down;
int left=Math.max(A, E);
int right=Math.min(C, G);
int width=right-left;
overlatpt=length*width;
}
return (C-A)*(D-B)+(G-E)*(H-F)-overlatpt;
}
public static void swap(int A,int B)
{
int temp=A;
A=B;
B=temp;
return;
}
}
计算几何图形面积差
本文介绍了一种通过计算两个矩形交集与并集面积来得出面积差的方法,并提供了具体的Java实现代码。文章中详细解释了如何判断矩形是否相交及如何计算相交部分的面积。
1062

被折叠的 条评论
为什么被折叠?



