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()
     
    

    No comments:

    Post a Comment