UPI Study › Courses › Discrete Mathematics › Free lessons › Lesson 12.3 Free with a free account
Rotation Matrices and Their Applications
Discrete Mathematics · Chapter 12: Matrices and TransformationsSection 12.3 · lesson 79 of 879 min · free with a free account
Free preview
Create a free account to keep watching
It takes a minute and costs nothing. Your free account unlocks all of Chapter 1 and 3 more lessons of Discrete Mathematics.
Lesson complete
Earn the 4 college credits for Discrete Mathematics
Enrolling adds a quiz after each lesson, the graded work and the official transcript. Recommended for credit by NCCRS.
Next lesson: Translation Matrices and Their Applications →← Matrix Operations and InversesNext lesson: Translation Matrices and Their Applications →
In this lesson
Turning points, vectors and whole shapes about the origin.
- Derive the 2D rotation matrix from the basis vectors
- Rotate points and whole polygons
- Show that rotation preserves distance
- Handle screen conventions in graphics code
Transcript
Everything said in the video, section by section (1,293 words). Read along, or search it.
Matrices and Transformations
Rotation Matrices and Their Applications
This section builds the matrix that rotates the plane. We'll derive it from scratch, use it on points and whole shapes, and see why it never changes distances. Then we'll look at the graphics systems that use it constantly, and one convention that trips programmers up.
The Opening Problem
Turning a Spaceship Sprite
A two D game engine redraws a player's spaceship sprite dozens of times every second. Every time the player steers, the sprite has to turn to face the new direction on screen. The engine never redraws the artwork from scratch at a new angle. Instead, it stores the sprite as a set of points, the corners of its outline. Then it recomputes where every one of those points should sit after turning through some angle. Taking a point, and finding where it lands after rotating about a fixed center, is exactly the operation this section formalizes.
12.3.1
Where the Basis Vectors Land
We want a two by two matrix, R of theta, that rotates any point counterclockwise by angle theta about the origin. Here's the key idea. A linear transformation is completely determined by what it does to the two standard basis vectors, e one and e two. And the matrix that represents it has those two images as its columns. So we only need to find where rotation sends e one and e two. E one, the point one, zero, sits at angle zero, at distance one. Rotate it by theta, and it lands at angle theta, distance one. By the definition of cosine and sine, that's the point cosine theta, sine theta. E two, the point zero, one, sits at ninety degrees. Rotating it puts it at ninety degrees plus theta. The angle-addition formulas simplify that. Cosine of ninety plus theta is minus sine theta, and sine of ninety plus theta is cosine theta. So e two lands at minus sine theta, cosine theta.
Definition
The 2D Rotation Matrix
Place the image of e one as the first column, and the image of e two as the second. That gives the rotation matrix. Its top row is cosine theta, minus sine theta. Its bottom row is sine theta, cosine theta. Applied to a point x, y, the rotated point has x prime equal to x cosine theta, minus y sine theta. And y prime equals x sine theta, plus y cosine theta. By convention, a positive theta means counterclockwise rotation, and a negative theta means clockwise. That matches the standard way angles are measured in the plane.
12.3.2
Checking It, Then Using It
First, a sanity check. At ninety degrees, cosine is zero and sine is one. Then e one goes to zero, one, and e two goes to minus one, zero. That's the classic quarter-turn rule: x, y goes to minus y, x. Now a general angle. Rotate the point four, two, by thirty degrees, where cosine is root three over two, and sine is one half. x prime is four times root three over two, minus two times one half. That's two root three, minus one. y prime is four times one half, plus two times root three over two. That's two plus root three. Numerically, the new point is about two point four six four, three point seven three two.
Distance Check
Rotation Never Changes Distance
Here's a check worth doing. Rotation should never change a point's distance from the origin. The original point four, two has squared distance sixteen plus four, which is twenty. For the rotated point, two root three minus one, squared, expands to thirteen minus four root three. And two plus root three, squared, is seven plus four root three. Add them, and the root three terms cancel, leaving twenty. The distance is unchanged, exactly as it should be. A transformation that preserves distances is called an isometry. Rotation changes a point's direction from the origin, but never its distance.
Figure 3
Rotating a Whole Sprite
To rotate a whole shape made of straight edges, apply the rotation to every vertex separately. The edges come along automatically, because a linear transformation sends straight lines to straight lines. Here's a triangular spaceship sprite pointing right. Its nose is at two, zero, and its back corners are at minus one, one, and minus one, minus one. Rotate each vertex by ninety degrees, using x, y goes to minus y, x. The nose moves to zero, two. Minus one, one moves to minus one, minus one. And minus one, minus one moves to one, minus one. The nose now points straight up, a quarter-turn from its original heading, exactly as expected.
Listing 1
Rotation in Code
Here's the formula in Python. The rotate point function converts the angle from degrees to radians, computes its cosine and sine, and applies the two formulas directly. Rotate polygon just applies the same rotation to every vertex in a list. Run it on the sprite's three vertices, at ninety degrees, and it returns zero, two, then minus one, minus one, then one, minus one. That matches the figure exactly.
Table 1
The Quarter-Turn Rotations
Four rotations are used constantly in practice, because sprites, icons and interface elements are so often turned in quarter-turn steps. At zero degrees, the matrix is the identity, and nothing changes. At ninety degrees, x, y goes to minus y, x, a quarter-turn counterclockwise. At one hundred eighty, it goes to minus x, minus y, a half-turn. And at two hundred seventy, it goes to y, minus x, a quarter-turn clockwise. These matrices contain only zero, one and minus one. They need no trigonometry at all, which is why graphics code often hard-codes them.
12.3.3
Which Way Is Positive?
Every two D graphics system, from game engines to image editors, design tools and CAD software, rotates objects this way. It represents the object as points, and applies the same matrix to each one. But there's a practical hazard. Which way is positive? Our derivation used the mathematical convention, where the y axis points up, and a positive angle turns counterclockwise. Many screen coordinate systems flip this. Pixel coordinates usually put the origin at the top-left corner, with y increasing downward. The formula still applies exactly as derived. But because the y axis itself is flipped, a positive angle now looks clockwise to the viewer. This isn't hypothetical. As documented by the Mozilla Developer Network, C S S's rotate function turns a positive angle clockwise on screen, in a left-to-right writing context.
Composition
Rotations Add, and Undo Cheaply
One more useful fact. Rotating by theta one and then by theta two is the same as rotating once, by their sum. That follows from the same angle-addition formulas. It's why a smoothly spinning sprite can be animated by applying a small fixed rotation every frame, rather than recomputing a large angle from scratch. Rotation matrices are also orthogonal, which means a rotation matrix's inverse equals its transpose. That's one algebraic reason rotations preserve distances. So back to the spaceship. The engine recomputes the sprite's three corners every frame, using this matrix. And if its rendering library uses screen coordinates, its programmers need the convention table, so that turn left on the controller actually turns the ship counterclockwise on screen.
Takeaways
Turn Every Point the Same Way
To summarize. The rotation matrix comes from where rotation sends the two standard basis vectors, using the angle-addition formulas. Applying it to a point gives x cosine theta minus y sine theta, and x sine theta plus y cosine theta. A whole shape rotates vertex by vertex. Rotation is an isometry, so it never changes distance from the origin. Quarter-turns need only zero, one and minus one. And screen systems may reverse the visual sense of a positive angle. Here's something to think about. Rotating by theta, then by minus theta, returns every point to where it started. So R of minus theta is the inverse of R of theta. Write out R of minus theta, and compare it entry by entry with the transpose of R of theta. What do you notice? And why might that matter to someone writing fast graphics code?
About Discrete Mathematics at UPI Study
Discrete Mathematics (Math 230) is a self-paced online course of 87 video lessons in 13 chapters, from logic and proof to counting, probability, graph theory and trees. It carries a NCCRS credit recommendation for 4 semester credits.
Every lesson transcript is free on pages like this one, and the videos are free with a free UPI Study account. Enrolling adds a practice quiz after each lesson, the graded work and final exam that earn the credit, lifetime access, and an official transcript on completion. UPI Study has helped over 130,000 students with college credits. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Enroll now → Course details Download the syllabus Every course with PRO →
Why take it here
Why students take Discrete Mathematics with UPI Study
Where the credit goes
Universities that work with NCCRS
Discrete Mathematics carries a NCCRS credit recommendation. These universities work with NCCRS; some are UPI Study's signed partners. ACE and NCCRS have over 2,100 cooperating colleges that will consider accepting credits; the final decision is with the universities.
Thomas Edison State UniversitySigned partner
University of the PeopleSigned partner
Charter Oak State CollegeSigned partner
Strayer UniversitySigned partner
Columbia Southern UniversitySigned partner
Indiana TechSigned partner
Penn StateWorks with NCCRS
Southern New Hampshire UniversityWorks with NCCRS
SUNYWorks with NCCRS
+many more cooperating universitiesFind my college →Find my college → Not listed? Ask Athena in the corner of this page, or book a 15-minute call.
FAQ
Questions students ask us
About Rotation Matrices and Their Applications
How do you derive the 2D rotation matrix from the basis vectors?
How do you rotate a point like (4, 2) by 30 degrees about the origin?
Why does a rotation preserve distance from the origin?
Why does a positive rotation angle look clockwise in graphics code?
About the Discrete Mathematics course
How many college credits is Discrete Mathematics?
How much does Discrete Mathematics cost and what is included?
Which universities accept UPI Study credits?
How long does Discrete Mathematics take?
How are the exams done?
How do my credits reach my college?
Ready for the full course? Earn 4 credits in Discrete Mathematics.
87 video lessons, a quiz after each one, and the graded work that earns the credit. Recommended for credit by NCCRS.
4 credits · NCCRSEnroll →