3D Rotation Matrix - Graph 3D
This was a simple program I wrote that Displays 3D points. It does so by simply projecting a 3D (vector) into 2D.
The Jar file can be downloaded here: Graph3D.jar
The C/C++ version can be found here

Before looking at the source, let’s take a look at some of the fundamental mathematics behind the software.
rotations - Rotations in this software simple geometric transformations based around an unmoving center axis. Below are the three rotation matrices for each axis, X, Y, and Z, respectively. Every operation can be found in Transform.java below.
Rotate Around X,Y, or Z Axis
![]() |
![]() |
![]() |
It is also important to know that every point in our world is defined by:
![vec{p } = delim{[}{matrix{3}{1}{x y z}}{ ]} vec{p } = delim{[}{matrix{3}{1}{x y z}}{ ]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_971.5_b37f4b7621c7a8e878c47165d7a82672.png)
This can be seen in Point3D.java below. (Which is synonymous to a Vector in this application.)
Rotate Around Arbitrary Axis
The above rotations are all rotations about either the X,Y, or Z axises. But another common and more complicated rotation is to rotate Vector/Point A around Vector/Point B.
Here are the Steps:
1. the Vector that is being rotated around must be NORMALIZED.
this can be done very easily
1a. ![]() |
1b. ![]() |
2. next, using quaternions, perform the rotation.
Where:
2a. 
(
is in radians)
The rotation matrix evaluates to the following:
2b. ![rotAB = delim{[}{matrix{3}{3}{ (q0^2 + q1^2 - q2^2 - q3^2) {2(q1q2 - q0q3)} {2(q1q3 + q0q2)} {2(q2q1 + q0q3)} (q0^2 - q1^2 + q2^2 - q3^2) {2(q2q3 - q0q1)} {2(q3q1 - q0q2)} {2(q3q2 + q0q1)} {(q0^2 - q1^2 - q2^2 + q3^2)} }}{]} rotAB = delim{[}{matrix{3}{3}{ (q0^2 + q1^2 - q2^2 - q3^2) {2(q1q2 - q0q3)} {2(q1q3 + q0q2)} {2(q2q1 + q0q3)} (q0^2 - q1^2 + q2^2 - q3^2) {2(q2q3 - q0q1)} {2(q3q1 - q0q2)} {2(q3q2 + q0q1)} {(q0^2 - q1^2 - q2^2 + q3^2)} }}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_95d334d1cb8898933a43bf2429eb6902.png)
Example: Suppose we have point ![vec{p} = delim{[}{matrix{3}{1}{1 2 3}}{]} vec{p} = delim{[}{matrix{3}{1}{1 2 3}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_971.5_6e5df3fbc73e5cdb4afc1f80e840b529.png)
and we would like to rotate
by 30° around the X axis.
![vec{p}_{new} = (rotX)vec{p} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]}.delim{[}{matrix{3}{1}{x y z}}{]} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(30)} {-sin(30)} 0 {sin(30)} {cos(30)}}}{]}.delim{[}{matrix{3}{1}{1 2 3}}{]} vec{p}_{new} = (rotX)vec{p} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]}.delim{[}{matrix{3}{1}{x y z}}{]} = delim{[}{matrix{3}{3}{1 0 0 0 {cos(30)} {-sin(30)} 0 {sin(30)} {cos(30)}}}{]}.delim{[}{matrix{3}{1}{1 2 3}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_29bf15549cfab5d584da786e22323e1b.png)
And finally, to project the 3D points onto a 2D canvas after performing a rotation, a simple way is to simply ignore the Z coordinate and draw the point based on it’s X and Y coordinates. However this is assuming that you’re projecting it on to the screen as if you are looking straight at it.
The source code can be found below as well as being bundled into the Jar file.
Cube 3D - A simple 3D Cube engine that demonstrates the usage of ALL the above mentioned equations.
Transform3D.java - This is a simple version using a 3×3 matrix. Using a 4×4 matrix you can also store the translation information. This is useful when trying to program a skeleton represented by vectors.
(Also, check out Graph/Rotate4D here
Other included source:









![rotX = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]} rotX = delim{[}{matrix{3}{3}{1 0 0 0 {cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)}}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_bc19288dce04b8ed2a6361e370fff4d3.png)
![rotY = delim{[}{matrix{3}{3}{{cos(theta)} 0 {sin(theta)} 0 1 0 {-sin(theta)} 0 {cos(theta)}}}{]} rotY = delim{[}{matrix{3}{3}{{cos(theta)} 0 {sin(theta)} 0 1 0 {-sin(theta)} 0 {cos(theta)}}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_48f328ce40e3daa3b6b254993f00f539.png)
![rotZ = delim{[}{matrix{3}{3}{{cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)} 0 0 1 0}}{]} rotZ = delim{[}{matrix{3}{3}{{cos(theta)} {-sin(theta)} 0 {sin(theta)} {cos(theta)} 0 0 1 0}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_07348bdb67e7aa9dbaec5ba1f4444cf8.png)

![hat{p} = delim{[}{matrix{3}{1}{x/d y/d z/d}}{]} hat{p} = delim{[}{matrix{3}{1}{x/d y/d z/d}}{]}](http://ken-soft.com/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_941.5_da220b5a11df89470afe59ad29b530c6.png)
Thank you for sharing. I came to this site to read how things really are
[...] 3D Rotation Matrix - Graph 3D [...]
[...] 3D Rotation Matrix - Graph 3D [...]
Thanks for this very helpful page.
Please note that formula 2a is erroneous. The numbers on the matrix diagonal should all be squared, i.e. q0^2+q1^2-q2^2-q3^2 etc.
I haven’t used the programming code so I have no idea if the code is okay.
>iceblink
The code was correct, just my comments were wrong, and I wrote this blog based off my code comments, It has been updated. Thanks again.
[...] 3D Rotation Matrix - Graph 3D [...]