HDU 1221 rectangle and circle (determine whether the circle and rectangle are intersecting)

Source: Internet
Author: User


This is a simple question, but there are not many people and the AC rate is not very high. The main reason is that this question should be noted and detailed, and the card accuracy is not so strict.

1. If the distance from the four vertices of the rectangle to the center of the circle is greater than the radius, it will obviously not overlap (many people will die here)

2. Roll the circle center around the side of the rectangle and the area covered by the circle. As long as the center of the circle is inside the area, it will meet the intersection conditions.

For specific judgment, see the following code in three cases: the distance from the horizontal side, the distance from the vertical side, and the distance from the four vertices.

#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <cmath>#define eps 1e-8using namespace std;struct point{    double x;    double y;}circle,a,b,c,d;double r;double dis(point &a,point &b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){    int t;    scanf("%d",&t);    point temp;    while(t--)    {        scanf("%lf%lf%lf%lf%lf%lf%lf",&circle.x,&circle.y,&r,&a.x,&a.y,&b.x,&b.y);        if(a.x > b.x)        temp=a,a=b,b=temp;       // if(((circle.x >= min(a.x,b.x)) && (circle.x <=max(a.x,b.x))) && ((circle.y <=max(a.y,b.y)) && (circle.y>=min(a.y,b.y))))      //  { printf("YES\n");continue;}        c.x=a.x,c.y=b.y;        d.x=b.x,d.y=a.y;        if(dis(a,circle)<r && dis(b,circle) <r && dis(c,circle)<r && dis(d,circle) <r)        {printf("NO\n");continue;}        if(circle.x>=a.x && circle.x<=b.x)        {            if(fabs(circle.y-a.y) <= r || fabs(circle.y-b.y) <= r)           {printf("YES\n");continue;}        }        if((circle.y >= a.y && circle.y <=b.y) || (circle.y>=b.y && circle.y<=a.y))        {            if(fabs(circle.x-a.x) <=r || fabs(circle.x-b.x) <=r)            {printf("YES\n");continue;}        }        if(dis(a,circle)<=r || dis(b,circle) <=r || dis(c,circle)<=r || dis(d,circle) <=r)        {printf("YES\n");continue;}        printf("NO\n");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.