Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Creating circles

Ok so I'm trying to create a recursive algorithm which results in series of circles being produced.

At present it can be seen that I have created the circle class and attempted to use recursion, however as you can probably tell I'm fairly new to all this.

Having now drawn all of the ovals in their correct locations I have included a Color object into each circle. My aim is to make it so that as the circles are produced their colors change, with each set of circles being a certain shade of green (as seen in the above example).

Presently, however the shades of green are applied to the incorrect circles. As is seen below:

If anyone is able to hazard a guess as to why this is happening I would be very grateful. Thanks.

like image 461
user3352349 Avatar asked Aug 17 '26 17:08

user3352349


1 Answers

Every call to createCircles() should paint one large circle in the center and call itself recursively 3 times for the 3 smaller circles. The y coordinate always remains the same and you can recalculate the x coordinate by adding and subtracting the radius of original circle.

public void createCircles(int x, int y, int rad) {

    Circle myCircle = new Circle(x, y, rad);
    circles.add(myCircle);

    createCircles(x - (2*rad), y, rad/3);
    createCircles(x, y, rad/3);
    createCircles(x + (2*rad), y, rad/3);
}

For the overflow error you can set a terminating condition on the size of rad, like

if (rad < 5) {
    return;
}
like image 150
Warlord Avatar answered Aug 19 '26 07:08

Warlord



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!