UCF Local Programming Contest 2018 Circle Meets Square (圆与正方形位置关系的判断)
We all know that you can’t put a round peg in a square hole. Asking you to do so in this contestwould be cruel and unusual punishment, which is banned by the Eighth Amendment to the UnitedStates Constitution. But, perhaps a more reasonable problem that the Framers of the Constitutionnever thought about is determining if a given circle and square have an intersection of positivearea (overlap), or touch (share a point in common), or don’t touch at all.
The Framers of the US Constitution and the UCF Programming Team coaches would like to know,given a circle and a square, do the two overlap in some positive area, touch, or don’t touch at all.Help them out by writing a program to solve the problem!
The Problem:
Given the description of a square and circle in the Cartesian plane, determine if the intersectionbetween the two has positive area (overlap), is a single point (touches) or doesn’t exist.
The Input:
The first line of input contains three integers: x (-1,000 ≤ x ≤ 1,000), y (-1,000 ≤ x ≤ 1,000), and r(0 < r ≤ 1,000), representing the x and y coordinates and radius, respectively of the circle.
The second line of input contains three integers: tx (-1,000 ≤ tx < 1,000), ty (-1,000 ≤ ty < 1,000),and s (0 < s ≤ 1,000), where (tx, ty) represents the coordinates of the bottom left corner of the squareand s represents the side length of the square. The square’s top right corner is (tx+s, ty+s), so thatits sides are parallel to the x and y axes.
The Output:
If the circle and square don’t touch, output 0 (zero). If they touch at a single point, output 1(one). If they overlap in positive area, output 2 (two).
题目大意与分析
本题是判断圆与正方形的关系,是相交、只有一个点接触还是相离
对于相交(包括包含)的判断,有两种情况:正方形四个顶点有至少一个在圆内 或者 圆的上下左右四个点与圆心这五个点至少有一个在正方形内
对于只有一个点接触,是在不是上述关系的基础上,如果正方形的四个顶点有一个在圆上,或者圆的上下左右四个点有一个点在正方形上
其余情况为相离
#include<bits/stdc++.h> using namespace std; int zx,zy,sx,sy,d,r; int incirle(int x,int y) //判断点在圆内 用点到圆心距离判断 { if(((x-sx)*(x-sx)+(y-sy)*(y-sy))<(r*r)) { return 1; } else { return 0; } } int oncirle(int x,int y) //判断点在圆上 用点到圆心距离判断 { if(((x-sx)*(x-sx)+(y-sy)*(y-sy))==(r*r)) { return 1; } else { return 0; } } int insquare(int x,int y) //判断点在正方形内 用坐标关系判断 { if((x>zx)&&(x<zx+d)&&(y>zy)&&(y<zy+d)) { return 1; } else { return 0; } } int onsquare(int x,int y) //判断点在正方形上 用坐标关系判断 { if(((x==zx)&&(y>=zy)&&(y<=zy+d))||((x==zx+d)&&(y>=zy)&&(y<=zy+d))||((y==zy)&&(x>=zx)&&(x<=zx+d))||((y==zy+d)&&(x>=zx)&&(x<=zx+d))) { return 1; } else { return 0; } } int pd2() { if(incirle(zx,zy)||incirle(zx+d,zy)||incirle(zx,zy+d)||incirle(zx+d,zy+d)) //某个顶点在圆内 { return 1; } if(insquare(sx,sy)||insquare(sx+r,sy)||insquare(sx-r,sy)||insquare(sx,sy+r)||insquare(sx,sy-r)) //圆的点在正方形内 { return 1; } else { return 0; } } int pd1() { if(oncirle(zx,zy)||oncirle(zx+d,zy)||oncirle(zx,zy+d)||oncirle(zx+d,zy+d)) //点在圆上 { return 1; } if(onsquare(sx+r,sy)||onsquare(sx-r,sy)||onsquare(sx,sy+r)||onsquare(sx,sy-r)) //点在正方形上 { return 1; } else { return 0; } } int main() { cin>>sx>>sy>>r; cin>>zx>>zy>>d; if(pd2()) cout<<"2"<<endl; else if(pd1()) cout<<"1"<<endl; else cout<<"0"<<endl; }