# Polygon2 / Polygon / Shapes.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93``` ```# -*- coding: utf-8 -*- from cPolygon import Polygon from math import sin, cos, pi def Circle(radius=1.0, center=(0.0,0.0), points=32): """ Create a polygonal approximation of a circle. :Arguments: - optional radius: float - optional center: point (2-tuple of float) - optional points: integer :Returns: new Polygon """ p = [] for i in range(points): a = 2.0*pi*float(i)/points p.append((center[0]+radius*sin(a), center[1]+radius*cos(a))) return Polygon(p) def Star(radius=1.0, center=(0.0,0.0), beams=16, iradius=0.5): """ Create a star shape, iradius is the inner and radius the outer radius. :Arguments: - optional radius: float - optional center: point (2-tuple of float) - optional beams: integer - optional iradius: float :Returns: new Polygon """ p = [] for i in range(beams): a = 2.0*pi*float(i)/beams p.append((center[0]+radius*sin(a), center[1]+radius*cos(a))) b = 2.0*pi*(float(i)+0.5)/beams p.append((center[0]+iradius*sin(b), center[1]+iradius*cos(b))) return Polygon(p) def Rectangle(xl= 1.0, yl=None): """ Create a rectangular shape. If yl is not set, a square is created. :Arguments: - optional xl: float - optional yl: float :Returns: new Polygon """ if yl is None: yl = xl return Polygon(((0.0, 0.0), (xl, 0.0), (xl, yl), (0.0, yl))) def __sierpinskiRec(c, box, level): """ recursive function to cut out parts of the sierpinski carpet """ if level <= 0: return w = (box[1]-box[0])/3.0 for i in range(3): for j in range(3): if i*j == 1: c.addContour(((box[0]+w, box[2]+w), (box[1]-w, box[2]+w), (box[1]-w, box[3]-w), (box[0]+w, box[3]-w)), True) else: xmin = box[0]+i*w xmax = xmin+w ymin = box[2]+j*w ymax = ymin+w __sierpinskiRec(c, (xmin, xmax, ymin, ymax), level-1) def SierpinskiCarpet(width=1.0, level=5): """ Create a sierpinski carpet. ### DO NOT USE LEVELS > 6 UNLESS YOU KNOW WHAT YOU DO! ### :Arguments: - optional width: float (1.0) - optional level: int (5) :Returns: new Polygon """ carpet = Rectangle(width) __sierpinskiRec(carpet, carpet.boundingBox(), level) return carpet ```