How to check if circles overlap
Asked Answered
Z

4

7

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;

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.

Zepeda answered 22/3, 2018 at 16:34 Comment(0)
S
5

You can refer to Relative position of two circles.

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)
}
Swane answered 22/3, 2018 at 16:55 Comment(2)
Tried this, still giving me an output of true, when it should be false..Zepeda
Awesome, tried multiple scenarios, this does work :) ty!Zepeda
T
2

if the distance between the centres of the circles is less than the sum of the radius of the two circles then they are overlapping.

double minDistance = Math.max(circle.getRadius(),this.radius) - Math.min(circle.getRadius(),this.radius);
    if (distance <= (this.radius + circle.getRadius()) && distance>= minDistance) return true;
            else return false;
Tabes answered 22/3, 2018 at 16:46 Comment(5)
yes but wouldnt it give out true if the circle is contained inside a circle?Zepeda
yes, and they do overlap in that case and also one is contained inside another, if you want false in that case you also need a condition that the distance should be more than the radius of bigger of the two circlesTabes
there can be better ways of calculating min distance here but the idea would be same, distance should be more than the difference of the radius of two circles.Tabes
EDIT I tried this with a graph that does overlap and it gives me output of false..Zepeda
` you also need a condition that the distance should be more than the radius of bigger of the two circles` doesn't work when the smaller circle's center is inside the bigger circle, yet they still intersect. See my answer for detailsSiphonophore
S
1

Most answers here are wrong.

There are three cases to consider:

  1. enter image description here

Circles overlap and the center of the smaller circle is inside the bigger circle

  1. enter image description here

Circles overlap and the center of the smaller circle is outside the bigger circle

3.enter image description here

The two circles touch at their borders

The algorithm is this (in Java):

  1. Calculate the distance between centers:
double centersDistance = Math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
  1. Check if one circle contains another:
boolean blueContainsRed = blue.radius > centersDistance + red.radius;
boolean redContainsBlue = red.radius > centersDistance + blue.radius;
  1. Check if they overlap:
boolean circlesOverlap = centersDistance <= blue.radius + red.radius;

the <= would make sure that true is returned for case 3 (when borders only touch). If you don't want that, use < .

  1. So the final formula would be:
return !blueContainsRed && !redContainsBlue && circlesOverlap;

This table might also prove useful (from https://planetcalc.com/8098/):

enter image description here

Siphonophore answered 13/10, 2022 at 1:50 Comment(0)
I
0

1.- you have to to place both circles in space, give them some cordinates
2.- you have to get the vectors from 2 circles.
3.- you have to normailze those vectors and get the correct distance in units, im gonna use pixels.
4.- finally you have to check if the distance between those 2 vectors are less that the radious of both circles, if so, then they are overlaped.
here you have a link that is better explained: https://gamedevelopment.tutsplus.com/tutorials/when-worlds-collide-simulating-circle-circle-collisions--gamedev-769, actually this is something very common we use in game development when we want to check circle collisions ( for 2D games )

Intimacy answered 22/3, 2018 at 16:47 Comment(1)
Must have been a mis understanding because this was not helpful to my situation.Zepeda

© 2022 - 2024 — McMap. All rights reserved.