SCAU 判断二维坐标系中2个圆的位置关系

练习2:判断二维坐标系中2个圆的位置关系

内容要求:

编写程序,实现如下功能:
提示用户输入2个圆的圆心坐标和各自的半径值,判断并输出这两个圆之前的位置关系。
两个圆之间的位置关系有如下三种:

两个圆包含时输出:圆 (x1, y1) - r1 包含 圆 (x2, y2) - r2
两个圆相交时输出:圆 (x1, y1) - r1 与 圆 (x2, y2) - r2 相交
两个圆无关时输出:圆 (x1, y1) - r1 与 圆 (x2, y2) - r2 无关

运行实例1:
输入第1个圆 x y r : 0.0 1.0 5.0
输入第2个圆 x y r : 0.0 0.0 10.0
(0.00,0.00)-10.00 包含 (0.00,1.00)-5.00

运行实例2:
输入第1个圆 x y r : 0.0 0.0 3.0
输入第2个圆 x y r : 1.0 1.0 4.0
(0.00,0.00)-3.00 与 (1.00,1.00)-4.00 相交

运行实例3:
输入第1个圆 x y r : 1.0 1.0 1.0
输入第2个圆 x y r : 15.0 -3.0 5.0
(1.00,1.00)-1.00 与 (15.00,-3.00)-5.00 无关

code:

package two;

import java.util.Scanner;

public class second {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        double x1 = cin.nextDouble();
        double y1 = cin.nextDouble();
        double r1 = cin.nextDouble();
        double x2 = cin.nextDouble();
        double y2 = cin.nextDouble();
        double r2 = cin.nextDouble();
        double d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        if (d>(r1+r2))
            System.out.printf("圆 (%.2f, %.2f) - %.2f 与 圆 (%.2f, %.2f) - %.2f 无关",x1,y1,r1,x2,y2,r2);
        else if ((r1>r2&&r2+d<r1)||(r1<r2&&r1+d<r2))
            System.out.printf("圆 (%.2f, %.2f) - %.2f 包含 圆 (%.2f, %.2f) - %.2f",x1,y1,r1,x2,y2,r2);
        else
            System.out.printf("圆 (%.2f, %.2f) - %.2f 与 圆 (%.2f, %.2f) - %.2f 相交",x1,y1,r1,x2,y2,r2);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值