Skip to content
Tactiverge

Rotations in 2D

Describe turning with a rotation matrix, compose rotations by multiplying matrices, and learn the crucial difference between rotating a point and rotating a frame.

Lesson 3 of 6 · 35 min · Self-paced · Free

Why rotation needs its own tool

Robots turn constantly. A mobile robot changes its heading; a shoulder joint swings a whole arm; a camera on a pan head looks left and right. Every time something rotates, the coordinates of everything attached to it change. In the two-link arm lesson you handled this with sine and cosine for one link at a time, and by adding angles (θ₁ + θ₂) for the second link. That works for two links in a plane, but it gets unmanageable fast. Engineers instead package "rotate by θ" into a single object, a rotation matrix, that can be applied, combined and undone with simple rules. This lesson builds that object from what you already know.

Rotating a point, one axis at a time

Take a point p = (x, y) and rotate it anticlockwise about the origin by an angle θ. Where does it go?

The neat way to answer is to think about the two axis directions first.

  • The unit vector along x, (1, 0), rotated by θ, lands at (cos θ, sin θ). That is the definition of cosine and sine from the vectors lesson.
  • The unit vector along y, (0, 1), starts at 90°. Rotated by θ, it points at 90° + θ, which lands at (−sin θ, cos θ). Check with θ = 90°: (0, 1) should go to (−1, 0), and indeed (−sin 90°, cos 90°) = (−1, 0).

Now, any point is "x steps along the x direction plus y steps along the y direction". Rotation turns the whole plane rigidly, so after rotating, the point is x steps along the rotated x direction plus y steps along the rotated y direction:

x' = x · cos θ − y · sin θ
y' = x · sin θ + y · cos θ

That is the whole rule. It looks unfamiliar, but every piece came from the picture of two rotated arrows.

The rotation matrix

A matrix is a rectangular grid of numbers. The rule above is usually written as a 2 × 2 matrix multiplying a column of coordinates:

R(θ) = [ cos θ   −sin θ ]
       [ sin θ    cos θ ]

