3D Graphics in Excel: Rotation


“3D graphics are computationally intensive”.

How many times have you heard this without really understanding what it means?  Well, I’m going to show you with a simple example of rotating a single point on a two-dimensional surface.

Have I lost your attention?  If I have then download THIS FILE and press the ‘Animate’ buttons on the two worksheets to see what I’m getting at.  If you think it’s pretty cool, then come back and read the rest of this blog to see how I did it.

In the first worksheet of the above linked file, we rotate point A to point B.  Because this is a rotation, the distance from The Origin (marked ‘O’ in the below diagram) doesn’t change, ie. the distance from O to the point A = the distance from O to the point B.

Rotation1

Now I’m going to take you through some mathematics.  To those who dislike maths, I apologise, but I’m afraid it’s unavoidable.  That said, it’s just geometry, trigonometry and algebra; High School stuff.  Familiarise yourself with the below diagram.

Rotation2

Recall that the (x, y) co-ordinates of any point (eg. A) on a Cartesian Plane can be expressed in terms of sine, cosine and its distance from The Origin (eg. the distance OA).  Now recall the trigonometric acronym “SOHCAHTOA” (Google it if you’ve forgotten).  We use SOHCAHTOA and a couple of trigonometric identities to express the rotation problem in linear (matrix) algebra.  From there, we use the MMULT function in Excel to find our “rotated” co-ordinates.

So, first to the trigonometry:

Rotation3

This gives us the rotated x co-ordinate (x’) expressed as a function of our beginning co-ordinates (x, y and z) and a “rotation vector”.  The above also gives us our rotated y co-ordinate (y’) in terms of our starting co-ordinates and a rotation vector.  For three dimensions, we have the additional “z” co-ordinate.  However, we’re only concerned with a change in (x, y) so our z co-ordinate is irrelevant and can be multiplied by a coefficient of zero in order to eliminate it.

Of course, in 3D rotations, the third dimension (z) IS important but as we’re just concerned with rotating in (x, y) space the value of z never changes.  Therefore, it should be multiplied by a coefficient of 1.

Rotation4

Since the value of z doesn’t change, we’re simply rotating about the z-axis.  You can think of this as the z-axis being a rod passing through The Origin and coming out of your screen (perpendicular to the screen) and you grabbing hold of it and rotating it like you would a spit on a rotiserie.  If you wanted to rotate around another axis, then the trigonometry remains the same (as it has done for millennia) and only the algebra changes.  Generate all 3 rotation matrices (one for each axis), multiply them together et voila! You have your 3D Rotation Matrix for rotating around a fixed point (The Origin) in any direction.

Now that you can do this, toute possible!  Take a look at “The Full Monty” in the above linked file which shows an example of rotating a tetrahedron in 3-dimensional space.  It’s a simple example and if you fancy something more challenging try rotating 8 different sized dots at different “speeds” (steps in a For loop) to get your very own model of the solar system.  Who said maths can’t be fun!

Enjoy!

 

Categories:VBA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: