Now we will see how to handle rotating lines. In any kind of graphic computer work, the need to rotate objects arises eventually. By starting off as simply as possible and progressively adding behaviors we can handle some increasingly complicated situations. This recipe is that first simple step in the art of making things rotate.
To understand the mathematics of rotation you need to be reasonably familiar with the trigonometry functions of sine, cosine, and tangent. The good news for those of us whose eyes glaze at the mention of trigonometry is that you can use these examples without understanding trigonometry. However, it is much more rewarding if you do try to figure out the math. It is like the difference between watching football or playing it. Only the players get fit.
You just need to write and run this code and observe the results as you did for all the other recipes. The insights come from repeated tinkering and hacking the code. Change the values of variables p1_x
to p2_y
one at a time and observe the results.
# rotate_line_1.py #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> from Tkinter import * import math root = Tk() root.title("Rotating line") cw = 220 # canvas width ch = 180 # canvas height chart_1 = Canvas(root, width=cw, height=ch, background="white") chart_1.grid(row=0, column=0) cycle_period = 50 # pause duration (milliseconds). p1_x = 90.0 # the pivot point p1_y = 90.0 # the pivot point, p2_x = 180.0 # the specific point to be rotated p2_y = 160.0 # the specific point to be rotated. a_radian = math.atan((p2_y - p1_y)/(p2_x - p1_x)) a_length = math.sqrt((p2_y - p1_y)*(p2_y - p1_y) + (p2_x - p1_x)*(p2_x - p1_x)) for i in range(1,300): # end the program after 300 position shifts a_radian +=0.05 # incremental rotation of 0.05 radians p1_x = p2_x - a_length * math.cos(a_radian) p1_y = p2_y - a_length * math.sin(a_radian) chart_1.create_line(p1_x, p1_y, p2_x, p2_y) chart_1.update() chart_1.after(cycle_period) chart_1.delete(ALL) root.mainloop()
In essence, all rotation comes down to the following:
For math students what you do is relocate the origin of your rectangular coordinate system to the pivot point, express the coordinates of your specific point into polar coordinates, add an increment to the angular position, and convert the new polar coordinate position into a fresh pair of rectangular coordinates. The preceding recipe performs all these actions.
The pivot point was purposely placed near the bottom corner of the canvas so that the point on the end of the line to be rotated would fall outside the canvas for much of the rotation process. The rotation continues without errors or bad behavior emphasizing a point made earlier in this chapter that Python is mathematically robust. However, we need to exercise care when using the arctangent
function math.atan()
because it flips from a value positive infinity to negative infinity as angles move through 90 and 270 degrees. Atan()
can give ambiguous results. Again the Python designers have taken care of business well by creating the math. atan2(y,x)
function that takes into account the signs of both y and x to give unambiguous results between 180 degrees and -180.
3.128.200.71