C. Two Squares

C. Two Squares
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two squares, one with sides parallel to the coordinate axes, and another one with sides at 45 degrees to the coordinate axes. Find whether the two squares intersect.

The interior of the square is considered to be part of the square, i.e. if one square is completely inside another, they intersect. If the two squares only share one common point, they are also considered to intersect.

Input

The input data consists of two lines, one for each square, both containing 4 pairs of integers. Each pair represents coordinates of one vertex of the square. Coordinates within each line are either in clockwise or counterclockwise order.

The first line contains the coordinates of the square with sides parallel to the coordinate axes, the second line contains the coordinates of the square at 45 degrees.

All the values are integer and between 100−100 and 100100.

Output

Print "Yes" if squares intersect, otherwise print "No".

You can print each letter in any case (upper or lower).

Examples
input
Copy
0 0 6 0 6 6 0 6
1 3 3 5 5 3 3 1
output
Copy
YES
input
Copy
0 0 6 0 6 6 0 6
7 3 9 5 11 3 9 1
output
Copy
NO
input
Copy
6 0 6 6 0 6 0 0
7 4 4 7 7 10 10 7
output
Copy
YES
Note

In the first example the second square lies entirely within the first square, so they do intersect.

In the second sample squares do not have any points in common.

Here are images corresponding to the samples:

题意:判断两个正方形是否有公共区域

题解:我用的方法比较死,判断斜的正方形(sq2)上的点是否在正的正方形(sq1)上是很简单的,求出sq1的x,y的范围就好,因为都是整数点,且坐标在-100到100之间我们可以枚举所以sq2边上的点,并判断是否在sq1内,这就判断了两个正方形是否有交点,除此之外还要判断sq2是否把sq1包住了(通过简单的数学知识)

代码:

#include <bits/stdc++.h>
using namespace std;
struct point
{
	int x;
	int y;
}a[5],re[50000];
int cmp(point p1,point p2)
{
	if(p1.x==p2.x)
	{
		return p1.y<p2.y;
	}
	else
	{
		return p1.x<p2.x;
	}
}
int main()
{
	int x1,x2,x3,x4,y1,y2,y3,y4;
	int fx[10],fy[10];
	int minx,miny;
	int maxx,maxy;
	cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
	minx = min(x1,x2);
	minx = min(minx,x3);
	minx = min(minx,x4);
	miny = min(y1,y2);
	miny = min(miny,y3);
	miny = min(miny,y4);
	maxx = max(x1,x2);
	maxx = max(maxx,x3);
	maxx = max(maxx,x4);
	maxy = max(y1,y2);
	maxy = max(maxy,y3);
	maxy = max(maxy,y4);
	//cout<<minx<<" "<<miny<<" "<<maxx<<" "<<maxy<<endl;
	int p=0;
	for(int i=0;i<4;i++)
	{
		cin>>a[i].x>>a[i].y;
	}
	sort(a,a+4,cmp);
	int num=0;
	int i,j;
	for(i=a[0].x,j=a[0].y;i<=a[1].x;i++,j++)
	{
		//cout<<i<<" "<<j<<endl;
		re[num].x = i;
		re[num].y=j;
		num++;
	}
	for(i=a[0].x+1,j=a[0].y-1;i<=a[1].x;i++,j--)
	{
		re[num].x = i;
		re[num].y=j;
		//cout<<i<<" "<<j<<endl;
		num++;
	}
	
	for(i=a[3].x,j=a[3].y;i>=a[1].x;i--,j++)
	{
		//cout<<i<<" "<<j<<endl;
		re[num].x = i;
		re[num].y=j;
		num++;
	}
	for(i=a[3].x-1,j=a[3].y-1;i>=a[1].x;i--,j--)
	{
		//cout<<i<<" "<<j<<endl;
		re[num].x = i;
		re[num].y=j;
		num++;
	}
	for(i=0;i<num;i++)
	{
		if(re[i].x>=minx&&re[i].x<=maxx&&re[i].y>=miny&&re[i].y<=maxy)
		{
			p=1;
			//cout<<re[i].x<<" "<<re[i].y<<endl;
			break;
		}
	}
		if(miny>-minx+a[0].x+a[0].y&&miny>minx+a[1].y-a[1].x&&miny<minx+a[2].y-a[2].x&&miny<-minx+a[3].x+a[3].y)
		{
			p=1;
		}
	if(p)
	{
		cout<<"YES"<<endl;
	}
	else
		cout<<"NO"<<endl;
}
如果喜欢,欢迎点赞关注

阅读更多
换一批

没有更多推荐了,返回首页