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.


    Equally spaced circle in UI

    Pythonista
    5
    28
    10705
    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.
    • AZOM
      AZOM @mikael last edited by

      @mikael
      Yeah, thanks for all.

      1 Reply Last reply Reply Quote 0
      • mikael
        mikael @Guest last edited by mikael

        @adomanim, here you go. You need the vector.py from here.

        import ui
        import vector
        
        chars = 'ABCDEFGH'
        start_angle = 0 # First character on the right
        
        circle_color = 'red'
        char_color = 'white'
        char_font = ('Apple SD Gothic Neo', 32)
        
        diameter = min(ui.get_screen_size())/2
        
        root = ui.View()
        root.present()
        
        pointer = vector.Vector()
        pointer.magnitude = diameter/2
        pointer.degrees = start_angle
        
        for c in chars:
            label = ui.Label(
                text=c,
                text_color=char_color,
                alignment=ui.ALIGN_CENTER,
                font=char_font)
            label.center = root.bounds.center() + tuple(pointer)
            pointer.degrees += 360/len(chars)
            root.add_subview(label)
            
            
        class CircleView(ui.View):
            
            def layout(self):
                self.corner_radius = self.width/2
                
        circle = CircleView(
            width=diameter, height=diameter,
            border_width=1, border_color=circle_color,
            center = root.bounds.center()
        )
        
        root.add_subview(circle)
        circle.send_to_back()
        
        1 Reply Last reply Reply Quote 0
        • AZOM
          AZOM last edited by

          This is for my friend: can someone make a 3 points generator on the circumference of a circle that has a diameter of 2 units. And with these three x, y coordinates, look if the point (0,5:0) is in the triangle made with the three points?

          mikael 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @AZOM last edited by

            @AZOM, sounds like a school exercise. Wouldn’t it be more useful for your friend to spend the time on cracking it?

            AZOM 2 Replies Last reply Reply Quote 0
            • AZOM
              AZOM @mikael last edited by AZOM

              @mikael

              No, we are in high school, and this is not a homework or something like that, he just like maths and wants help to “verify” his answer to a question he gets somewhere on the web.

              1 Reply Last reply Reply Quote 0
              • AZOM
                AZOM @mikael last edited by AZOM

                @mikael

                The only thing I am doing wrong is with rounding up numbers (I think it is my only problem) beau a se if 2 values that I compare are like at 0,000001 appart, they are not equal. Also, I am using cos and sin to try to find the three points. And my friend doesn’t program btw.

                mikael 2 Replies Last reply Reply Quote 0
                • mikael
                  mikael @AZOM last edited by

                  @AZOM, try

                  0.1 + 0.2 == 0.3
                  

                  If you are not satisfied with the result, you might try to use the Decimal class, where you can also

                  getcontext().prec == 6
                  
                  1 Reply Last reply Reply Quote 0
                  • mikael
                    mikael @AZOM last edited by

                    @AZOM, meanwhile, for your problem, I would use ui.Path with the three points, then hit_test with the given point. I do not know whether this would be any more accurate than the method you are using, though.

                    AZOM 1 Reply Last reply Reply Quote 1
                    • AZOM
                      AZOM @mikael last edited by

                      @mikael

                      Right, now, I don’t have my program, but I’m going to send it at ~5h pm.

                      mikael 1 Reply Last reply Reply Quote 0
                      • mikael
                        mikael @AZOM last edited by

                        @AZOM, going to get my beauty sleep now, but here’s what I meant by the hit test:

                        import random
                        import ui
                        import vector
                        
                        random_angle = lambda: random.random() * 360
                        
                        p = ui.Path()
                        point = vector.Vector(0, 1)
                        
                        point.degrees = random_angle()
                        p.move_to(*point)
                        print(point)
                        for _ in range(2):
                            point.degrees = random_angle()
                            p.line_to(*point)
                            print(point)
                        p.close()
                            
                        print('IN' if p.hit_test(0.5,0) else 'OUT') 
                        
                        1 Reply Last reply Reply Quote 1
                        • JonB
                          JonB last edited by

                          So, are you trying to input 3 points (not all colinear) and find the circle (radius and x,y). Then want to determine if a fourth point is on the edge of the circle?

                          A simple method is to just subtract the center, then compute the length, then compare to the circle radius. But, as you found, you want to use abs(point_to_center_dist-circle_radius)< threshold, where you'll have to decide what is an acceptable threshold.

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