Python | Ellipse (different polygons) in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows, etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications.
Kivy Tutorial – Learn Kivy with Examples.
Ellipse:
Ellipse is a vertex canvas instruction. It allows drawing of a regular polygon, or an arc based on them. In this article, we will see different polygons have different number of sides and also many vertices on circle.
As in Kivy, we can make polygons based on an ellipse. 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.
Note:
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 a translation by x of a and y of b. The width is 2a, and height is 2b.
N segments = N vertices
1) angle[i]= i*360/n ; i=0 to n
2) x[i]=a cos(angle[i])
3) y[i]=b sin(angle[i])
4) In kivy 12’o clock is 0 degree and increases clockwise but in math 3’o clock is 0 degree increases counter clockwise.
Program to show how we can make different polygon in kivy by Ellipse.
main.py file:
Python3
import kivy
kivy.require( "1.9.1" )
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
class Ellipsekv(GridLayout):
pass
class EllipseApp(App):
def build( self ):
return Ellipsekv()
if __name__ = = '__main__' :
EllipseApp().run()
|
Ellipse.kv file
Python3
<Ellipsekv>:
cols: 4
canvas:
Color:
rgb: 0 , 0 , 1
Rectangle:
pos: self .pos
size: self .size
RelativeLayout:
canvas:
Color:
rgb: 1 , . 8 , . 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 , . 59 , . 86
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: . 5 , . 5 , . 5
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
|
Output:
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.
Last Updated :
21 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...