I have a script that creates Circle GraphicsObjects and applies newton's laws to them (gravitational pull), and moves each shape by its respective x,y velocity at each time step.
My goal is to draw a line between each of these circles and to move each line along with each circle every time step.
Circle.move(dx, dy) takes two inputs, but the same method for a line should have 4 inputs for each of the end points of the line. This method does not exist as far as I can tell.
The code in my project may be hard to digest so I added a sample of something similar. I would expect the code below to move the line like the hands of a clock (i.e., one point stays in the center, and the other end of the line rotates around the center in a circle).
I attempted to create a new version of the Line object and give it the move method, but this doesn't work as expected.
from graphics import *
import math, random, time
width = 500
height = 500
win = GraphWin("Example",width,height)
resolution = 10 # steps/lines per rotation
centerX = width/2
centerY = height/2
radius1 = 300
P1 = Point(centerX,centerY)
class newLine(Line):
def move(self, dx1, dy1, dx2, dy2):
self.p1.x = self.p1.x + dx1
self.p1.y = self.p1.y + dy1
self.p2.x = self.p2.x + dx2
self.p2.y = self.p2.y + dy2
circleX = centerX+radius1*math.cos(2*math.pi*0/resolution)
circleY = centerY+radius1*math.sin(2*math.pi*0/resolution)
P2 = Point(circleX,circleY)
L1 = newLine(P1,P2)
L1.draw(win)
for i in range(resolution):
circleX2 = centerX+radius1*math.cos(2*math.pi*(i+1)/resolution)
circleY2 = centerY+radius1*math.sin(2*math.pi*(i+1)/resolution)
dx = circleX2-circleX
dy = circleY2-circleY
L1.move(0,0,dx,dy)
time.sleep(0.2)
win.getMouse()
win.close()
First idea:
Remove old line using
undraw()and next create newLineand draw it.It can be the only method - because it uses
tkinter.Canvasand it can't rotate when it moves.Full working code
PEP 8 -- Style Guide for Python Code
BTW:
You can get full path to source code
and you can check how
move()works - maybe it use the same method.EDIT:
Digging in source code I got another idea - use
Polygonwhich has list of points and you can move last point - but it needs to calculatedx, dy- or simpler you can replace last point.But both methods needs ot use
undraw() + draw()to refresh screen.Teoretically
update()should refresh screen but it doesn't do it.or
EDIT:
After digging in source code I created
or
In similar way it can move end point.