Search code examples
javageometryoverlappingpoints

Find overlapping circles in Java


is there a simple way to find overlapping circles (each with one point in the middle and the same radius) in Java?

For example, if I have a dataset and I have those points

Point1 (3|3)
Point2 (4|2)
r = 1

So I will have it like this:

Overlapping Points

How can I check in Java, whether those two points are overlapping?

Best and thank you in advance!


Solution

  • The circles overlap if the distance between the centers is less than the sum of the radii:

    public static boolean checkOverlap(Circle c1, Circle c2) {
        return Math.hypot(c1.x - c2.x, c1.y - c2.y) < c1.r + c2.r;
    }
    

    If you have many circles and are looking for pairwise overlaps, you might be able to use a k-d tree to do better than O(n2).