You are on page 1of 10

Rotation About an Arbitrary Axis in 3 Dimensions

Glenn Murray
June 6, 2013

1 Introduction
The problem of rotation about an arbitrary axis in three dimensions arises in many fields
including computer graphics and molecular simulation. In this article we give an algorithm
and matrices for doing the movement. Many of the results were initially obtained with
Mathematica.
An algorithm (See Figure 1):

Figure 1: Moving the axis of rotation A to the z-axis.

(1) Translate space so that the rotation axis passes through the origin.

1
(2) Rotate space about the z axis so that the rotation axis lies in the xz plane.
(3) Rotate space about the y axis so that the rotation axis lies along the z axis.
(4) Perform the desired rotation by θ about the z axis.
(5) Apply the inverse of step (3).
(6) Apply the inverse of step (2).
(7) Apply the inverse of step (1).
We will write our three-dimensional points in four homogeneous coordinates; i.e., (x, y, z)
will be written as (x, y, z, 1). This enables us to do coordinate transformations using 4x4
matrices. Note that these are really only necessary for translations, if we omitted translations
from our movements we could do the motions with 3x3 rotation matrices obtained by deleting
the last rows and last columns of the 4x4 matrices. In this article vectors are multiplied by
matrices on the vector’s left.

2 A translation matrix
The product TP1 ·v is equivalent to the vector sum h−a, −b, −c, 0i+v, i.e., this transformation
moves the point P1 (a, b, c) to the origin.
 
1 0 0 −a
0 1 0 −b 
TP1 = 0 0 1 −c 

0 0 0 1
 
1 0 0 −a
0 1 0 −b 
TP1 = 0 0 1 −c 

0 0 0 1

3 3D Coordinate axes rotation matrices


Here are the matrices for rotation by α around the x-axis, β around the y-axis, and γ around
the z-axis.
 
1 0 0 0
 0 cos α − sin α 0 
Rx (α) = 
 0 sin α cos α 0 

0 0 0 1
 
cos β 0 sin β 0
 0 1 0 0 
Ry (β) = 
 − sin β 0 cos β 0 

0 0 0 1

2
 
cos γ − sin γ 0 0
 sin γ cos γ 0 0 
Rz (γ) = 
 0

0 1 0 
0 0 0 1
The general rotation matrix depends on the order of rotations. The first matrix rotates
about x, then y, then z; the second rotates about z, then y, then x.

 
cos β cos γ cos γ sin α sin β − cos α sin γ cos α cos γ sin β + sin α sin γ 0
 cos β sin γ cos α cos γ + sin α sin β sin γ − cos γ sin α + cos α sin β sin γ 0 
Rz Ry Rx = 
 − sin β

cos β sin α cos α cos β 0 
0 0 0 1

 
cos β cos γ − cos β sin γ sin β 0
 cos α sin γ + sin α sin β cos γ cos α cos γ − sin α sin β sin γ − sin α cos β 0 
Rx Ry Rz = 
 sin α sin γ − cos α sin β cos γ sin α cos γ + cos α sin β sin γ cos α cos β

0 
0 0 0 1

4 Transformations for rotating a vector to the z -axis


In this section we introduce matrices to move a rotation vector hu, v, wi to the z-axis. Note
that we use the components to form expressions for the cosines and sines to avoid using
inverse trigonometric functions. We require that the rotation vector not be parallel to the
z-axis, else u = v = 0 and the denominators vanish.

4.1 The matrix to rotate a vector about the z -axis to the xz -plane
 √ √ 
u √u2 + v 2 v √u2 + v 2 0 0
 −v u2 + v 2 u u2 + v 2 0 0 
Txz = 
 0 0 1 0 
0 0 0 1

4.2 The matrix to rotate the vector in the xz -plane to the z -axis
 √ √ √ 
w u2 + v 2 + w 2 0 − u2 + v 2 u2 + v 2 + w2 0
√ √0 1 √ 0 0 

Tz = 
 u2 + v 2 u2 + v 2 + w2

