Thursday, June 02, 2011

The Flag

I decided to participate in Playing with Mathematica's "make a US flag using Mathematica" contest. Making the stripes and blue section is fairly trivial to do in Mathematica, so I won't go into that. But, I like my method for making the stars. There's a little bit of math here, so feel free to skip down to the pictures at the bottom.

First, note that a five-sided star, like that on the US flag, can be constructed as follows:

If you take a regular pentagon (thick black lines) and connect all the vertices (numbered blue dots), you sketch a star. Alternatively, if you start with one vertex, and, traveling in a counter-clockwise direction, skip a vertex and connect the two, and repeat you get the same thing (i.e. line from 1 to 3, then 3 to 5, then 5 to 2, then 2 to 4, then 4 to 1).

However, to sketch the perimeter of the star, you also need to include the lettered vertices of the smaller, red, pentagon which is formed by the inscribed star. To get the resulting star shape, you need to connect the vertices in order:

The way I did this in Mathematica was using complex notation. The corners of the pentagons in the Complex Plane are given by
where n is an integer from 0 to 4 and A is the length from the origin to a corner (I rotated them by pi/10 to straighten the thing out). If A for the outer pentagon is 1, then the inner one is
Then, to get the vertices in order, you just need to sort them in order of Argument. All this is pretty easy to do in Mathematica:
starVert = (#1[[Ordering[Arg[#1]]]] & )[Join[E^(I*(Pi/10))*Exp[2*2*Range[6]*Pi*(I/5)], (1/2)*(3 - Sqrt[5])*E^(I*(Pi + Pi/10))*Exp[2*Range[6]*Pi*(I/5)]]]

Then I just mapped Re[coords] to x and Im[coords] to y, tiled the stars on the flag, and:

But, I wanted to make the flag wave. To do that, I summed 7 time-dependent sin curves in 2D with (bounded) random wavevector and frequencies. A resulting surface looks like this, for example:
Then, I used Mathematica's Texture functionality to use the flag Graphics I created on the surface:
And then, I animated it (click through for the subtle effect):

This was the one I submitted for the contest. Since then, I've seen Eric Brodeur's amazing Mathematica flag:
which is far better than the one I submitted. I'm not sure why I decided to do a top-down view, rather than the side view (also, I'm not sure how he got rid of framing boxes around the surface). Anyway, it's been fun.


  1. Plot3D[x y, {x, -1, 1}, {y, -1, 1}, Boxed -> False, Axes -> False]

  2. Duh! Thanks. I was trying Framed->False.