java实验3 判断二维坐标系中2个圆的位置关系

关键字: Java基本语法 输入输出 算术运算 关系运算 选择语句

内容要求:

编写程序,实现如下功能:

提示用户输入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 无关



备注

提交要求:包含源程序文件的JAR


实现代码:

package Experience1124;

import java.util.Scanner;

public class Circle {
    public static void main(String[] args) {
        System.out.print("输入第1个圆 x y r :");
        Scanner sc=new Scanner(System.in);
        double x1=sc.nextDouble();
        double y1=sc.nextDouble();
        double r1=sc.nextDouble();
        System.out.print("输入第2个圆 x y r :");
        double x2=sc.nextDouble();
        double y2=sc.nextDouble();
        double r2=sc.nextDouble();
        sc.close();//   eclipse下添加此句,以防报错
        Relationship(x1,y1,r1,x2,y2,r2);

    }

    public static void Relationship(double x1,double y1,double r1,double x2,double y2,double r2){
        double x=Math.abs(x2-x1);
        double y=Math.abs(y2-y1);
        double min=r1<r2?r1:r2;
        double l=Math.sqrt(x*x+y*y);
        //包含
        if(l+min<r1+r2-min){
            if(r1<r2){
                System.out.println("("+String.format("%.2f",x2)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+
                        " 包含 ("+String.format("%.2f",x1)+","+String.format("%.2f",y1)+")-"+String.format("%.2f",r1)+" ");
            } else{
                System.out.println("("+String.format("%.2f",x1)+","+String.format("%.2f",y1)+")-"+String.format("%.2f",r1)+
                        " 包含 ("+String.format("%.2f",x2)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+" ");
            }
        }
        //无关
        else if(l>r1+r2){
            System.out.println("("+String.format("%.2f",x1)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+
                    " 与 ("+String.format("%.2f",x2)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+" 无关");
        }
        //相交
        else{
            System.out.println("("+String.format("%.2f",x1)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+
                    " 与 ("+String.format("%.2f",x2)+","+String.format("%.2f",y2)+")-"+String.format("%.2f",r2)+" 相交");
        }
    }
}

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值