Wednesday, June 4, 2014

14. Line

Line is a vertex canvas instruction. It allows drawing of lines through points.




The Python program first imports BoxLayout so the root class is based on this layout. The App import always has to be done. Then the math standard library is used. Three of the math functions will be used here: cos, sin, and radians. The function radians is imported as the alias rad. The function radians() converts the degrees to radians so cos and sin can be used.




Inside the build function, there is a nested function. The function, ellie(), is based on transversing the points, around an ellipse, centered at origin variable, orig. The ellipse height, is 2b, and ellipse width, is 2a. An ellipse goes from 0 to 360 degrees, however, here, start angle and stop angle can be specified, to create an arc. The number of segments determine the step angle. The List pts is returned, which has the length of 2 times segments plus 2.




The ellie() function is used to create four ellipses and two arcs. Note, orig, can either be a list or tuple. In Python, usually, the two, often, are interchangable. You can change the parameters to find a suitable set.


# ex14.py

from kivy.uix.boxlayout import BoxLayout
from kivy.app import App

from math import cos,sin,radians as rad

class Ex14(BoxLayout):
    pass
   
class Ex14App(App):
    def build(self):
        def ellie(a,b,orig,start_ang,stop_ang,seg):
            pts=[]
            step_ang=(stop_ang-start_ang)//seg
            for ang in range(start_ang,stop_ang+step_ang,step_ang):
                pts.extend([a*cos(rad(ang))+orig[0],
                            b*sin(rad(ang))+orig[1]])
            return pts
        
        self.Head=ellie(400,300,[400,300],-45,225,6)
        self.Chin=ellie(400,300,(400,300),225,315,3)
        self.Leye=ellie(60,40,[250,450],0,360,15)
        self.Reye=ellie(60,40,(550,450),0,360,15)
        self.Mouth=ellie(160,30,[400,200],0,360,25)
        self.Nose=ellie(5,50,(400,350),0,360,25)
        print(self.Chin)
        print(len(self.Chin))
        return Ex14()

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



In the kv file, the import directive is used to import the random module from the standard library. It is not imported with any alias. It is used to create a gray color with the red, green, and blue components, between .245 and .295. This gray color will be the background.




Now the lines corresponding to Head and Chin are drawn with the specified color. Here, two lists are joined to create a bigger list.




The rest of the Lines are drawn. We can not use the plus operator to join them, since that will draw an additional line connecting the different components, resulting in all joined shapes. Thus, the lines have to be seperate since you do not want these parts physically joined.


# ex14.kv
#:import random random

<Ex14>:
    canvas:
        Color:
            rgb: [.245+.05*random.random()]*3
        Rectangle:
            pos: self.pos
            size: self.size
        Color:
            rgb: 1,.1,.5
        Line:
            points: app.Head+app.Chin
            width: 3
        Color:
            rgb: 1,.5,.1
        Line:
            points: app.Leye
            width: 2
        Line:
            points: app.Reye
            width: 2
        Line:
            points: app.Mouth
            width: 2
        Line:
            points: app.Nose
            width: 2
    



This is the result. Lines, for the Head and Chin, are collectively 360 degrees, and form the outer, nine-sided polygon. The interior contains four ellipses as they all went from 0 to 360 degrees. They have enough segments to look curved. It will be a very simple modification, to add a rotate parameter, to the ellie() function, to give the ellipses a slant. We can also use the context rotate canvas instructions.




1 comment:

  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