Skip to content

Instantly share code, notes, and snippets.

@fospathi
Last active August 16, 2025 11:24
Show Gist options
  • Save fospathi/32bfabec7ea927fffd369df2f5ea7dd3 to your computer and use it in GitHub Desktop.
Save fospathi/32bfabec7ea927fffd369df2f5ea7dd3 to your computer and use it in GitHub Desktop.
Rotation around a line

Rotation around a line

Here, rotating around a line uses the concept of a change of basis. See Change of basis for further information.

Rotation around a line through the origin

The steps to perform a rotation around a line through the origin are

  1. Find any basis (x, y, z) such that the positive x-axis direction of the basis is codirectional with the direction of the line:

        |x.x|        |y.x|        |z.x|
    x = |x.y|    y = |y.y|    z = |z.y|
        |x.z|        |y.z|        |z.z|
    
  2. Perform a change of basis from the implied reference basis to the new basis:

        | x.x  x.y  x.z |
    B = | y.x  y.y  y.z |
        | z.x  z.y  z.z |
    
  3. Perform the rotation as a rotation around the x-axis:

        | 1     0         0    |
    R = | 0   cos(θ)   -sin(θ) |
        | 0   sin(θ)    cos(θ) |
    
  4. Perform a change of basis back to the implied reference basis:

          | x.x  y.x  z.x |
    B⁻¹ = | x.y  y.y  z.y |
          | x.z  y.z  z.z |
    

Combine these steps into one matrix M:

                M = B⁻¹ R  B

| m00  m01  m02 |   | x.x  y.x  z.x | | 1  0  0 | | x.x  x.y  x.z |
| m10  m11  m12 | = | x.y  y.y  z.y | | 0  c -s | | y.x  y.y  y.z |
| m20  m21  m22 |   | x.z  y.z  z.z | | 0  s  c | | z.x  z.y  z.z |

where

  c = cos(θ)
  s = sin(θ)

k10 = c*y.x - s*z.x
k11 = c*y.y - s*z.y
k12 = c*y.z - s*z.z
k20 = s*y.x + c*z.x
k21 = s*y.y + c*z.y
k22 = s*y.z + c*z.z

m00 = x.x*x.x + y.x*k10 + z.x*k20
m01 = x.x*x.y + y.x*k11 + z.x*k21
m02 = x.x*x.z + y.x*k12 + z.x*k22
m10 = x.y*x.x + y.y*k10 + z.y*k20
m11 = x.y*x.y + y.y*k11 + z.y*k21
m12 = x.y*x.z + y.y*k12 + z.y*k22
m20 = x.z*x.x + y.z*k10 + z.z*k20
m21 = x.z*x.y + y.z*k11 + z.z*k21
m22 = x.z*x.z + y.z*k12 + z.z*k22

Rotation around a general line

The steps to perform a rotation around a general line are

  1. Find any frame (x, y, z, o) such that the origin of the frame coincides with the line and the frame's positive x-axis direction is codirectional with the direction of the line:

        |x.x|        |y.x|        |z.x|        |o.x|
    x = |x.y|    y = |y.y|    z = |z.y|    o = |o.y|
        |x.z|        |y.z|        |z.z|        |o.z|
        | 1 |        | 1 |        | 1 |        | 1 |
    
  2. Perform a change of frame from the implied reference frame to the new frame:

    (Here we translate the new frame's origin to the implied reference frame's origin and then change to the new frame's basis.)

        | x.x  x.y  x.z  0 | | 1  0  0  -o.x |
    F = | y.x  y.y  y.z  0 | | 0  1  0  -o.y |
        | z.x  z.y  z.z  0 | | 0  0  1  -o.z |
        |  0    0    0   1 | | 0  0  0    1  |
    
        | x.x  x.y  x.z  -(x.x*o.x + x.y*o.y + x.z*o.z) |
      = | y.x  y.y  y.z  -(y.x*o.x + y.y*o.y + y.z*o.z) |
        | z.x  z.y  z.z  -(z.x*o.x + z.y*o.y + z.z*o.z) |
        |  0    0    0                  1               |
    
  3. Perform the rotation as a rotation around the x-axis:

        | 1     0         0      0 |
    R = | 0   cos(θ)   -sin(θ)   0 |
        | 0   sin(θ)    cos(θ)   0 |
        | 0     0         0      1 |
    
  4. Perform a change of frame back to the implied reference frame:

          | 1  0  0  o.x | | x.x  y.x  z.x  0 |
    F⁻¹ = | 0  1  0  o.y | | x.y  y.y  z.y  0 |
          | 0  0  1  o.z | | x.z  y.z  z.z  0 |
          | 0  0  0   1  | |  0    0    0   1 |
    
          | x.x  y.x  z.x  o.x |
        = | x.y  y.y  z.y  o.y |
          | x.z  y.z  z.z  o.z |
          |  0    0    0    1  |
    

Combine these steps into one matrix M:

M = F⁻¹ R  F

    | x.x y.x z.x o.x | | 1  0  0  0 | | x.x  x.y  x.z  t0 |
  = | x.y y.y z.y o.y | | 0  c -s  0 | | y.x  y.y  y.z  t1 |
    | x.z y.z z.z o.z | | 0  s  c  0 | | z.x  z.y  z.z  t2 |
    |  0   0   0   1  | | 0  0  0  1 | |  0    0    0   1  |

    | m00 m01 m02 m03 |
  = | m10 m11 m12 m13 |
    | m20 m21 m22 m23 |
    |  0   0   0   1  |

where

  c = cos(θ)
  s = sin(θ)

 t0 = -(x.x*o.x + x.y*o.y + x.z*o.z)
 t1 = -(y.x*o.x + y.y*o.y + y.z*o.z)
 t2 = -(z.x*o.x + z.y*o.y + z.z*o.z)

k10 = c*y.x - s*z.x
k11 = c*y.y - s*z.y
k12 = c*y.z - s*z.z
k13 = c*t1  - s*t2
k20 = s*y.x + c*z.x
k21 = s*y.y + c*z.y
k22 = s*y.z + c*z.z
k23 = s*t1  + c*t2

m00 = x.x*x.x + y.x*k10 + z.x*k20
m01 = x.x*x.y + y.x*k11 + z.x*k21
m02 = x.x*x.z + y.x*k12 + z.x*k22
m03 = x.x*t0  + y.x*k13 + z.x*k23 + o.x
m10 = x.y*x.x + y.y*k10 + z.y*k20
m11 = x.y*x.y + y.y*k11 + z.y*k21
m12 = x.y*x.z + y.y*k12 + z.y*k22
m13 = x.y*t0  + y.y*k13 + z.y*k23 + o.y
m20 = x.z*x.x + y.z*k10 + z.z*k20
m21 = x.z*x.y + y.z*k11 + z.z*k21
m22 = x.z*x.z + y.z*k12 + z.z*k22
m23 = x.z*t0  + y.z*k13 + z.z*k23 + o.z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment