You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断圆形是否重叠?Java圆几何判断程序开发技术咨询

嘿,我来帮你搞定这个圆的几何判断问题!你已经搭好了基础的代码框架,现在咱们一步步把缺失的功能补全,特别是你卡壳的圆重叠判断。

一、先理清核心几何逻辑

先把三个功能的数学规则明确下来,写代码就有了依据:

  • 圆A包含圆B:两圆圆心的距离 + 圆B的半径 ≤ 圆A的半径(涵盖完全包含、内切的情况)
  • 点在圆内:点到圆心的距离 ≤ 圆半径(包含点在圆上的情况)
  • 两圆重叠:两圆圆心距离 < 两圆半径之和,且 圆心距离 > |圆A半径 - 圆B半径|(也就是既不互相包含,也不分离,有实际面积交集的情况)
二、完整实现Circle2D类

我把需要的方法都封装到Circle2D里,同时给主类补充了测试代码,方便你验证:

import javafx.scene.shape.Circle;

// 自定义Circle2D类,封装圆的属性与几何判断方法
class Circle2D {
    private double x;
    private double y;
    private double radius;

    // 构造方法初始化圆心坐标与半径
    public Circle2D(double x, double y, double radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    // 计算并返回圆的面积
    public double getArea() {
        return Math.PI * radius * radius;
    }

    // 判断当前圆是否包含另一个圆
    public boolean contains(Circle2D other) {
        // 计算两圆圆心的直线距离
        double centerDistance = Math.sqrt(Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2));
        // 包含条件:圆心距 + 目标圆半径 ≤ 当前圆半径
        return centerDistance + other.radius <= this.radius;
    }

    // 判断指定坐标的点是否在圆内(含圆上)
    public boolean contains(double x, double y) {
        // 计算点到圆心的距离平方(避免开根号,提升性能)
        double distanceSquared = Math.pow(x - this.x, 2) + Math.pow(y - this.y, 2);
        return distanceSquared <= radius * radius;
    }

    // 判断两个圆是否重叠(有交集但互不包含)
    public boolean overlaps(Circle2D other) {
        double centerDistance = Math.sqrt(Math.pow(other.x - this.x, 2) + Math.pow(other.y - this.y, 2));
        double radiusSum = this.radius + other.radius;
        double radiusDiff = Math.abs(this.radius - other.radius);
        
        // 重叠条件:圆心距大于半径差(不包含),且小于半径和(不分离)
        return centerDistance > radiusDiff && centerDistance < radiusSum;
    }
}

public class Problem10_11 {
    public static void main(String[] args) {
        // 创建指定参数的圆形
        Circle2D c1 = new Circle2D(2, 2, 5.5);
        // 测试面积输出
        System.out.println("The area for circle 1 is " + c1.getArea() + " ...");

        // 测试点是否在圆内
        System.out.println("Is point (3,3) inside c1? " + c1.contains(3, 3));
        System.out.println("Is point (10,10) inside c1? " + c1.contains(10, 10));

        // 创建不同的圆测试包含与重叠逻辑
        Circle2D c2 = new Circle2D(4, 5, 1.5); // 被c1包含的小圆
        Circle2D c3 = new Circle2D(10, 10, 3); // 与c1完全分离的圆
        Circle2D c4 = new Circle2D(5, 6, 2); // 与c1部分重叠的圆

        System.out.println("Does c1 contain c2? " + c1.contains(c2));
        System.out.println("Do c1 and c3 overlap? " + c1.overlaps(c3));
        System.out.println("Do c1 and c4 overlap? " + c1.overlaps(c4));
    }
}
三、关键细节说明
  • 计算距离时,我在contains(double x, double y)里用了距离平方对比,避免调用Math.sqrt,能小幅提升性能,如果你需要更直观的写法,换成开根号也完全没问题。
  • 重叠判断里特意排除了“内切”“外切”的情况——因为这两种情况只有一个交点,没有面积交集;如果你的需求里把相切也算作重叠,只需要把判断符号改成>=和<=即可。

内容的提问来源于stack exchange,提问作者Thomas123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:26:11