Tag Archives: Mathematics


Rotating points...

Part of the Mathematica scripts that I've been writing require the generation of curved geometries and I've been spending a bit of time to work through different ways of achieving this.  My initial solutions were quite straightforward and made use of polar co-ordinates to create an array of points, which were then converted to cartesian co-ordinates at the end to make their inclusion in the analysis scripts a little easier.

Another way to do it makes use of complex numbers, but I haven't studied complex numbers for over 20 years and if I'm brutally honest I always struggled to work out what on earth they were really used for in the real world.  I've been through a few texts but the practical applications have always been glossed over.  One line of texts that I've found helpful for rediscovering lost maths skills, specifically for generating geometry, are mathematics books aimed at computer scientists rather than engineers (Vince, 2010).

So what's a complex number?  Essentially a complex number has a real part and an imaginary part and this was the part I used to struggle with as a maths student because I could never understand what use in the real world an imaginary thing could be.  If we could make an imaginary part useful then we could harness the power of unicorns and dragons surely?  This is where Vince (2010) has helped me decipher how complex numbers can be useful as the principles are used in geometry to rotate a point around another point.

\Large a = \underbrace b_{{\text{Real}}} + \underbrace {ci}_{{\text{Imaginary}}}

And to convert from a simple cartesian co-0rdinate into an equivalent complex number representation is quite straightforward too, you just use the relationship below.

\Large\left( {x,y} \right) = \left( {x + yi} \right)

The key concept to understand is that a cartesian co-ordinate can easily be converted to an equivalent complex number and to rotate this new co-ordinate by 90 degrees anti-clockwise you multiply this new complex number by i.  If you want to rotate by a further 90 degrees, then multiply this again by i.  One important thing to remember when going through this series of operations though is that i² can be simplified.

\Large{i^2} = - 1


Complex Number Figure v2

So for example if we wanted to take the co-ordinate at point A and rotate it through 90 degrees, we simply multiply the point by i, then simplify and we'll get the new co-ordinate for point B.  Here's the worked example for anyone that wants to follow along at home.

\Large A = \left( {1 + 2i} \right)

Rotate by 90 degrees, therefore multipy by i.

\Large\left({1 + 2i} \right)i = i + 2{i^2}

i² can be simplified to - 1 though

\Large{i + 2{i^2}} = i + 2\left( { - 1} \right)


\Large\left({ - 2 + i} \right)

Once you've followed the working above, why not have a go at rotating B through 90 degrees to get to point C, then again to get point D and then again to get back to point A?  Essentially multiplying the co-ordinates by i rotates the point 90 degrees in an anti-clockwise direction.

If you wanted to boil this down to a single expression to allow a programme to be coded this can be done quite easily and the expression is as set out below with the angle expressed in terms of degrees.  This expression, takes an existing point (x,y) and rotates it by an angle θ to get the new co-ordinate (x',y')

\Large \left( {x' + y'i} \right) = \left( {x + yi} \right).{\text{ }}{\Large e^{\frac{{2\pi i\theta }}{{360}}}}

Mathematica sample code.

Now we have this simplified expression, let's see how hard it is write a little bit of code to calculate the angle for us using complex numbers.  I don't pretend for a second this code is either elegant or efficient, but for new programmers I think it should be fairly easy to follow.  First we define a variable called coords to input a cartesian co-ordinate, this has to be in between curly brackets.

{-19.94, 392.11}

Next the angle that you wish to rotate between has to be defined, I've assigned this to a variable called angleRot for this example.  This angle has been entered in degrees.


The next stage applies the equation above for imaginary numbers on the first line of code, with the second line extracting the Real component using the Re[ ] function for the X co-ordinate, and the imaginary component using the Im[ ] function for the Y co-ordinate.

{284.977, 270.066}

This gives the new position based on the co-ordinate being rotated by an angle around the origin (0,0).

Rotate a point around another specified point.

It's just as straightforward to rotate a point around an arbitrary point rather than the origin it just requires an additional step that moves the co-ordinate back to the origin, then after the rotation has been completed the point is shifted back again by the same amount.

Rotation around a point

Enter co-ordinates as cartesian.

{3, 2}

Confirm the angle by which you want to rotate the co-ordinate by degrees. All angles are measured positive from 3 o'clock in the anticlockwise direction as per complex numbers standard.


Now you need to declare the point which you intend to rotate around.

{2, 1}

Before the rotate can begin you need to determine the distance between the origin and the rotate point, this will be the distance you have to translate the point being moved around before the rotation starts.

