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.
163square_rotation: float = 0.0
When ‘q’ is pressed, increase the angle.
168def handle_inputs() -> None:
169 global square_rotation
170 if glfw.get_key(window, glfw.KEY_Q) == glfw.PRESS:
171 square_rotation += 0.1
...
Event Loop¶
In the previous chapter, this was the rendering code for the square.
244 glColor3f(0.0, 0.0, 1.0)
245 glBegin(GL_QUADS)
246 for ms in square:
247 ms_to_ndc: InvertibleFunction[Vector2D] = compose(
248 # camera space to NDC
249 uniform_scale(1.0 / 10.0),
250 # world space to camera space
251 inverse(translate(camera.position_ws)),
252 # model space to world space
253 compose(translate(paddle1.position),
254 rotate(paddle1.rotation)),
255 # square space to paddle 1 space
256 translate(Vector2D(x=2.0,
257 y=0.0)))
258 square_vector_ndc: Vector2D = ms_to_ndc(ms)
259 glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
260 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.
250 glColor3f(0.0, 0.0, 1.0)
251 glBegin(GL_QUADS)
252 for ms in square:
253 ms_to_ndc: InvertibleFunction[Vector2D] = compose(
254 # camera space to NDC
255 uniform_scale(1.0 / 10.0),
256 # world space to camera space
257 inverse(translate(camera.position_ws)),
258 # model space to world space
259 compose(translate(paddle1.position),
260 rotate(paddle1.rotation)),
261 # square space to paddle 1 space
262 compose(translate(Vector2D(x=2.0,
263 y=0.0)),
264 rotate(square_rotation)))
265 square_vector_ndc: Vector2D = ms_to_ndc(ms)
266 glVertex2f(square_vector_ndc.x, square_vector_ndc.y)
267 glEnd()
The author is getting really tired of having to look at all the different transformation functions repeatedly defined for each object being drawn.