0

I'm trying to draw fractal: for every circle displeyed I want to draw circle half it's size to the left and right of that circle. Here is my code:

let rec drawCircle x y r =
    let halfSize = r/2 in
        draw_circle x y r;
        drawCircle (x+r) y halfSize;
        drawCircle (x-r) y halfSize;;

It compiles but when I run it I get stack overflow. The question is why and how can I fix it in this function ?

1
  • Just to clarify Bor Laze's answer if you don't see what's meant: Your function will keep calling itself over and over again until you get a stack overflow. It calls itself (twice), and then those two invocations of the function call itself again (twice), and so on forever. Or: If you only want to draw two circles near every base level circle, it looks like you should replace the two instances of drawCircle in the body of the function, with draw_circle. Commented May 1, 2017 at 18:33

1 Answer 1

2

Your code doesn't have any conditions to terminate recursion.

For example, do not call drawCircle again when r == 1 pixel. Or any other case on your choice.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.