And two rectangular areas

Today, his teammates gave me a very good idea, share it

First: Given four points identified by two rectangular, if the two do not intersect the rectangle, then it must be ordered, and either one or a row, that is the coordinates x or y is a linearly-aligned, this is where the area and the two areas and

Second: If the intersection, first find the area and subtracting the area of ​​the intersection, two points of intersection between the two certain points in four ordered, thereby to know later intersects rectangular expressed

Posted a question

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin >> t;
	for(int i = 0; i < t; i++){
		ll x[4], y[4];
		scanf("%lld %lld %lld %lld %lld %lld %lld %lld", &x[0], &y[0], &x[1], &y[1], &x[2], &y[2], &x[3], &y[3]);
		ll k = (y[2] - y[3]) * (x[3] - x[2]) + (y[0]-y[1]) * (x[1] - x[0]);
		if(x[1] <= x[2] || x[0] >= x[3] || y[1] >= y[2] || y[0] <= y[3]){
			printf("%lld\n", k);
		}
		else {
			sort(x, x+4);
			sort(y, y+4);
			printf("%lld\n", k - (x[2] - x[1]) * (y[2] - y[1]));
		}
	}
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8211

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/103229788