This is for a computer science assignment using Python, does anybody know where I would begin to create an algorithm to create a square or box that rolls across the screen? And I do indeed mean roll, not slide. It does not necessarily have to be using python, I just need a general idea as to how the coordinates would work and a general algorithm.
This is for a computer science assignment using Python, does anybody know where I
Share
If a unit square starts out with one side resting on the x-axis, and the lower right corner at (xs, 0), then after a quarter turn clockwise it will again have one side resting on the x-axis, and the lower right corner now at (xs+1, 0). Before it turns, label the lower left corner a; the upper left, b; and the upper right, c. Corners a and c move along arcs of a unit circle as the square turns. Corner b moves with radius d = sqrt(2).
This leads to the following method: Step angle t from 0 to pi/2 (ie 90°), letting
• xa = xs – cos t
• ya = sin t
• xb = xs – d*cos(t+pi/4)
• yb = d*sin(t+pi/4)
• xc = xs + sin t
• yc = cos t
At each time step, erase the old square by drawing background-colored lines, compute new (xa,ya,xb,yb,xc,yc) from equations, draw new square with lines from (xs,0) to (xa,yb) to (xc,yc) to (xd,yd) to (xs,0), and then delay appropriate amount. Each time t gets up to pi/2, set t back to 0 and add 1 to xs. Note, instead of erasing the whole square and then drawing the new, one might try erasing one old line and drawing one new line in turn for the four sides.