Two Squares CodeForces - 994C(思维)

本文介绍了一种算法,用于判断两个正方形是否存在相交的情况。其中一个正方形的边平行于坐标轴,而另一个正方形则与坐标轴成45度角。通过检查顶点位置和中心点来确定是否相交。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值