0 w u2 + v 2 + w2 0 
0 0 0 1

5 Rotations about the origin


In this section we rotate the point (x, y, z) about the vector hu, v, wi by the angle θ.

3
5.1 The matrix for rotations about the origin
−1 −1
This is the product Txz Tz Rz (θ)Tz Txz .

 √ √ 
u2 +(v 2 +w2 ) cos θ uv(1−cos θ)−w u2 +v 2 +w2 sin θ uw(1−cos θ)+v u2 +v 2 +w2 sin θ
u2 +v 2 +w2 u2 +v 2 +w2 u2 +v 2 +w2 0
√ √
 
2 2 2
uv(1−cos θ)+w u2 +v 2 +w2 sin θ v +(u +w ) cos θ vw(1−cos θ)−u u2 +v 2 +w2 sin θ
 

 u2 +v 2 +w2 u2 +v 2 +w2 u2 +v 2 +w2 0
 √ √ 
 uw(1−cos θ)−v u2 +v 2 +w2 sin θ vw(1−cos θ)+u u2 +v 2 +w2 sin θ w2 +(u2 +v 2 ) cos θ 
 u2 +v 2 +w2 u2 +v 2 +w2 u2 +v 2 +w2 0
 
0 0 0 1

If we multiply this times hx, y, zi we can obtain a function of of seven variables that yields
the result of rotating the point (x, y, z) about the axis hu, v, wi by the angle θ.

f (x, y, z, u, v, w, θ) =
 √ 
u(ux+vy+wz)(1−cos θ)+(u2 +v 2 +w2 )x cos θ+ u2 +v 2 +w2 (−wy+vz) sin θ
u2 +v 2 +w2

 
v(ux+vy+wz)(1−cos θ)+(u2 +v 2 +w2 )y cos θ+ u2 +v 2 +w2 (wx−uz) sin θ
 
 
 u2 +v 2 +w2 
 √ 
w(ux+vy+wz)(1−cos θ)+(u2 +v 2 +w2 )z cos θ+ u2 +v 2 +w2 (−vx+uy) sin θ
u2 +v 2 +w2

5.2 The normalized matrix for rotations about the origin


At this point we would like to simplify the expressions by making the assumption that
hu, v, wi is a unit vector; i.e., that u2 + v 2 + w2 = 1. With this simplification, we obtain the
−1 −1
following expression for Txz Tz Rz (θ)Tz Txz .
 2
u + (1 − u2 ) cos θ

uv(1 − cos θ) − w sin θ uw(1 − cos θ) + v sin θ 0
 
uv(1 − cos θ) + w sin θ 2 2
 v + (1 − v ) cos θ vw(1 − cos θ) − u sin θ 0

 
uw(1 − cos θ) − v sin θ vw(1 − cos θ) + u sin θ 2 2
w + (1 − w ) cos θ
 
0
 
0 0 0 1

If we multiply this times hx, y, zi we can obtain a function of of seven variables that yields
the result of rotating the point (x, y, z) about the axis hu, v, wi (where u2 + v 2 + w2 = 1) by
the angle θ.

4
f (x, y, z, u, v, w, θ) =
 
u(ux + vy + wz)(1 − cos θ) + x cos θ + (−wy + vz) sin θ
 
 v(ux + vy + wz)(1 − cos θ) + y cos θ + (wx − uz) sin θ 
 
 
w(ux + vy + wz)(1 − cos θ) + z cos θ + (−vx + uy) sin θ

6 Rotation about an arbitrary line


We will define an arbitrary line by a point the line goes through and a direction vector. If
the axis of rotation is given by two points P1 = (a, b, c) and P2 = (d, e, f ), then a direction
vector can be obtained by hu, v, wi = hd − a, e − b, f − ci. We can now write a transformation
for the rotation of a point about this line.

6.1 The matrix for rotation about an arbitrary line


This is given by the product TP−1
1
−1 −1
Txz Tz Rz (θ)Tz Txz TP1 . In hopes of fitting the matrix onto
the page we make the substitution L = u2 + v 2 + w2 .

 √ √
 √ 
