Two Squares CodeForces - 994C(思维)


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 (c) Copyright 2010-2018 Mike Mirzayanov

题意:两行,每行给出4组坐标,表示一个正方形,第一行的正方形①与x轴平行,第二行的正方形②与x轴成45度角,问,这两个正方形是否相交。(哪怕只有一个点重合也是相交)


思路:要知道,如果两个正方形相交,那么,一个正方形的四个角至少有一个处于另一个正方形内,或者一个正方形的中心处于另一个正方形内。  好吧,了解的这个就可以开始做了,很繁琐!但是不难,如果是正方形②的角在正方形①内的话,就很容易就可以判断,但是,如果是正方形①的角在正方形②内的话,就需要求出正方形②的边,然后根据点在直线的上下方关系来判段。


#include "iostream"
#include "algorithm"
using namespace std;
int main()
{
    double nu,nd,nl,nr,mu,md,ml,mr,mx,my,x,y;//u,d,l,r,分别记录最上,最下,最左,最右的值,mx,my记录的是正方形②中心的坐标
    nu=nr=mu=mr=-105;//由于范围是-100~100,所以赋初值
    nd=nl=md=ml=105;
    for(int i=0;i<4;i++){
        cin>>x>>y;
        nd=min(nd,y);
        nu=max(nu,y);
        nl=min(nl,x);
        nr=max(nr,x);
    }
    for(int i=0;i<4;i++){
        cin>>x>>y;
        md=min(md,y);
        mu=max(mu,y);
        ml=min(ml,x);
        mr=max(mr,x);
    }
    mx=(ml+mr)/2;
    my=(mu+md)/2;
    //正方形②的角在正方形①中的情况,分别判断四个角,有一个在里面就成立
    if(mx>=nl&&mx<=nr&&md>=nd&&md<=nu||mx>=nl&&mx<=nr&&mu>=nd&&mu<=nu||ml>=nl&&ml<=nr&&my>=nd&&my<=nu||mr>=nl&&mr<=nr&&my>=nd&&my<=nu)
        cout<<"YES"<<endl;
    //正方形①的角在正方形②中的情况,分别判断四个角,有一个在里面就成立,其中,诸如nu+nr>=ml+my的式子是判断点在直线的上方还是下方
    else if(nu+nr>=ml+my&&nu<=nr-mx+mu&&nu>=nr-mx+md&&nu+nr<=mr+my||nd+nr>=ml+my&&nd<=nr-mx+mu&&nd>=nr-mx+md&&nd+nr<=mr+my)
        cout<<"YES"<<endl;
    else if(nu+nl>=ml+my&&nu<=nl-mx+mu&&nu>=nl-mx+md&&nu+nl<=mr+my||nd+nl>=ml+my&&nd<=nl-mx+mu&&nd>=nl-mx+md&&nd+nl<=mr+my)
        cout<<"YES"<<endl;
    //一个正方形中心在另一个中的情况
    else if(mx>=nl&&mx<=nr&&my>=nd&&my<=nu)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80720174