ebz
ebz

Reputation: 21

return true if circle c overlaps any other circle in the arraylist

I have an assigment in my beginners class in coding, in java. This is the assignment (translated from swedish to english by me, therefor I'm sorry in advance);

The class CircleList keeps track of a number of circles. Implement the class.

Guidance

The class Circle looks like this;

class Circle {
    private double radius;
    private double x;
    private double y;

   /** Constructs a circle with the radie radius and the 
       center in  x, y. */
    public Circle(double x, double y, double radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }

    /** Returns the x coordinate of the center. */
    public double getX() {
        return x;
    }

    /** Returns the y coordinate of the center. */
    public double getY() {
        return y;
    }

    /** Returns the radius. */
    public double radius() {
        return radius;
    }

     /** Returns the area. */
    public double area() {
        return Math.PI * radius * radius;
    }
}

And this is what I've come up with so far, (the methods were already declared but not implemented);

public class CircleList {
   private ArrayList<Circle> circles;

    /** Constructs an empty list for circles. */
    public CircleList() {
        circles = new ArrayList<Circle>();
    }

    /** Adds the circle c to the list if it not ovelaps
        with any circle in the list. Return true if 
        circle has been added. */ 
    public boolean add(Circle c) {
          circles.add(c); 
    }

    /** Returns true if the circle c ovelaps with any 
        one circle in this list. */
    public boolean overlaps(Circle c) {

    }

    private boolean circlesOverlaps(Circle c1, Circle c2) {
        double r1 = Circle.radius(c1); //radius, x- & y-value of circle 1 (c1)
        double x1 = Circle.getX(c1);
        double y1 = Circle.getY(c1);

        double r2 = Circle.radius(c2); //radius, x- & y-value för circle 2 (c2)
        double x2 = Circle.getX(c2);
        double y2 = Circle.getY(c2);
        double distance = Math.pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5);

    if (r2 >= r1 && distance <= (r2 - r1)){  //c1 is inside c2

    }
    else if (r1 >= r2 && distance <= (r1 - r2) ) { //c2 is inside c1

    }
    else if (distance > (r1 + r2)){ //c2 doesn't overlap c1

    }
    else { //c2 overlaps c1

    }


    }
}

I don't really know where to start. And I don't really have anyone to ask at the moment. I understand if this is too much to even start with. But thank you anyways.

Take care.

Upvotes: 1

Views: 357

Answers (1)

harsh
harsh

Reputation: 1551

Basically you have to iterate over all circles and check if the given circle overlaps with any like

public boolean overlaps(Circle c) {
        return circles.stream()
                .anyMatch(circle -> circlesOverlaps(c, circle));
    }

and you have to return true or false from your if conditions in circlesOverlaps method, depending on whether you consider one circle inside another as an overlap or not for your problem.

One other place you need to correct is

public boolean add(Circle c) {
      return circles.add(c); 
}

basically adding return as you need to return a boolean based on your return type.

Upvotes: 1

Related Questions