hdu-1221--Rectangle and Circle(计算几何)
2013-08-13 18:24
381 查看
Rectangle and Circle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2132 Accepted Submission(s): 509
[align=left]Problem Description[/align]
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.
![](http://acm.hdu.edu.cn/data/images/C9-1003-1.jpg)
[align=left]Input[/align]
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).
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
1 1 1 1 2 4 3
1 1 1 1 3 4 4.5
[align=left]Sample Output[/align]
YES
NO
[align=left]Author[/align]
weigang Lee
[align=left]Source[/align]
杭州电子科技大学第三届程序设计大赛
[align=left]Recommend[/align]
Ignatius.L
import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub 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"); else System.out.println("NO"); } }//main private static boolean F(Shape p, double r, Shape p1, Shape p2) { // TODO Auto-generated method stub 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) 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) { // TODO Auto-generated method stub 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; } }
相关文章推荐
- Rectangle and Circle_hdu_1221(数学几何).java
- HDU 1221 Rectangle and Circle
- HDU 1221 Rectangle and Circle(判断圆与矩形是否相交)
- HDU 1221 Rectangle and Circle(判定圆与矩形是否相交)
- 【数学】hdu 1221 Rectangle and Circle
- HDU 1221 Rectangle and Circle(判定圆与矩形是否相交)
- HDU 1221 Rectangle and Circle
- hdu 1221 Rectangle and Circle(矩形与圆焦点问题)
- ZOJ - 3806 Incircle and Circumcircle(计算几何+二分)
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- HDU-Rectangle and Circle
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- 通过程序设计几何图形(Shape)、矩形(Rectangle)、圆形(Circle)、正方形(Square)几种类型, 能够利用接口和多态性计算几何图形的面积和周长并显示。
- HDU-2857-Mirror and Light(计算几何)
- JOJ 2109 && POJ 1981 Circle and Points 计算几何 单位圆覆盖问题
- HDU 4720 Naive and Silly Muggles(计算几何)
- HDU 4720 Naive and Silly Muggles (简单计算几何)
- HDU 4720 Naive and Silly Muggles (简单计算几何)
- POJ1981 Circle and Points(计算几何)