# SierpinskiCarpet.frink

``` /** Draws Sierpinski carpets of various polygons. */ [n, levels] = eval[input["Sierpinski Carpet", [["Enter number of vertices: ", 4], ["Enter number of levels: ", 3]]]] anglestep = circle / n    // Angle between vertices theta2 = 1/2 anglestep    // half angle between vertices inner = 180 degrees (n-2) / n halfinner = 1/2 inner r = 1 theta3 = 180 degrees - theta2 - halfinner r2 = sin[halfinner] / sin[theta3] vertices = [ [0,0] ] g = new graphics for level = 0 to levels {    if level > 0       g.color[1,1,1]        startAngle = (n mod 2 == 1) ? 90 degrees + theta2 * (level mod 2) : theta2        newVertices = new array    for [cx, cy] = vertices    {       p = new filledPolygon       for vertex = 0 to n-1       {          // Draw the polygon          theta = anglestep * vertex + startAngle          x = r cos[theta] + cx          y = r sin[theta] + cy          p.addPoint[x,-y]          if (level > 0) and (level < levels)          {             // Add the next 2 polygon centers             newVertices.push[ [2 r cos[theta] + cx, 2 r sin[theta] + cy] ]             newVertices.push[ [2 r2 cos[theta + theta2] + cx, 2 r2 sin[theta + theta2] + cy] ]          }       }       g.add[p]    }    if level > 0       vertices = newVertices        r = 1/3 r    r2 = 1/3 r2 } g.show[] ```

This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 18663 days, 5 hours, 12 minutes ago.