-3

Hey all

I have a program that draw a bubble anywhere by touching the screen (gesture detector), I want to handle an issue not to draw two bubbles at the same position by intersect method how can I do that ?**

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
mervat
  • 5
  • 1
  • private synchronized boolean intersects(float x, float y) { } – mervat Mar 07 '14 at 22:26
  • Can you maybe show what you have done so far? There are dozens of ways to do that – donfuxx Mar 07 '14 at 22:28
  • i want to check if the new drawn bubble's position is interesected with one already drawn or not ,any help! – mervat Mar 07 '14 at 22:29
  • public boolean onSingleTapConfirmed(MotionEvent event) { for(int i=0;i – mervat Mar 07 '14 at 22:31
  • If you want to add information to your question, especially snippets of code, you'll get a more readable result if you use the **edit** link under the question, than if you use the comment field. – Dawood ibn Kareem Mar 07 '14 at 22:47

1 Answers1

2

I'm assuming you have some kind of collection for your existing bubbles. Whenever you have a new bubble, use something like this to find out whether it intersects any of the bubbles in the collection. If not, then draw it and add it to the collection.

import java.util.Collection;

public class Bubble {
    private int centreX;
    private int centreY;
    private int radius;

    public Bubble(int centreX, int centreY, int radius) {
        this.centreX = centreX;
        this.centreY = centreY;
        this.radius = radius;
    }

    public boolean intersectsAny(Collection<Bubble> others){
        for (Bubble other : others) {
            if (intersects(other)) {
                return true;
            }
        }
        return false;
    }

    private boolean intersects(Bubble other) {
        int distanceSquared = (centreX - other.centreX) * (centreX - other.centreX)
            + (centreY - other.centreY) * (centreY - other.centreY);
        return distanceSquared <= (radius + other.radius) * (radius + other.radius);
    }
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110