u2 +(v 2 +w2 ) cos θ uv(1−cos θ)−w L sin θ uw(1−cos θ)+v L sin θ a(v 2 +w2 )−u(bv+cw) (1−cos θ)+(bw−cv) L sin θ
 L L L L 

 
√ √

b(u2 +w2 )−v(au+cw) (1−cos θ)+(cu−aw) L sin θ 
 
uv(1−cos θ)+w L sin θ v 2 +(u2 +w2 ) cos θ vw(1−cos θ)−u L sin θ
 L L L L 
 

uw(1−cos θ)−v√L sin θ √
  
vw(1−cos θ)+u L sin θ w2 +(u2 +v 2 ) cos θ c(u2 +v 2 )−w(au+bv) (1−cos θ)+(av−bu) L sin θ 
 
 L L L L 
0 0 0 1

If we multiply this times hx, y, zi we can obtain a function of of ten variables that yields
the result of rotating the point (x, y, z) about the line through (a, b, c) with direction vector
hu, v, wi by the angle θ.
f (x, y, z, a, b, c, u, v, w, θ) =
  √ 
a(v 2 +w2 )−u(bv+cw−ux−vy−wz) (1−cos θ)+Lx cos θ+ L(−cv+bw−wy+vz) sin θ
 L 

2 2
 √ 
 b(u +w )−v(au+cw−ux−vy−wz) (1−cos θ)+Ly cos θ+ L(cu−aw+wx−uz) sin θ 
 
 L 

2 2
 √ 
c(u +v )−w(au+bv−ux−vy−wz) (1−cos θ)+Lz cos θ+ L(−bu+av−vx+uy) sin θ
L

5
6.2 The normalized matrix for rotation about an arbitrary line
Assuming that hu, v, wi is a unit vector so that L = 1, we obtain a more practical result for
TP−1
1
−1 −1
Txz Tz Rz (θ)Tz Txz TP1 .

u2 + (v 2 + w2 ) cos θ a(v 2 + w2 ) − u(bv + cw) (1 − cos θ) + (bw − cv) sin θ
 
uv(1 − cos θ) − w sin θ uw(1 − cos θ) + v sin θ
  
uv(1 − cos θ) + w sin θ v 2 + (u2 + w2 ) cos θ vw(1 − cos θ) − u sin θ b(u2 + w2 ) − v(au + cw) (1 − cos θ) + (cu − aw) sin θ
 
 
  
uw(1 − cos θ) − v sin θ
 vw(1 − cos θ) + u sin θ w2 + (u2 + v 2 ) cos θ c(u2 + v 2 ) − w(au + bv) (1 − cos θ) + (av − bu) sin θ 

 
0 0 0 1

If we multiply this times hx, y, zi we can obtain a function of of ten variables that yields
the result of rotating the point (x, y, z) about the line through (a, b, c) with direction vector
hu, v, wi (where u2 + v 2 + w2 = 1) by the angle θ.
f (x, y, z, a, b, c, u, v, w, θ) =
  
a(v 2 + w2 ) − u(bv + cw − ux − vy − wz) (1 − cos θ) + x cos θ + (−cv + bw − wy + vz) sin θ
  
2 2
b(u + w ) − v(au + cw − ux − vy − wz) (1 − cos θ) + y cos θ + (cu − aw + wx − uz) sin θ
 
 
 
2 2

c(u + v ) − w(au + bv − ux − vy − wz) (1 − cos θ) + z cos θ + (−bu + av − vx + uy) sin θ

7 Code and visualization


A graphic visualization of rotating a point about a line can be found at http://twist-and-shout.
appspot.com/.
Tested Java code for the matrices and formulas, released under the Apache license, is at
https://sites.google.com/site/glennmurray/Home/rotation-matrices-and-formulas.

