omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Fractal drawing using canvas doesn’t show drawing

    Pythonista
    3
    4
    2194
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • iOSBrett
      iOSBrett last edited by

      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)

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        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 1 Reply Last reply Reply Quote 0
        • iOSBrett
          iOSBrett @JonB last edited by

          @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 1 Reply Last reply Reply Quote 1
          • mikael
            mikael @iOSBrett last edited by

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

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors