Rectangle and Circle_hdu_1221(数学几何).java

来源:互联网 发布:网络黑洞是什么意思 编辑:程序博客网 时间:2024/04/28 06:24

Rectangle and Circle

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2131    Accepted Submission(s): 508


Problem Description
Given a rectangle and a circle in the coordinate system(two edges of the rectangle are parallel with the X-axis, and the other two are parallel with the Y-axis), you have to tell if their borders intersect.

Note: we call them intersect even if they are just tangent. The circle is located by its centre and radius, and the rectangle is located by one of its diagonal.


 

Input
The first line of input is a positive integer P which indicates the number of test cases. Then P test cases follow. Each test cases consists of seven real numbers, they are X,Y,R,X1,Y1,X2,Y2. That means the centre of a circle is (X,Y) and the radius of the circle is R, and one of the rectangle's diagonal is (X1,Y1)-(X2,Y2).
 

Output
For each test case, if the rectangle and the circle intersects, just output "YES" in a single line, or you should output "NO" in a single line.
 

Sample Input
21 1 1 1 2 4 31 1 1 1 3 4 4.5
 

Sample Output
YESNO
 

Author
weigang Lee
 

Source
杭州电子科技大学第三届程序设计大赛
 
import java.util.Scanner;public class Main{private static Point R,f[];public static void main(String[] args) {Scanner input=new Scanner(System.in);int N=input.nextInt();while(N-->0){float a=input.nextFloat();float b=input.nextFloat();float r=input.nextFloat();R=new Point(a,b,r);a=input.nextFloat();b=input.nextFloat();float a1=input.nextFloat();float b1=input.nextFloat();f=new Point[4];f[0]=new Point(Math.min(a, a1),Math.min(b, b1));f[1]=new Point(Math.min(a, a1),Math.max(b, b1));f[2]=new Point(Math.max(a, a1),Math.max(b, b1));f[3]=new Point(Math.max(a, a1),Math.min(b, b1));if(pd()){System.out.println("YES");}elseSystem.out.println("NO");}}private static boolean pd() {//长方形完全在园内if(F(1)<R.r&&F(2)<R.r&&F(3)<R.r&&F(0)<R.r){return false;}//圆在长方形左边、下边、右边、上边if(f[0].x-R.x>R.r||f[0].y-R.y>R.r||R.x-f[3].x>R.r||R.y-f[2].y>R.y)return false;//圆完全在长方形的内部if(R.x-f[0].x>R.r&&R.y-f[0].y>R.r&&f[3].x-R.x>R.r&&f[1].y-R.y>R.r){return false;}return true;}private static double F(int i) {return Math.sqrt(Math.pow(f[i].x-R.x, 2)+Math.pow(f[i].y-R.y, 2));}}class Point{float x,y,r;Point(float x,float y){this.x=x;this.y=y;}Point(float x,float y ,float r){this.x=x;this.y=y;this.r=r;}}



/* * 89310372013-08-13 18:15:21Accepted1221140MS3616K1842 BJavazhangyi hdu */import java.util.Scanner;public class Main {//郭思慧,检查错误public static void main(String[] args) {Scanner input=new Scanner(System.in);int n=input.nextInt();while(n-->0){double x,y;x=input.nextDouble();y=input.nextDouble();Shape p=new Shape(x,y);//圆心坐标double R=input.nextDouble();//半径x=input.nextDouble();y=input.nextDouble();Shape p1=new Shape(x,y);x=input.nextDouble();y=input.nextDouble();Shape p2=new Shape(x,y);boolean flag=F(p,R,p1,p2);if(flag==true)System.out.println("YES");elseSystem.out.println("NO");}}//mainprivate static boolean F(Shape p, double r, Shape p1, Shape p2) {double min_x=Math.min(p1.x, p2.x);double max_x=Math.max(p1.x, p2.x);double min_y=Math.min(p1.y, p2.y);double max_y=Math.max(p1.y, p2.y);Shape q1=new Shape(min_x,min_y);Shape q2=new Shape(min_x,max_y);Shape q3=new Shape(max_x,max_y);Shape q4=new Shape(max_x,min_y);//圆在矩形的里面if(p.x-r>min_x && p.x+r<max_x && p.y+r<max_y && p.y-r>min_y)return false;//圆在矩形的外面if(p.x+r<min_x || p.x-r>max_x || p.y-r>max_y || p.y+r<min_y)//p.y-r<max_y 出错return false;//矩形在圆外面,特别是圆是矩形的外接圆boolean i=Double.compare(distance(p, q1),r)<0;boolean j=Double.compare(distance(p, q2),r)<0;boolean k=Double.compare(distance(p, q3),r)<0;boolean t=Double.compare(distance(p, q4),r)<0;if(i&&j&&k&&t)return false;return true;}private static double distance(Shape p, Shape q1) {return Math.hypot(p.x-q1.x, p.y-q1.y);}}class Shape{double x=0;double y=0;Shape(double x,double y){this.x=x;this.y=y;}}




原创粉丝点击