6
How to Prepare Yourself for an Interview with Google
These tips and trick to help prepare for an interview with Google were sent to one of our
students who had an interview with them. The format of the interview is discussed and what to
expect while interviewing is also covered. At the end is a list of outside resources that would be
good to check out to help prepare and plan for an interview with Google.
Materials to Review before Scheduling your Phone Interview:

• an unofficial copy of your transcript


• at LEAST 3 days and times in which you would be available for the technical phone
interview including the time zone in the following format (ex: 6/12 - 10:00am - 2:00pm
PST)
• the programming language you would like to use for the interview: Java, C++, C or
Python
• the best contact number for the engineer to reach you the day of the interview
Google Interview Tips:
Here are some hints and tips to help you prepare for success! Those who study tend to do FAR
better on their interviews!

1) Plan ahead:
The Google engineers who will be interviewing you have only limited time set aside from their
projects, so please reserve time in your schedule, too! This also helps you relax and perform
better at interview. Please have a pen and paper handy in the case that you are asked to write
some things down.

2) What to Expect:
Up to a 45 minute technical interview with a Google software engineer.
The interviewer will be interested in your knowledge of computer science principles (data
structures, algorithms etc.) and how they can be used in your solutions.

3) Interview Questions:
Interview topics may cover anything on your CV (especially if you have stated that you are an
expert!), whiteboard coding questions, building and developing complex algorithms and analyzing
their performance characteristics, logic problems, systems design and core computer science
principles - hash tables, stacks, arrays, etc. Computer Science fundamentals are pre-requisite for
all engineering roles at Google, regardless of seniority, due to the complexities and global scale
of the projects you would end up participating in.

4) How to succeed:
At Google, we believe in collaboration and sharing ideas. Most importantly, you'll need more
information from the interviewer to analyze & answer the question to its full extent.
* It’s OK to question your interviewer.
* When asked to provide a solution, first define and frame the problem as you see it.
* If you don't understand - ask for help or clarification.
* If you need to assume something - verbally check it’s a correct assumption!
* Describe how you want to tackle solving each part of the question.
* Always let your interviewer know what you are thinking as he/she will be as interested in your
process of thought as your solution.
Also, if you're stuck, they may provide hints if they know what you're doing.
* Finally, listen - don't miss a hint if your interviewer is trying to assist you!
5) What is Google looking for?:
"We are not simply looking for engineers to solve the problems they already know the answers to;
we are interested in engineers who can work out the answers to questions they had not come
across before."

Interviewers will be looking at the approach to questions as much as the answer:


* Does the candidate listen carefully and comprehend the question?
* Are the correct questions asked before proceeding? (important!)
* Is brute force used to solve a problem? (not good!)
* Are things assumed without first checking? (not good!)
* Are hints heard and heeded?
* Is the candidate slow to comprehend / solve problems? (not good!)
* Does the candidate enjoy finding multiple solutions before choosing the best one?
* Are new ideas and methods of tackling a problem sought?
* Is the candidate inventive and flexible in their solutions and open to new ideas?
* Can questioning move up to more complex problem solving?

Google is keen to see really high quality, efficient, clear code without typing mistakes. Because all
engineers (at every level) collaborate throughout the Google code base, with an efficient code
review process, it’s essential that every engineer works at the same high standard.

6) Ask more questions!:


Make sure you have a decent understanding of Google as a business - further than Google's
main products - find out about what we do here:
http://www.google.com/corporate/ OR:
http://en.wikipedia.org/wiki/Google
At the end of the interview, most interviewers will ask you if you have any questions about the
company, work environment, their experience, etc. It’s clever to have some pre-prepared for each
interview, but don't worry too much if your mind goes blank.

If you have questions about the interview process, remuneration or your performance, please
direct these to your recruiter.

7) Further reading:
*To understand how Google's development teams work -
http://en.wikipedia.org/wiki/Agile_development
*To know more about Google's core projects - http://labs.google.com/why-google.html
If you have not already read through Steve Yegge's technical prep tips, please check out his blog:
http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html
*Due to the size of the products you'll be building, it’s imperative you're comfortable with big O
notation, here's where to brush up:
http://en.wikipedia.org/wiki/Big_o_notation

