Rotations - Demo 07¶
Objective¶
Attempt to rotate the paddles around their center. Learn about rotations. This demo does not work correctly, because of a misunderstanding of how to interpret a sequence of transformations.

Demo 07¶
How to Execute¶
Load src/modelviewprojection/demo07.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 |
For another person’s explanation of the trigonometry of rotating in 2D, see
Rotate the Paddles About their Center¶
Besides translate and scale, the third main operation in computer graphics is to rotate an object.
Rotation Around Origin (0,0)¶
We can rotate an object around (0,0) by rotating all of the object’s vertices around (0,0).
In high school math, you will have learned about sin, cos, and tangent. Typically the angles are described on the unit circle, where a rotation starts from the positive x axis.
We can expand on this knowledge, allowing us to rotate all vertices, wherever they are, around the origin (0,0), by some angle \(\theta\).
Let’s take any arbitrary point
From high school geometry, remember that we can describe a Cartesian (x,y) point by its length r, and its cosine and sine of its angle \(\theta\).
Also remember that when calculating sine and cosine, because right triangles are proportional, that sine and cosine are preserved for a right triangle, even if the length sides are scaled up or down.
So we’ll make a right triangle on the unit circle, but we will remember the length of (x,y), which we’ll call “r”, and we’ll call the angle of (x,y) to be “\(\beta\)”. As a reminder, we want to rotate by a different angle, called “\(\theta\)”.
Before we can rotate by “\(\theta\)”, first we need to be able to rotate by 90 degrees, or \(\pi\)/2. So to rotate (cos(\(\beta\)), sin(\(\beta\))) by \(\pi\)/2, we get (cos(\(\beta\) + \(\pi\)/2), sin(\(\beta\) + \(\pi\)/2)).
Now let’s give each of those vertices new names, x’ and y’, for the purpose of ignoring details for now that we’ll return to later, just let we did for length “r” above.
Now forget about “\(\beta\)”, and remember that our goal is to rotate by angle “\(\theta\)”. Look at the picture below, while turning your head slightly to the left. x’ and y’ look just like our normal Cartesian plane and unit circle, combined with the “\(\theta\)”; it looks like what we already know from high school geometry.
So with this new frame of reference, we can rotate x’ by “\(\theta\)”, and draw a right triangle on the unit circle using this new frame of reference.
So the rotated point can be constructed by the following
Now that we’ve found the direction on the unit circle, we remember to make it length “r”.
Ok, we are now going to stop thinking about geometry, and we will only be thinking about algebra. Please don’t try to look at the formula and try to draw any diagrams.
Ok, now it is time to remember what the values that x’ and y’ are defined as.
A problem we have now is how to calculate cosine and sine of \(\beta\) + \(\pi\)/2, because we haven’t actually calculated \(\beta\); we’ve calculated sine and cosine of \(\beta\) by dividing the x value by the magnitude of the a, and the sine of \(\beta\) by dividing the y value by the magnitude of a.
We could try to take the inverse sine or inverse cosine of theta, but there is no need given properties of trigonometry.
Therefore
28@dataclass
29class Vector2D:
30 x: float #: The x-component of the 2D Vector
31 y: float #: The y-component of the 2D Vector
156def rotate_90_degrees() -> InvertibleFunction[Vector2D]:
157 def f(vector: Vector2D) -> Vector2D:
158 return Vector2D(-vector.y, vector.x)
159
160 def f_inv(vector: Vector2D) -> Vector2D:
161 return Vector2D(vector.y, -vector.x)
162
163 return InvertibleFunction(f, f_inv)
164
165
166def rotate(angle_in_radians: float) -> InvertibleFunction:
167 r90: InvertibleFunction[Vector2D] = rotate_90_degrees()
168
169 def f(vector: Vector2D) -> Vector2D:
170 parallel: Vector2D = math.cos(angle_in_radians) * vector
171 perpendicular: Vector2D = math.sin(angle_in_radians) * r90(vector)
172 return parallel + perpendicular
173
174 def f_inv(vector: Vector2D) -> Vector2D:
175 parallel: Vector2D = math.cos(angle_in_radians) * vector
176 perpendicular: Vector2D = math.sin(angle_in_radians) * inverse(r90)(
177 vector
178 )
179 return parallel + perpendicular
180
181 return InvertibleFunction(f, f_inv)
182
183
Note the definition of rotate, from the description above. cos and sin are defined in the math module.
113@dataclass
114class Paddle:
115 vertices: list[Vector2D]
116 color: Color3
117 position: Vector2D
118 rotation: float = 0.0
a rotation instance variable is defined, with a default value of 0
146def handle_movement_of_paddles() -> None:
147 global paddle1, paddle2
148
149 if glfw.get_key(window, glfw.KEY_S) == glfw.PRESS:
150 paddle1.position.y -= 1.0
151 if glfw.get_key(window, glfw.KEY_W) == glfw.PRESS:
152 paddle1.position.y += 1.0
153 if glfw.get_key(window, glfw.KEY_K) == glfw.PRESS:
154 paddle2.position.y -= 1.0
155 if glfw.get_key(window, glfw.KEY_I) == glfw.PRESS:
156 paddle2.position.y += 1.0
157
158 if glfw.get_key(window, glfw.KEY_A) == glfw.PRESS:
159 paddle1.rotation += 0.1
160 if glfw.get_key(window, glfw.KEY_D) == glfw.PRESS:
161 paddle1.rotation -= 0.1
162 if glfw.get_key(window, glfw.KEY_J) == glfw.PRESS:
163 paddle2.rotation += 0.1
164 if glfw.get_key(window, glfw.KEY_L) == glfw.PRESS:
165 paddle2.rotation -= 0.1
Cayley Graph¶