{-2, -1}

{1, 1}

The final step is to rotate the point around the origin and then move it back by the same amount that it was translated by before rotating.

{1, 2}


Vince, J. (2010). Mathematics for Computer Graphics (Third ed.). London: Springer.



Golden ratio...

A quick link through to my first ever screen case written for a Maths MOOC that's hopefully going live soon... this was much more hard work than I gave it credit for when I agreed to write it.

The slides are available from below as a slideshare link.

Teaching Tutorial


I was recently approached to see if I could tinker about and create a short video associated with the Fibonacci series and more specifically the golden ratio for a maths MOOC that's about to launch.  As per usual my inability to say no came into play and then I suddenly realised that I've never created a teaching video before and really what do I know about the Fibonacci series apart from fluffing about with a few Mathematica notebooks, and so blind panic set in... and with blind panic comes a quick trip to XKCD to check out the latest strips for a bit of inspiration...

XKCD Image

Now the golden ratio (φ) doesn't sound like the sort of everyday mathematics that you would use will pushing a trolley around TESCO's but the key thing you have to appreciate is that it's a very sneaky number.  Things are that are proportioned using the golden ratio are naturally and inexplicably appealing to the eye, we find the ratio a thing of beauty even though most people don't even know what it is.  It turns out that it's used in everything from composing pleasing photographs (using the rule of thirds as an approximation) through to designing the Parthenon in Greece which is proportioned using this ratio.  It's cool stuff, and to be frank if Mother Nature sees fit to use the principle to establish geometries in plants and other animals... who am I to argue?!

One of the things I like about XKCD are the graphs that they create, they have a certain charm about them and I love the style... and as sketchy as it looks, I've always struggled to replicate these diagrams using Visio or Omnigraffle.

XKCD Graph

One of my other digital haunts is the Stack Exchange forum, which as a (very) amateur coder I constantly find myself sitting reading through in a sense of amazement at some of the nifty tricks that people pull to make some quite elegant and funky code.  One of the sub-forums I like there is the Mathematica one, which recently asked the question if it was possible to create XKCD graphs using Mathematica, a copy of the thread is here.  I've replicated the code below, as much to save it for myself should the thread ever get closed.

The author of this code has created some sample images using this code and I think they're brilliant.

Sample image from the code author.

But coming back to the reason I needed to create some diagrams, the fibonacci sequence and the golden ratio... starting with the golden ratio this is defined by the equation below.

\varphi = \frac{{1 + \sqrt 5 }}{2}

\varphi = 1.61803...

The reason that this number is expressed as a fraction is that it is an irrational number, which means it's similar to π and the decimal places keep on repeating forever with no repetition and so it can't accurately be approximated a decimal.  So how would you approximate this number? Well to start with you need to understand the Fibonacci sequence, which sounds quite grand and complex, but is simple once you understand the trick to determine the sequence... essentially you add together the previous two numbers in the sequence and this gives you the next number.  So to start with 1+0=1;  1+1=2; 2+1=3; 3+2=5; etc etc.

{\text{1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...}}

But how does this get you to the golden ratio?  Well, if you divide one number, by the number before it you get a series of numbers that after a few iterations start to get close to the golden ratio (φ).


Which shows that the number starts to approximate towards the golden ratio with a reasonable level of accuracy after about the fifth iteration or so (well reasonable to a simple engineering brain anyway).. but let's not be blinded by numbers, let's draw an XKCD style graph using our new mathematica code...

The last ImageSize term is important to get a reasonable resolution on the image when you export it from Mathematica for embedding into other documents, but the code above gives the graph below... which I think is quite a good attempt at a XKCD style diagram.


Now that we can determine the Fibonacci sequence and the golden mean easily in Mathematica, we could start to see how these relate to the large sunflower seed heads that mother nature creates time after time...


Just a single line of code will help us generate some nice pretty Golden Ratio inspired spirals in Mathematica which look strangely familiar.


So it turns out that I did know enough about the Fibonacci sequence and the Golden Ratio to be almost dangerous... the trick's going to be can I record a short YouTube video for a maths MOOC as I promised to a half decent standard?  We'll have to wait and see... but now I know I can muck about with formatting diagrams and the code that goes along with the Golden Ratio in Mathematica I can start to mess around with tessalating spheres to create geodesic domes...

Geodesic Dome

And here's a Mathematica graphic after following the procedure set out in this blog post.


But I don't want to go off another tangent just yet with Bucky Fuller... but he was inspired by nature and the cleanness of the geometry that the golden ratio presented.