8) Technical Preparation tips!:


The main areas software engineers should prepare to succeed at interview at Google:

Algorithm Complexity: It's fairly critical that you understand big-O complexity analysis. Again
run some practice problems to get this down in application.

Sorting: Know how to sort. Don't do bubble-sort. You should know the details of at least one
n*log(n) sorting algorithm, preferably two (say, quicksort and merge sort). Merge sort can be
highly useful in situations where quicksort is impractical, so take a look at it.
Hashtables: Arguably the single most important data structure known to mankind. You absolutely
should know how they work. Be able to implement one using only arrays in your favorite
language, in about the space of one interview.

Trees: Know about trees; basic tree construction, traversal and manipulation algorithms.
Familiarize yourself with binary trees, n-ary trees, and trie-trees. Be familiar with at least one type
of balanced binary tree, whether it's a red/black tree, a splay tree or an AVL tree, and know how
it's implemented. Understand tree traversal algorithms: BFS and DFS, and know the difference
between inorder, postorder and preorder.

Graphs: Graphs are really important at Google. There are 3 basic ways to represent a graph in
memory (objects and pointers, matrix, and adjacency list); familiarize yourself with each
representation and its pros & cons. You should know the basic graph traversal algorithms:
breadth-first search and depth-first search. Know their computational complexity, their tradeoffs,
and how to implement them in real code. If you get a chance, try to study up on fancier
algorithms, such as Dijkstra and A*.

Other data structures: You should study up on as many other data structures and algorithms as
possible. You should especially know about the most famous classes of NP-complete problems,
such as traveling salesman and the knapsack problem, and be able to recognize them when an
interviewer asks you them in disguise. Find out what NP-complete means.

Mathematics: Some interviewers ask basic discrete math questions.


This is more prevalent at Google than at other companies because we are surrounded by
counting problems, probability problems, and other Discrete Math 101 situations. Spend some
time before the interview refreshing your memory on (or teaching yourself) the essentials of
combinatorics and probability. You should be familiar with n-choose-k problems and their ilk – the
more the better.

Operating Systems: Know about processes, threads and concurrency issues. Know about locks
and mutexes and semaphores and monitors and how they work. Know about deadlock and
livelock and how to avoid them. Know what resources a processes needs, and a thread needs,
and how context switching works, and how it's initiated by the operating system and underlying
hardware. Know a little about scheduling. The world is rapidly moving towards multi-core, so
know the fundamentals of "modern" concurrency constructs.

Coding: You should know at least one programming language really well, and it should
preferably be C++ or Java. C# is OK too, since it's pretty similar to Java. You will be expected to
write some code in at least some of your interviews. You will be expected to know a fair amount
of detail about your favorite programming language.

9) Sample Topics:
Coding Sample topics: construct / traverse data structures, implement system routines, distill
large data sets to single values, transform one data set to another.

Algorithm Design / Analysis


Sample topics: big-O analysis, sorting and hashing, handling obscenely large amounts of data.
Also see topics listed under 'Coding'.

System Design
Sample topics: features sets, interfaces, class hierarchies, designing a system under certain
constraints, simplicity and robustness, tradeoffs.

Open-Ended Discussion
Sample topics: biggest challenges faced, best/worst designs seen, performance analysis and
optimization, testing, ideas for improving existing products.
Helpful Videos for Preparing for an Interview with Google:
https://www.youtube.com/watch?v=w887NIa_V9w
https://www.youtube.com/watch?v=qc1owf2-220&feature=youtu.be
https://www.youtube.com/watch?v=oWbUtlUhwa8&feature=youtu.be

Helpful Resources for Preparing for an Interview/Job with Google:


https://www.google.com/about/careers/lifeatgoogle/hiringprocess/
http://www.google.com/about/careers/
https://www.youtube.com/user/lifeatgoogle
http://www.fastcompany.com/703050/google
http://www.wired.com/2014/08/how-to-solve-crazy-open-ended-google-interview-questions/

You might also like