Rotate the Square - Demo 12¶
Objective¶
Rotate the square around its origin.

Demo 12¶
How to Execute¶
Load src/modelviewprojection/demo12.py in Spyder and hit the play button.
Move the Paddles using the Keyboard¶
Keyboard Input |
Action |
---|---|
w |
Move Left Paddle Up |
s |
Move Left Paddle Down |
k |
Move Right Paddle Down |
i |
Move Right Paddle Up |
d |
Increase Left Paddle’s Rotation |
a |
Decrease Left Paddle’s Rotation |
l |
Increase Right Paddle’s Rotation |
j |
Decrease Right Paddle’s Rotation |
UP |
Move the camera up, moving the objects down |
DOWN |
Move the camera down, moving the objects up |
LEFT |
Move the camera left, moving the objects right |
RIGHT |
Move the camera right, moving the objects left |
q |
Rotate the square around its center |
Description¶
Cayley Graph¶

Demo 12¶
Code¶
Make a variable to determine the angle that the square will be rotated.
134square_rotation: float = 0.0
When ‘q’ is pressed, increase the angle.
139def handle_inputs() -> None:
140 global square_rotation
141 if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
142 square_rotation += 0.1
...
Event Loop¶
In the previous chapter, this was the rendering code for the square.
222 GL.glColor3f(0.0, 0.0, 1.0)
223 GL.glBegin(GL.GL_QUADS)
224 for ms in square:
225 ms_to_ndc: mu2d.InvertibleFunction = mu2d.compose(
226 [
227 # camera space to NDC
228 mu2d.uniform_scale(1.0 / 10.0),
229 # world space to camera space
230 mu2d.inverse(mu2d.translate(camera.position_ws)),
231 # model space to world space
232 mu2d.compose(
233 [
234 mu2d.translate(paddle1.position),
235 mu2d.rotate(paddle1.rotation),
236 ]
237 ),
238 # square space to paddle 1 space
239 mu2d.translate(mu2d.Vector2D(x=2.0, y=0.0)),
240 ]
241 )
242 square_vector_ndc: mu2d.Vector2D = ms_to_ndc(ms)
243 GL.glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
244 GL.glEnd()
Since we just want to add one rotation at the end of the sequence of transformations from paddle 1 space to square space, just add a rotate call at the top.
224 GL.glColor3f(0.0, 0.0, 1.0)
225 GL.glBegin(GL.GL_QUADS)
226 for ms in square:
227 ms_to_ndc: mu2d.InvertibleFunction = mu2d.compose(
228 [
229 # camera space to NDC
230 mu2d.uniform_scale(1.0 / 10.0),
231 # world space to camera space
232 mu2d.inverse(mu2d.translate(camera.position_ws)),
233 # model space to world space
234 mu2d.compose(
235 [
236 mu2d.translate(paddle1.position),
237 mu2d.rotate(paddle1.rotation),
238 ]
239 ),
240 # square space to paddle 1 space
241 mu2d.compose(
242 [
243 mu2d.translate(mu2d.Vector2D(x=2.0, y=0.0)),
244 mu2d.rotate(square_rotation),
245 ]
246 ),
247 ]
248 )
249 square_vector_ndc: mu2d.Vector2D = ms_to_ndc(ms)
250 GL.glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
251 GL.glEnd()
The author is getting really tired of having to look at all the different transformation functions repeatedly defined for each object being drawn.