# Sierpinski Polygon

A Sierpinski triangle is the result of this line of thinking:

To draw a Sierpinski triangle just draw three smaller Sierpinski triangles. (Unless the shape is too small... then just draw a regular triangle).

What if we make the number 3 a parameter, :side, so that we create Sierpinski polygons: Sierpinski Squares, Sierpinski Pentagons, Sierpinski Hexagons, etc?

Then we'd have a shape known as the N-Flake.

to poly :sides :length repeat :sides [ fd :length rt 360/:sides ] end to poly_sier :sides :length :depth ifelse :depth = 0 [ poly :sides :length ] [ repeat :sides [ poly_sier :sides :length/2 :depth-1 fd :length rt 360/:sides ] ] end lt 90 poly_sier 3 190 5 setxy 400 200 poly_sier 4 130 4 setxy 600 200 poly_sier 5 100 4

As awesome as these shapes are, they don't really form a nice sequence. As the number of sides gets bigger, the shape turns into more and more of a splodgy blob, with polygons drawn over the top of polygons.

Looking at the algorithm, the chief suspect is that remaining constant, the number 2. Let's make that a configurable parameter, `:r`

, for `ratio`

. And try to work out an optimal value for it.

Here's the results of some experimentation with different values of `:r`

.

to poly :sides :length repeat :sides [ fd :length rt 360/:sides ] end to poly_sier :sides :length :depth :r ifelse :depth = 0 [ poly :sides :length ] [ repeat :sides [ poly_sier :sides :length/:r :depth-1 :r fd :length rt 360/:sides ] ] end setxy 50 500 poly_sier 3 190 5 2 setxy 350 230 poly_sier 4 190 4 2.5 setxy 50 300 poly_sier 5 190 3 2.62 setxy 320 400 poly_sier 6 100 3 3 setxy 550 200 poly_sier 7 100 3 3.3 setxy 500 450 poly_sier 8 100 3 3.415

Here's a table of my experimentally derived values of `:r, versus the number of`

:sides.

:sides | :r |
---|---|

3 | 2 |

4 | 2.5 |

5 | 2.62 |

6 | 3 |

7 | 3.3 |

8 | 3.415 |

What's the relationship between `:sides`

and `:r`

?

:r = f(:sides)

What is the equation inside this `f(:sides)`

?

I can see that `:r`

gets bigger as `:sides`

gets bigger. But not very quickly, and it appears to be slowing. And sometimes it neatly fits into integers. That's a pretty special property of the function. I tried drawing some figures on paper, looking at when the shapes touch, and quickly saw it was pretty complicated, so I went searching for answers online.

At the ultimate Sierpinski Triangle page he talks about "adjust(ing) the ratio until the corners match up," which is all I've done, but he doesn't allude to the actual formula for calculating `:r`

from `:sides`

.

But the wikipedia article on the N-Flake gives this formula for `:r`

:

$$r=2(1+\sum _{k=1}^{[n/4]}cos\frac{2\pi k}{n})$$

Where n = :sides, [n=4] means "the greatest integer less than or equal to n/4" (see this discussion). Cos is calulated in radians.

So, let's try and apply that to our `poly_sier`

logo program.

to poly :sides :length repeat :sides [ fd :length rt 360/:sides ] end to poly_sier :sides :length :depth :r ifelse :depth = 0 [ poly :sides :length ] [ repeat :sides [ poly_sier :sides :length/:r :depth-1 :r fd :length rt 360/:sides ] ] end to calculate_r :s make "x 0 make "k 0 make "sx :s /4 make "sx :sx - (1/2) ; this is a trick: round(x - 0.5) == floor(x) make "sx round(:sx) ; floor(s/4) repeat :sx [ make "k :k + 1 make "x :x + radcos(6.28 * :k / :s) ] make "r 2 * (1 + :x) output :r end setxy 50 500 poly_sier 3 190 5 calculate_r 3 setxy 350 230 poly_sier 4 190 4 calculate_r 4 setxy 50 300 poly_sier 5 190 3 calculate_r 5 setxy 320 400 poly_sier 6 100 3 calculate_r 6 setxy 550 200 poly_sier 7 100 3 calculate_r 7 setxy 500 450 poly_sier 8 100 3 calculate_r 8

One interesting thing is that the 4-sided Sierpinski polygon is now very boring looking, as it lines up all **too** well. The formula has given it a ratio of '2' - but any value between 2 and 3 will look better, with 2.5 about the best. I guess this is a case where aesthetics and mathematics have to agree to disagree.