如何检查圆圈是否重叠 [英] How to check if circles overlap

查看:94
本文介绍了如何检查圆圈是否重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,以检查一个圆是否包含另一个圆,某个点是否在一个圆内,或者我遇到麻烦的那个圆(如果一个圆与另一个圆重叠).

I'm trying to write a program that checks if a circle contains another circle, if a certain point is inside a circle, or the one I'm having trouble with, if a circle overlaps with another circle.

import javafx.scene.shape.Circle;
public class Problem10_11 {
    public static void main(String[] args) {
        //Create circle with certain parameters.
        Circle2D c1 = new Circle2D(2, 2, 5.5);

        //Create output which will be tested by all our methods.
        System.out.println("The area for circle 1 is " +c1.getArea()+ 
                " and its perimeter is " + c1.getPerimeter());
        System.out.println("Is (3,3) contained within circle 1? " 
                + c1.contains(3, 3));
        System.out.println("Does circle 1 contain circle 2? " 
                + c1.contains(new Circle2D(4,5,10.5)));
        System.out.println("Does circle 1 overlap with circle 3? " 
                + c1.overlaps(new Circle2D(3, 5, 2.3)));
    }
}
class Circle2D {
    double x; //first parameter
    double y; //second parameter
    double radius; //third parameter

    Circle2D() {
    }
    public Circle2D(double x, double y, double radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void setX(double x) {
        this.x = x;  //set x
    }
    public double getX() {
        return x; //grab x
    }
    public void setY(double y) {
        this.y = y; //set y
    }
    public double getY() {
        return y; //grab y
    }
    public void setRadius(double radius) {
        this.radius = radius; //set radius
    }
    public double getRadius() {
        return radius; //grab radius
    }
    public double getArea() {
            double area = Math.PI*radius*radius; //formula for area
                return area;
    }
    public double getPerimeter() {
            double perimeter = 2*Math.PI*radius; //formula for perimeter
                return perimeter;
    }
    public boolean contains(double x, double y) {
        //Use distance formula to check if a specific point is within our circle. 
            double distance = Math.sqrt(Math.pow(this.x - x, 2) + (Math.pow(this.y - y, 2)));
            if (distance  <= radius * 2)
                return true;
            else {
                return false;
            }
    }
    public boolean contains(Circle2D circle) {
        //Use distance formula to check if a circle is contained within another circle.
        double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));
        if (distance <= (this.radius - circle.radius)) {
            return true;
        } else {
            return false;
        }
    }
    public boolean overlaps(Circle2D circle) {
        //Use distance formula to check if a circle overlaps with another circle.
            double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));



    }
}

所以我的重叠方法一直在底部,但是我内部实际上没有任何东西,因为我不确定该怎么做.我尝试了这个:

So my overlap method is all the way at the bottom but I don't really have anything inside because I'm not sure exactly what to do. I tried this :

if (distance <= radius) return true;
        else return false;

但是没有用.所以我不确定还有什么尝试.仅供参考,我正在尝试检查c1是否与参数为(3,5,2.3)的圆重叠.我感谢您的任何建议/建议.

but that didnt work. So I'm not sure what else to try. FYI, I'm trying to check if c1 overlaps with a circle with parameters (3, 5, 2.3). I appreciate any suggestions/advice.

推荐答案

您可以参考相对位置两个圆圈.

public boolean overlaps(Circle2D circle) {
        //Use distance formula to check if a circle overlaps with another circle.
        double distance = Math.sqrt(Math.pow(circle.getX() - x, 2) + (Math.pow(circle.getY() - y, 2)));
        return distance <= (this.radius + circle.radius) and distance >= Math.abs(this.radius - circle.radius)
}

这篇关于如何检查圆圈是否重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