Reputation: 3452
I have a line declared with two points [x1,y1,x2,y2] and I have a new point (Nx,Ny) to which I have to first rotate the line and then move it towards it.
Here´s a picture to get it clear:
I have tried with this function but I can´t manage to accomplish the rotation, I´m using TkInter and Python:
def rotateLine(self,dx,dy): # x and y are the differences between x1,nx and y1,ny
angle= math.atan2(dy,dx)
print "angle",angle
newx = ((x1)*math.cos(angle)-(y1)*math.sin(angle))
newy = ((x1)*math.sin(angle)+(y1)*math.cos(angle))
self.panel.coords(self.robot,newx,newy,newx+10,newy+30) # I always add 10 in x and 30 in y to maintain de size of the line
self.panel.update()
Upvotes: 1
Views: 4060
Reputation: 183
I'm not familiar with TkInter, but by making an educated guess it seems that the inputs to self.panel.coords are the handle for the line and the four co-ordinates. By setting the co-ords to (x,y,x+10,y+30), you are always going to have a line segment of the same length AND angle, the only thing you are actually setting is the origin of the line.
Are you supposed to stretch the line from (x1,y1) to (nx,ny) or move the segment along the line between the points?
Also, when you calculate newx and newy, you need to centre it about the point (x1,y1). Therefore, each place you have (x1), you need (x2-x1), and similar for y1. You also then need to add x1 and y1 back in, because the formula you are using is for a rotation about the origin. The equations should then be
newx = ((x2-x1)*math.cos(angle)-(y2-y1)*math.sin(angle)) + x1
newy = ((x2-x1)*math.sin(angle)+(y2-y1)*math.cos(angle)) + y1
If the first thing you want to do is rotate the line segment towards the new point, then you should try
self.panel.coords(self.robot,x1,y1,newx,newy)
The preservation of the length of the line should have been preserved in your calculation of the new point. Moving the line segment is a simple matter of translating both points using the angle from the vertical and by the distance between (newx,newy) and (Nx,Ny).
Upvotes: 1