I am creating an application which rotates a few points around a center point. The aim is to connect each point using eg. lines/arcs and have the points/(subsequent drawing) rotate around the center point.
I am trying to accomplish this by a method which rotates each point by a given amount each time the method is called, then distributing each point n
times around the center point using a for
loop.
(For future use I will also need some tkinter widgets running along side the code eg. entries to get user-input.)
My current code simply draws a circle for each point, instead of connecting them. There are a couple of things I dont currently understand:
My code runs fine for a short while, then closes with
Error: maximum recursion depth exceeded.
- Is it bad to clear canvas by.delete
?The value of the
.after
function doesn't seem to have any effect at all hence usingtime.sleep
.
(I have also used a while True:
loop to run the code in an earlier version, but I read that it is bad practice to run an infinite loop inside the GUI event loop. And I edited it because of flickering)
Would it be better to structure my code differently? Sorry for any mis-terminology and the messy and long post/code, I am a new non-english python student.
class Create_gear:
def __init__(self, location, ox, oy, rpm, n):
self.location = location
self.ox = ox
self.oy = oy
self.rpm = rpm
self.n = n
self.rotation_from_normal = 0
#Rotates point px1, py1 by value of "rpm" each time method is called.
def draw_rotating_gear(self, px1, py1, px2, py2, r):
self.rotation_from_normal = self.rotation_from_normal +self.rpm
self.location.delete("all")
#rotates point px1, py1 n times around to form a circle.
for i in range (0, self.n):
angle = (math.radians(self.rotation_from_normal + 360/self.n *i) )
qx = ( self.ox + math.cos(angle) * (px1 - self.ox) - math.sin(angle) * (py1 - self.oy) )
qy = ( self.oy + math.sin(angle) * (px1 - self.ox) + math.cos(angle) * (py1 - self.oy) )
x0 = qx - r
y0 = qy - r
x1 = qx + r
y1 = qy + r
self.location.create_oval(x0, y0, x1, y1, fill = "black")
self.location.update()
time.sleep(0.01)
self.location.after(1000000000, self.draw_rotating_gear(480, 200, 500, 300, 5))