Friday, 6 April 2018

Python function -circle challenge 1 - Day 21(after 1 day)


  • this code will draw circle through function,by passing the center,radius

  •   
    #instead of using the for loop outside we can plot it in the parabol
    #funtion itself
    import math
    import tkinter
    
    def parabola(page,size):
        for x in range(-size,size):
            y = x * x/size
            plot(page,x,y)
    
    #the formula behind this is for circle it is enough to draw one quadrant and
    #then negating it the axis will provide the symmetry and make an circle
    #ie .. we have y = h + (math.sqrt(radius ** 2 -((x-g)**2)))
    #we want y = h - (math.sqrt(radius ** 2 -((x-g)**2)))
    #but minus y is actually -y = h - (math.sqrt(radius ** 2 -((x-g)**2)))
    #so we add 2 * h back onto -y
    def circle(page,radius,g,h):
        for x in range(g,g + radius):
            y = h + (math.sqrt(radius ** 2 -((x-g)**2)))
            plot(page,x,y)
            plot(page,x,2* h - y)
            plot(page,2 * g - x,y)
            plot(page,2 * g - x,2* h - y)
    
    def draw_axes(page):
        page.configure(scrollregion=(-200,-200,200,200))
        page.create_line(-200,0,200,0, fill="blue")
        page.create_line(0,200,0,-200, fill="blue")
    
    def plot(canvas,x,y):
        canvas.create_line(x,-y,x+1,-y+1,fill="red")
    
    mainwindow = tkinter.Tk()
    
    mainwindow.title("Parabola")
    mainwindow.geometry("640x480")
    
    canvas = tkinter.Canvas(mainwindow,width=640,height= 480)
    canvas.grid(row=0,column=0)
    
    
    print(id(canvas))
    
    draw_axes(canvas)
    parabola(canvas,100)
    parabola(canvas,150)
    
    circle(canvas,100,100,100)
    circle(canvas,100,100,-100)
    circle(canvas,100,-100,100)
    circle(canvas,100,-100,-100)
    circle(canvas,30,30,30)
    circle(canvas,30,30,-30)
    circle(canvas,30,-30,30)
    circle(canvas,30,-30,-30)
    
    # for x in range(-100,100):
    #     y = parabola(x)
    #     plot(canvas,x,-y)
    
    mainwindow.mainloop()
     
    

    Python function scope - Day 20(after 20 days)


  • this will explain how the scope of the variable works
  • here we are trying to plot the parabola by calling the function
  • we have used canvas variable one inside and another outside of the function to show how the scope works

  •   
    #this will explain the scope of the variable
    #we can use locals() to know the variable in that scopre,this can be used only inside functions
    
    import tkinter
    
    
    def parabola(x):
        y = x * x/100
        return y
    
    
    def draw_axes(canvas):
        #the canvas variable here is different from the canvas outside
        #of this function
        # canvas.update
        #here the x and y origin are not fetching properly so hardcoded the values
        # x_origin = canvas.winfo_width ()/2
        # y_origin = canvas.winfo_height()/2
        # x_origin = canvas.winfo_width ()
        # y_origin = canvas.winfo_height()
        canvas.configure(scrollregion=(-200,-200,200,200))
        canvas.create_line(-200,0,200,0, fill="blue")
        canvas.create_line(0,200,0,-200, fill="blue")
        #this will give the object location(address),we can differentiate object of same names
        #using this id()
        print(id(canvas))
        print(locals())
    
    def plot(canvas,x,y):
        canvas.create_line(x,y,x+1,y+1,fill="red")
    
    mainwindow = tkinter.Tk()
    
    mainwindow.title("Parabola")
    mainwindow.geometry("640x480")
    
    canvas = tkinter.Canvas(mainwindow,width=320,height= 480)
    canvas.grid(row=0,column=0)
    
    #we have created two canvas to show how the scope works
    canvas2 = tkinter.Canvas(mainwindow,width=320,height= 480)
    canvas2.grid(row=0,column=1)
    
    print(id(canvas))
    print(id(canvas2))
    
    draw_axes(canvas)
    draw_axes(canvas2)
    
    for x in range(-100,100):
        y = parabola(x)
        plot(canvas,x,-y)
    
    
    mainwindow.mainloop()