codeforces 994C 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:

codeforces 994C Two Squares (思维)

codeforces 994C Two Squares (思维)

 

一、原题地址

点我传送

 

二、大致题意

  给出两个正方形,询问是否相交(即使是一个点接触也算是相交)。

 

三、大致思路

一开始以为只需要枚举两个正方形的四个顶点在另外一个正方形内部就可以了,但是一直wa,后来看数据的时候发现过不掉codeforces 994C Two Squares (思维)这样的数据。是没有考虑到这种螺旋覆盖的情况。然后加上了两个正方形的中心也在另一个正方形中。

考虑点是否在多边形内套用的是kuangbin的模板。Orz

 

四、代码

#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <iterator>
#include <functional>
#include <stdlib.h>
#include <time.h>
#include <bitset>
using namespace std;



const double eps = 1e-6;
const double PI = acos(-1.0);
int sgn(double x)
{
	if (fabs(x) < eps)return 0; if (x < 0)return -1;
	else return 1;
}
struct Point {
	double x, y;
	Point() {}
	Point(double _x, double _y) {
		x = _x; y = _y;
	}
	Point operator -(const Point &b)const {
		return Point(x - b.x, y - b.y);
	}
	//叉积
	double operator ^(const Point &b)const {
		return x*b.y - y*b.x;
	}
	//点积
	double operator *(const Point &b)const {
		return x*b.x + y*b.y;
	}
	//绕原点旋转角度B(弧度值),后x,y的变化
	void transXY(double B)
	{
		double tx = x, ty = y;
		x = tx*cos(B) - ty*sin(B); y = tx*sin(B) + ty*cos(B);
	}
};
struct Line
{
	Point s, e;
	Line() {}
	Line(Point _s, Point _e) {
		s = _s; e = _e;
	}
	//两直线相交求交点 //第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交 //只有第一个值为2时,交点才有意义
	pair<int, Point> operator &(const Line &b)const {
		Point res = s; if (sgn((s - e) ^ (b.s - b.e)) == 0) {
			if (sgn((s - b.e) ^ (b.s - b.e)) == 0) return make_pair(0, res);//重合
			else return make_pair(1, res);//平行

		}
		double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e)); res.x += (e.x - s.x)*t;
		res.y += (e.y - s.y)*t;
		return make_pair(2, res);
	}
};
bool OnSeg(Point P, Line L)
{
	return
		sgn((L.s - P) ^ (L.e - P)) == 0 &&
		sgn((P.x - L.s.x) * (P.x - L.e.x)) <= 0 &&
		sgn((P.y - L.s.y) * (P.y - L.e.y)) <= 0;
}
bool inter(Line l1, Line l2) {
	return
		max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
		max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
		max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
		max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
		sgn((l2.s - l1.e) ^ (l1.s - l1.e))*sgn((l2.e - l1.e) ^ (l1.s - l1.e)) <= 0 &&
		sgn((l1.s - l2.e) ^ (l2.s - l2.e))*sgn((l1.e - l2.e) ^ (l2.s - l2.e)) <= 0;
}
int inPoly(Point p, Point poly[], int n) {
	int cnt;
	Line ray, side;
	cnt = 0;
	ray.s = p;
	ray.e.y = p.y;
	ray.e.x = -100000000000.0;//-INF,注意取值防止越界
	for (int i = 0; i < n; i++) {
		side.s = poly[i];
		side.e = poly[(i + 1) % n];
		if (OnSeg(p, side))return 0;
		//如果平行轴则不考虑
		if (sgn(side.s.y - side.e.y) == 0)
			continue;
		if (OnSeg(side.s, ray)) {
			if (sgn(side.s.y - side.e.y) > 0)cnt++;
		}
		else if (OnSeg(side.e, ray)) {
			if (sgn(side.e.y - side.s.y) > 0)cnt++;
		}
		else if (inter(ray, side)) cnt++;
	}
	if (cnt % 2 == 1)return 1; else return -1;
}
int main()
{
	Point a[5], b[5];
	scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
		&a[0].x, &a[0].y,
		&a[1].x, &a[1].y,
		&a[2].x, &a[2].y,
		&a[3].x, &a[3].y);
	a[4].x = (a[0].x + a[1].x + a[2].x + a[3].x) / 4;
	a[4].y = (a[0].y + a[1].y + a[2].y + a[3].y) / 4;
	scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
		&b[0].x, &b[0].y,
		&b[1].x, &b[1].y,
		&b[2].x, &b[2].y,
		&b[3].x, &b[3].y);
	b[4].x = (b[0].x + b[1].x + b[2].x + b[3].x) / 4;
	b[4].y = (b[0].y + b[1].y + b[2].y + b[3].y) / 4;
	int cntt = 0;
	for (int i = 0; i<=4; i++)
	{
		if (inPoly(a[i], b, 4) >= 0)
		{
			cntt++;
		}
	}
	for (int i = 0; i<=4; i++)
	{
		if (inPoly(b[i], a, 4) >= 0)
		{
			cntt++;
		}
	}
	if (cntt >= 1)
	{
		printf("Yes\n");
		return 0;
	}

	printf("No\n");
}