黑色:割草机
$\color{red}{红色:牛饲料}$
由于x轴和y轴是对称的,因此:
一条轴的交点数量>=1
,另外一条轴的交点数量为2
,即为有效情况
完整代码:
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a1, b1, a2, b2; // 割草机
int ta1, tb1, ta2, tb2; //牛饲料
int Area(int a1, int b1, int a2, int b2)
{
return abs(a1 - a2) * abs(b1 - b2);
}
int Len(int l, int r, int a, int b)
{
return min(r, b) - max(l, a);
}
// 计算在(a, b)中,有多少个交点
int cnt(int a, int b, int l, int r)
{
int res = 0;
if(a <= l && l <= b) res++;
if(a <= r && r <= b) res++;
return res;
}
int main()
{
cin >> a1 >> b1 >> a2 >> b2;
cin >> ta1 >> tb1 >> ta2 >> tb2;
int res = 0;
int xc = cnt(ta1, ta2, a1, a2), yc = cnt(tb1, tb2, b1, b2);
if( (xc >= 1 && yc == 2) || (yc >= 1 && xc == 2) )
res = Area(a1, b1, a2, b2) - Len(a1, a2, ta1, ta2) * Len(b1, b2, tb1, tb2);
else
res = Area(a1, b1, a2, b2);
cout << res << endl;
return 0;
}