Forum Archive

Fractal drawing using canvas doesn’t show drawing

iOSBrett

Hi,

I am playing around with some Fractal drawing using the canvas module. My scripts use recursion and I am finding that if I go to many levels deep (10ish) and “wide” then the output flashes up briefly and disappears. By “wide” l am referring to the fact that my recursion doubles each time I go down a level as it calls itself twice. The result is that the stack probably resembles the Fractal Tree I am trying to draw.

Adding begin_updates and end_updates just causes a crash.

Any advice on how I might get around this?

import random
import canvas
from math import pi

def drawBranch( angle, length):

if length < 10:
    return

canvas.draw_line( 0, 0, 0, length)
canvas.translate( 0, length)

canvas.save_gstate()
canvas.rotate( angle * pi / 180)
drawBranch( angle, length -10)
canvas.restore_gstate()

canvas.save_gstate()
canvas.rotate( - angle * pi / 180)
drawBranch( angle, length -10)
canvas.restore_gstate()

width = 800
height = 800
canvas.set_size( width, height)
canvas.translate( width / 2, 0)
drawBranch( 17, 120)

JonB

Canvas is mostly unsupported and has some quirks. You might have better luck using a ui.ImageContext, and use the ui drawing methods, then save ti an image or display in an ImageView.

iOSBrett

@JonB thanks, I didn’t realise it wasn’t really supported, good to know.

My day job is Swift/iOS, so doing the python thing as something fun, trying to keep away from the iOS specific stuff unless I really have to.

mikael

@iOSBrett, if your focus is on playing with Python the language, you could also consider optimizing the recursion.