Code¶
The Event Loop¶
174while not glfw.window_should_close(window):
So to rotate paddle 1 about its center, we should translate to its position, and then rotate around the paddle’s center.
193 glColor3f(*astuple(paddle1.color))
194
195 glBegin(GL_QUADS)
196 for p1_v_ms in paddle1.vertices:
197 # doc-region-begin compose transformations on paddle 1
198 fn: InvertibleFunction[Vector2D] = compose(
199 uniform_scale(1.0 / 10.0),
200 rotate(paddle1.rotation),
201 translate(paddle1.position),
202 )
203 paddle1_vector_ndc: Vector2D = fn(p1_v_ms)
204 # doc-region-end compose transformations on paddle 1
205 glVertex2f(paddle1_vector_ndc.x, paddle1_vector_ndc.y)
206 glEnd()
...
Likewise, to rotate paddle 2 about its center, we should translate to its position, and then rotate around the paddle’s center.
210 glColor3f(*astuple(paddle2.color))
211
212 glBegin(GL_QUADS)
213 for p2_v_ms in paddle2.vertices:
214 fn: InvertibleFunction[Vector2D] = compose(
215 uniform_scale(1.0 / 10.0),
216 rotate(paddle2.rotation),
217 translate(paddle2.position),
218 )
219 paddle2_vector_ndc: Vector2D = fn(p2_v_ms)
220 glVertex2f(paddle2_vector_ndc.x, paddle2_vector_ndc.y)
221 glEnd()
Why it is Wrong¶
Turns out, our program doesn’t work as predicted, even though translate, scale, and rotate are all defined correctly. The paddles are not rotating around their center.
Let’s take a look in detail about what our paddle-space to world space transformations are doing.
198 fn: InvertibleFunction[Vector2D] = compose(
199 uniform_scale(1.0 / 10.0),
200 rotate(paddle1.rotation),
201 translate(paddle1.position),
202 )
203 paddle1_vector_ndc: Vector2D = fn(p1_v_ms)
See modelviewprojection.mathutils.compose
modelspace vertices
Translate
Reset the coordinate system
Modelspace¶
Rotate around World Spaces’s origin
Modelspace¶
Reset the coordinate system
Modelspace¶
Final world space coordinates
Modelspace¶
So then what the heck are we supposed to do in order to rotate around an object’s center? The next section provides a solution.