Tuesday, June 3, 2014

13. Ellipse

Ellipse is a vertex canvas instruction. It allows drawing of a regular polygon, or an arc based on them.




This article shows regular polygons with different number of sides. They have many vertices on a circle.




However, in Kivy we can make polygons based on an ellipse. The area of an ellipse is smaller than a similarly sized rectangle.




This shows an ellipse, as well as what an ellipse, approximated by N = 8 segments looks like. We can see that as N increases, the shape approximates a true ellipse. The default in Kivy is 180.




These are the formulas for x and y. These math formulas assume the center is 0,0. Thus, after calculations are done, there has to be translation by x of a and y of b. The width is 2a, and height is 2b. Note, the angles in Kivy, are different than those in math. In Kivy, the 0 degree point, corresponds to the 90 degree-point, in math.




This shows an example when the number of segments is 6. We have 6 angles, and we have to find six x, and y coordinates. The first two are calculated here. After calculation, we have to translate. The first point becomes x of 800, and y of 300, after the translation.




This Python program shows the root widget is a GridLayout.


# ex13.py

from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class Ex13(GridLayout):
    pass
   
class Ex13App(App):
    def build(self):
        return Ex13()

if __name__=='__main__':
    Ex13App().run()



For the GridLayout, we have 4 columns. In this program, we use the set directive. Note directives, look like comments, but they are really commands. It sets a variable to an expression. Here, we just set them equal to different constants. These constants refer to what arc to cut for the figures in row2 and row3 of the Grid. The window is cleared, by painting with white color.




These instructions define the first two ellipses. We set a color for the row. The first ellipse has N = 180 (which is the default number of segments). The next one has 5 segments, which will be a pentagon.




The next 2 ellipses in the first row have segments of 4, and 3. This gives shapes of a square, and an equilateral triangle.




First, the color is set for the second row. These two arcs are created. For the arcs, we have to give the start, and the end angle. We use default number of segments, 180, and 5, for the two ellipse arcs. The rest of the kv file, corresponds, to the other, 6 ellipse arcs, following the same pattern.


# ex13.kv

#:set angle_start_row2 240 
#:set angle_end_row2 480
#:set angle_start_row3 120
#:set angle_end_row3 240

<Ex13>:
    cols:4
    canvas:
        Color:
            rgb: 1,1,1
        Rectangle:
            pos: self.pos
            size: self.size
    RelativeLayout:
        canvas:
            Color:
                rgb: 1,.1,.5
            Ellipse:
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                segments: 5
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                segments: 4
                pos: 0, 0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                segments: 3
                pos: 0, 0
                size: self.size

    RelativeLayout:
        canvas:
            Color:
                rgb: 1,.5,1
            Ellipse:
                angle_start: angle_start_row2
                angle_end: angle_end_row2
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row2
                angle_end: angle_end_row2
                segments: 5
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row2
                angle_end: angle_end_row2
                segments: 4
                pos: 0, 0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row2
                angle_end: angle_end_row2
                segments: 3
                pos: 0, 0
                size: self.size

    RelativeLayout:
        canvas:
            Color:
                rgb: 1,.5,0
            Ellipse:
                angle_start: angle_start_row3
                angle_end: angle_end_row3
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row3
                angle_end: angle_end_row3
                segments: 5
                pos: 0,0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row3
                angle_end: angle_end_row3
                segments: 4
                pos: 0, 0
                size: self.size
    RelativeLayout:
        canvas:
            Ellipse:
                angle_start: angle_start_row3
                angle_end: angle_end_row3
                segments: 3
                pos: 0, 0
                size: self.size



This is the result. We have 3 rows and 4 columns. Rows 2 and 3 are arc shapes, while Row1 has the default angles, 0 and 360 to form a complete circle. By changing the size of the window manually, we can get ovals, and shapes based on them. For the arc, the number of segments, corresponds to the number of lines that approximate the circular portion.




2 comments:

  1. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    Python Training in electronic city

    ReplyDelete