I have Circle structure :
public class Circle
{
private int x;
private int y;
private int radius;
private static final int color = Color.WHITE;
public Circle(int x, int y, int radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
public int getRadius()
{
return radius;
}
public int getY()
{
return y;
}
public int getX()
{
return x;
}
public int getColor()
{
return color;
}
}
and store them in Stack<Circle>
I want to generate a new circle that have no intersection of other Circles in Stack
Following this answer I do next :
public Circle generateNew(Stack<Circle> circles)
{
Circle circle = null;
while (circle == null)
{
int x0 = new Random().nextInt(mMaxWidth);
int y0 = new Random().nextInt(mMaxHeight);
int R0 = new Random().nextInt(5) * 25;
boolean isIntersect = !circles.isEmpty();
for (Circle c : circles)
{
int x1 = c.getX();
int y1 = c.getY();
int R1 = c.getRadius();
int d = (x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1);
if (((R0 - R1) * (R0 - R1) <= d && d <= (R0 + R1) * (R0 + R1)))
{
isIntersect = true;
}
}
if (!isIntersect)
{
circle = new Circle(x0, y0, R0);
}
}
return circle;
}
But it no working. What i'm doing wrong?
Log after try to generate second Circle :
x0 = 496
y0 = 487
R0 = 100
x1 = 1121
y1 = 188
R1 = 100