练习2:判断二维坐标中的2个圆的位置关系
package com. company ;
import java. util. Scanner ;
public class Main {
public static void main ( String [ ] args) {
Scanner in = new Scanner ( System . in) ;
System . out. print ( "请输入第1个圆x y r:" ) ;
double x1 = in. nextDouble ( ) ;
double y1 = in. nextDouble ( ) ;
double r1 = in. nextDouble ( ) ;
System . out. print ( "请输入第2个圆x y r:" ) ;
double x2 = in. nextDouble ( ) ;
double y2 = in. nextDouble ( ) ;
double r2 = in. nextDouble ( ) ;
relationship ( x1, y1, r1, x2, y2, r2) ;
}
public static void relationship ( double x1, double y1, double r1, double x2, double y2, double r2)
{
double m= Math . sqrt ( ( x1- x2) * ( x1- x2) + ( y1- y2) * ( y1- y2) ) ;
if ( Math . abs ( r1- r2) < m&& r1+ r2> m)
{
System . out. printf ( "圆 (%.2f, %.2f) - %.2f 与 圆 (%.2f, %.2f) - %.2f 相交\n" , x1, y1, r1, x2, y2, r2) ;
}
else if ( m> r1+ r2)
{
System . out. printf ( "圆 (%.2f, %.2f) - %.2f 与 圆 (%.2f, %.2f) - %.2f 无关\n" , x1, y1, r1, x2, y2, r2) ;
}
else
{
if ( r1> r2)
System . out. printf ( "圆 (%.2f, %.2f) - %.2f 包含 圆 (%.2f, %.2f) - %.2f\n" , x1, y1, r1, x2, y2, r2) ;
else
System . out. printf ( "圆 (%.2f, %.2f) - %.2f 包含 圆 (%.2f, %.2f) - %.2f\n" , x2, y2, r2, x1, y1, r1) ;
}
}
}