Fractal drawing circles

I am trying to do this: enter image description here

public void drawCircle(int x, int y, int diameter, int it) {
    int d = diameter / 3;
    if (it == 0) {
        return;
    }
    g.setColor(Color.green);
    g.fillOval(x, y, d, d);
    drawCircle(x, y, d, it--); // centre
    drawCircle(x - d, y, d, it--); // left
    drawCircle(x + d, y, d, it--); // right

}

For the purposes of this question, ignore the actual positions of those where I draw the circles - I will change this later, as this is not entirely correct. However, where am I mistaken in the logic of my program? It seems to me that I call the method of drawing three circles in each iteration. Here is my mistake:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at sun.java2d.loops.ProcessPath.ProcessMonotonicCubic(Unknown Source)
at sun.java2d.loops.ProcessPath.ProcessCubic(Unknown Source)
at sun.java2d.loops.ProcessPath.doProcessPath(Unknown Source)
at sun.java2d.loops.ProcessPath.fillPath(Unknown Source)
at sun.java2d.pipe.BufferedRenderPipe.fillPath(Unknown Source)
at sun.java2d.pipe.BufferedRenderPipe.fill(Unknown Source)
at sun.java2d.pipe.BufferedRenderPipe.fillOval(Unknown Source)
at sun.java2d.SunGraphics2D.fillOval(Unknown Source)
at Draw.drawCircle(Draw.java:40)
at Draw.drawCircle(Draw.java:41)

Here are lines 40 and 41:

g.fillOval(x, y, d, d);
drawCircle(x, y, d, it--); // centre

Thanks for the help.

+4
source share
1 answer

Your recursive call gets the same value itduring each recursive call, because it it--returns the value itbefore it decreases.

, it , it - 1 , .

drawCircle(x, y, d, it - 1); // centre
+4

Source: https://habr.com/ru/post/1528802/


All Articles