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.


    How to draw a circle segment (pie) in Scene?

    Pythonista
    5
    16
    10266
    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.
    • Oscar
      Oscar last edited by Oscar

      Not the most straight forward bit of Code, but it works:

      def draw(self):
      	with ui.ImageContext(200, 200) as ctx:
      		path = ui.Path()
      		path.move_to(180, 100)
      		path.add_arc(100, 100,  80, 0, radians(170))
      		path.line_width = 5
      		ui.set_color('blue')
      		path.stroke()
      		ui_image = ctx.get_image()
      	pil_image = Image.open(io.BytesIO(ui_image.to_png()))
      	scene_image = load_pil_image(pil_image)
      	image(scene_image)
      
      1 Reply Last reply Reply Quote 0
      • Webmaster4o
        Webmaster4o last edited by

        I have also been frustrated in the past by the lack of drawing functionality in scene. It lacks simple functions like drawing a polygon from a list of points. I'd love for scene to have all the functionality of PIL.ImageDraw.

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

          I think that right now the way to do it is using a PathNode. The old way of doing explicit drawing is not very efficient.

          Edit: I meant ShapeNode when I wrote PathNode...

          1 Reply Last reply Reply Quote 1
          • omz
            omz last edited by omz

            You can either use a ShapeNode if you're using the new scene API, or the triangle_strip function (also new in 2.0) if you're working in "legacy mode".

            1 Reply Last reply Reply Quote 1
            • Olaf
              Olaf last edited by

              Example:

              # coding: utf-8
              
              import math, scene, ui
              
              def circle_segment_path(r, angle):
                  '''Path for circle segment (i.e. 'pizza slice') of radius r and angle degrees'''
                  path = ui.Path()
                  path.move_to(0, 0)
                  path.line_to(r, 0)
                  path.add_arc(0, 0, r, 0, -math.radians(angle), False)
                  path.line_to(0, 0)
                  return path
                  
              def circle_segment_shape(point, r, angle):
                  '''Blue & red shape for circle segment of radius r and angle degrees at point'''
                  return scene.ShapeNode(path=circle_segment_path(r, angle),
                                         fill_color='blue', stroke_color='red', position=point)
              
              class MyScene (scene.Scene):
                  def setup(self):
                      self.add_child(circle_segment_shape(self.size/2, 200, 45))
              
              if __name__ == '__main__':
                  scene.run(MyScene(), show_fps=True)```
              1 Reply Last reply Reply Quote 0
              • Olaf
                Olaf last edited by

                Or if you want more 'pizza':

                class MyScene (scene.Scene):
                    def setup(self):
                        for x in xrange(0, int(self.size.w)+50, 50):
                            for y in xrange(0, int(self.size.h)+50, 50):            
                                self.add_child(circle_segment_shape((x, y), 50, 45))
                
                1 Reply Last reply Reply Quote 0
                • upwart
                  upwart last edited by

                  @omz
                  I am trying to work out how to use triangle_strip, but I can't get it to work. For now all I want to do is draw a (solid) triangle.
                  If I try triangle_strip(((0,0),(10,0),(0,10))) I get a "Type error: expected sequence". Same if I close the sequence with a (0,0) tuple.

                  An example would be very helpful.

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

                    Thanks for all thoce nice examples with the new node objects. But for me the classical render loop is a must, because what am actuelly doing is prototyping an animation film that finally will be ported to Python on a Windows machine, most likely with PyGame or just plain PIL. Therefor the node objects are not an option to me, although I really like the concept.

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

                      The list of points in your example is not strictly a list, it is a tuple of tuples.

                      I haven't tried it myself, but does triangle_strip([(0,0),(10,0),(0,10)]) work?

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

                        @Oscar
                        No also the list of tuples as you suggest does not work. Alas.

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

                          Although the documentation states scene_drawing.triangle_strip(points[, tex_coords, image_name]), it seems tex_coords are mandatory (and of equal length as points). image_name is optional. I don't fully comprehend the logic at this time.
                          scene_drawing contains a call to triangle_strip in image_quad.

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

                            @Olaf
                            Super! I just added a list of tuples with random values and voilà it works!
                            Thanks.

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