[ x' ]   [ cos θ   −sin θ ] [ x ]
[ y' ] = [ sin θ    cos θ ] [ y ]

To multiply a matrix by a column, take each row of the matrix in turn, multiply its entries by the matching entries of the column, and add. The first row gives x' = cos θ · x + (−sin θ) · y, the second gives y' = sin θ · x + cos θ · y, exactly as before.

Notice what the columns are: the first column is where (1, 0) goes, and the second column is where (0, 1) goes. That is a useful way to read any rotation matrix: its columns are the rotated axes.

Worked example

Rotate p = (2, 1) by 90°. With cos 90° = 0 and sin 90° = 1:

x' = 2 · 0 − 1 · 1 = −1
y' = 2 · 1 + 1 · 0 =  2

So (2, 1) goes to (−1, 2). Sketch it: a point to the right and slightly up swings round to the upper left. Now rotate (2, 1) by 30°, with cos 30° ≈ 0.866 and sin 30° = 0.5:

x' = 2 · 0.866 − 1 · 0.5 = 1.232
y' = 2 · 0.5   + 1 · 0.866 = 1.866

Check the length. Before: √(2² + 1²) = √5 ≈ 2.236. After: √(1.232² + 1.866²) = √(1.518 + 3.482) = √5.000 ≈ 2.236. Rotation never changes the distance from the origin, or the angle between two points. Robots exploit this as a free sanity check.

Here is the same calculation in Python. Remember that Python's trigonometry functions use radians:

import math

def rotate(point, degrees):
    """Rotate a 2D point anticlockwise about the origin."""
    t = math.radians(degrees)
    x, y = point
    return (x * math.cos(t) - y * math.sin(t),
            x * math.sin(t) + y * math.cos(t))

p = (2, 1)
for angle in [90, 30, -30, 360]:
    x, y = rotate(p, angle)
    print(f"{angle:4d} deg: ({x:6.3f}, {y:6.3f})  length {math.hypot(x, y):.3f}")
  90 deg: (-1.000,  2.000)  length 2.236
  30 deg: ( 1.232,  1.866)  length 2.236
 -30 deg: ( 2.232, -0.134)  length 2.236
 360 deg: ( 2.000,  1.000)  length 2.236

A negative angle rotates clockwise, and a full turn brings the point home, as it should.

Composing rotations

Rotate by 30°, then by 45°. Obviously the total is 75°. The matrix version of "then" is matrix multiplication: R(45°) · R(30°). Each entry of the product comes from a row of the first matrix and a column of the second, multiplied and added exactly as before. Working it through numerically gives:

R(45°) · R(30°) = [ 0.2588  −0.9659 ]  =  R(75°)
                  [ 0.9659   0.2588 ]

and cos 75° ≈ 0.2588, sin 75° ≈ 0.9659. So, in 2D:

R(a) · R(b) = R(a + b)

This is the matrix form of what you already did in the two-link arm: link 2 points along θ₁ + θ₂ because its rotation is the shoulder's rotation followed by the elbow's.

In 2D, the order of rotations does not matter, because all of them turn about the same axis. In 3D, order matters. Hold a book flat, turn it 90° about a vertical axis then 90° about a horizontal axis, and note where it ends up. Start again and do the two turns in the opposite order: the book finishes in a different orientation. That is one reason 3D robotics needs more care, and why getting comfortable with matrices now pays off later.

Undoing a rotation

To undo a rotation by θ, rotate by −θ. Because cos(−θ) = cos θ and sin(−θ) = −sin θ:

R(−θ) = [  cos θ   sin θ ]
        [ −sin θ   cos θ ]

This is R(θ) with its rows and columns swapped, called the transpose. So for rotation matrices, the inverse is simply the transpose. That is a special and very convenient property: no division, no risk of dividing by zero.

Rotating points versus rotating frames

This is the idea that causes the most bugs in real robot code, so take it slowly.

There are two different questions that both involve "rotating by θ":

  1. Rotating a point. The frame stays still and the object moves. "The arm swung 30° anticlockwise; where is the hand now?" Use R(θ).
  2. Rotating a frame. The object stays still and you turn. "My robot is facing 30° anticlockwise from the world's x-axis; where does this fixed object appear in my robot's own coordinates?" Use R(−θ).

Why the opposite sign? Sit in a swivel chair facing a door, then turn 30° to the left. The door has not moved, but from your point of view it is now 30° to your right. Turning the observer one way makes the world appear to turn the other way.

Worked example

A mobile robot sits at the world origin, facing 30° anticlockwise from the world x-axis. Its own frame has x pointing forward and y pointing to its left.

From robot to world. Its sensor sees a box 3 m straight ahead: (3, 0) in the robot frame. In world coordinates that is R(30°) applied to (3, 0):

x = 3 · 0.866 − 0 · 0.5 = 2.598
y = 3 · 0.5   + 0 · 0.866 = 1.500

From world to robot. A charging station is at world (4, 2). In the robot frame it is R(−30°) applied to (4, 2):

x = 4 · 0.866 + 2 · 0.5   = 4.464
y = −4 · 0.5  + 2 · 0.866 = −0.268

So the station is about 4.46 m ahead and 0.27 m to the robot's right (negative y is right). Check it by rotating back with R(30°): you should recover (4, 2). Doing that round trip is the best habit you can build.

Common mistakes

  • Degrees versus radians, again. Every trigonometry function in most languages expects radians.
  • The sign of the angle. Anticlockwise is positive in the standard convention. A different library or sensor may use clockwise; check its documentation.
  • Point versus frame. Ask "is the thing moving, or am I?" before choosing R(θ) or R(−θ).

Recap

A 2D rotation by θ sends (x, y) to (x cos θ − y sin θ, x sin θ + y cos θ), written as the matrix R(θ), whose columns are the rotated axes. Rotations preserve lengths. Doing one rotation after another multiplies their matrices, and in 2D the angles simply add. The inverse of a rotation is its transpose, R(−θ). Rotating a point uses R(θ); expressing a fixed point in a frame that has turned by θ uses R(−θ).

Project

Paper or Python project: rotate a shape

Draw a small triangle on squared paper with corners at (1, 0), (3, 0) and (3, 2).

  1. Rotate each corner by 90° using the rotation rule, and draw the new triangle.
  2. Rotate the original by 45°. Use cos 45° = sin 45° ≈ 0.707.
  3. Check that each side of the triangle has the same length before and after.
  4. Now imagine a robot at the origin facing 90°. Compute where the original triangle's corners appear in the robot's own frame, and explain why the answer differs from step 1.

You may use the Python rotate function from the lesson to check your hand calculations.

Deliverables

  • A drawing of the original, 90°-rotated and 45°-rotated triangles
  • Side lengths before and after each rotation
  • The corners in the robot's frame, with a one-sentence explanation of the sign change

Knowledge check

Check your understanding

  1. 1. Rotating the point (0, 3) by 90° anticlockwise about the origin gives:
  2. 2. What do the two columns of a 2D rotation matrix represent?
  3. 3. Which statement about R(20°) · R(50°) in 2D is correct?
  4. 4. How can you undo a 2D rotation matrix R(θ) most simply?
  5. 5. A robot at the origin turns to face 90° anticlockwise. A fixed object is at world (5, 0). Where is it in the robot's own frame (x forward, y left)?

Go further