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 ?**
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 ?**
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);
}
}