CIRCLE DEMO

Back to index

Code demonstrating four different circle drawing algorithms is presented:
  1. TRIG FUNCTIONS FROM CENTER POINT
  2. TRIG FUNCTIONS FROM PREVIOUS POINT
  3. COMPLEX NUMBERS FROM CENTER POINT
  4. COMPLEX NUMBERS FROM PREVIOUS POINT

TRIG FUNCTIONS FROM CENTER POINT:
#!/usr/bin/python # draws a circle using trig functions # based on rotation around center point from Tkinter import * import math import cmath c = Canvas(width=200, height=200) c.pack() center = 100, 100 radius = 50 c.create_oval(98,98,102,102,outline="blue", width=2) for t in range(0,360): angle = t*math.pi/180 x = center[0] + math.cos(angle)*radius y = center[1] + math.sin(angle)*radius c.create_line(x,y,x+1,y+1,fill='red',width=2) mainloop()
TRIG FUNCTIONS FROM PREVIOUS POINT:
#!/usr/bin/python # draws a circle using trig functions # extending from previous point on circumference from Tkinter import * import math import cmath c = Canvas(width=200, height=200) c.pack() sx = 40 sy = 100 c.create_oval(sx-2,sy-2,sx+2,sy+2,outline='blue',width=2) for t in range(270,630): nx = sx+math.cos(t*math.pi/180) ny = sy+math.sin(t*math.pi/180) c.create_line(sx,sy,nx,ny,fill='red',width=2) sx=nx sy=ny mainloop()
COMPLEX NUMBERS FROM CENTER POINT:
#!/usr/bin/python # draws a circle using complex numbers # based on rotation around center point from Tkinter import * import math import cmath c = Canvas(width=200, height=200) c.pack() start = 90*math.pi/180 center = 100, 100 c.create_oval(98,98,102,102,outline="blue",width=2) radius = 50 x = center[0] y = center[1]+radius for t in range(0,562): angle = (t*math.pi/180)/start offset = complex(center[0],center[1]) v = cmath.exp(angle*1j) * (complex(x,y) - offset) + offset c.create_line(v.real,v.imag,v.real+1,v.imag+1,fill='red',width=2) mainloop()
COMPLEX NUMBERS FROM PREVIOUS POINT:
#!/usr/bin/python # draws a circle using complex numbers # extending from previous point on circumference from Tkinter import * import math import cmath c = Canvas(width=400, height=200) c.pack() curr = 50,100 c.create_oval(48,98,52,102,outline="blue",width=2) for t in range(315,675): x = curr[0]+0.9 # radius value added y = curr[1]-0.9 # radius value added angle = t*math.pi/180 offset = complex(curr[0],curr[1]) v = cmath.exp(angle*1j) * (complex(x,y) - offset) + offset c.create_line(v.real,v.imag,v.real+1,v.imag+1,fill='red',width=2) curr = v.real,v.imag mainloop()

COMMENTS:

Although Tkinter provides a create_oval() function and so it is not necessary to use these algorithms in order to draw circles, it is the case that these algorithms can be modified to draw complex objects or to perform basic two-dimensional rotations.