def fractest(self, item, it=3): self.frac_choice = item if not self.F_c_v: self.F_c_v = True mb = Menubutton(self.fracsubframe, text="Iterations") mb.grid() mb.menu = Menu(mb, tearoff=0) mb["menu"] = mb.menu for x in range(3, 7): mb.menu.add_command( label="%s" % x, command=lambda x=x: self.fractest(self.frac_choice, x)) mb.pack(side=BOTTOM, anchor=W) if item == 1: sq = Shapes.RegPoly(150, 4) sq.set_center(175, 175) sq.vertices(0) draw.sierpinski_sq_start(self.fraccr, sq, it) if item == 2: draw.t_frac_start(self.fraccr, it) elif item == 0: sq = Shapes.RegPoly(160, 3) sq.set_center(175, 175) sq.vertices(0) draw.s_gasket_start(self.fraccr, sq, it)
def draw_PENROSE(cr, tri, l): #Draws Penrose N-sided regular polygon onto canvas with a stroke and no fill #Checks if possible to draw with given polygon side length and offset length if 0 >= tri.side_length - 4 * l * (math.cos(tri.inter_angle) + 1): print "INVALID" return else: inner_tri = Shapes.RegPoly( tri.side_length - 4 * l * (math.cos(tri.inter_angle) + 1), tri.num_sides) inner_tri.set_center(tri.centerx, tri.centery) inner_tri.vertices(tri.offset) point_tri = [] for y in range(tri.num_sides): point_tri.append(Shapes.RegPoly(l, tri.num_sides)) point_tri[y].set_center(tri.centerx+(tri.radius-point_tri[y].radius)*math.cos(tri.angle*y \ +tri.offset),tri.centery+(tri.radius-point_tri[y].radius)*math.sin(tri.angle*y+tri.offset)) point_tri[y].vertices(tri.offset) #draw_RegPoly(cr,point_tri[y]) #draw_RegPoly(cr,tri) draw_RegPoly(cr, inner_tri) cr.move_to(point_tri[0].points[tri.num_sides - 1][0], point_tri[0].points[tri.num_sides - 1][1]) cr.line_to(point_tri[0].points[1][0], point_tri[0].points[1][1]) for x in range(1, tri.num_sides): a = (x + tri.num_sides - 1) % tri.num_sides b = (x + 1) % tri.num_sides cr.line_to(point_tri[x].points[a][0], point_tri[x].points[a][1]) cr.line_to(point_tri[x].points[b][0], point_tri[x].points[b][1]) cr.close_path() cr.stroke() for z in range(tri.num_sides): a = (z + tri.num_sides - 1) % tri.num_sides b = (z + 1) % tri.num_sides angle = tri.inter_angle*(float((tri.num_sides-3))/(tri.num_sides-2) ) - \ (tri.inter_angle)*(z)*(float(2)/(tri.num_sides-2)) cr.move_to(inner_tri.points[b][0], inner_tri.points[b][1]) cr.rel_line_to(l * math.sin(angle), l * math.cos(angle)) cr.line_to(point_tri[z].points[a][0], point_tri[z].points[a][1]) cr.stroke()
def Penrose(): Length, Width = 1000, 1000 for x in range(3, 10): img = cairo.SVGSurface("Penrose%s.svg" % x, Length, Width) cr = cairo.Context(img) polygon = Shapes.RegPoly(333, x) polygon.set_center(Length / 2, Width / 2) draw_PENROSE(cr, polygon, 30) cr.show_page()
def sierpinski_sq_start(cr, sq, it): #Draws outer and center square of Sierpinski Carpet if it == 0: return draw_RegPoly(cr, sq) sq0 = Shapes.RegPoly(sq.side_length / 3, 4) sq0.set_center(sq.centerx, sq.centery) draw_RegPoly(cr, sq0) sierpinski_sq_rest(cr, sq0, it - 2)
def save_svg_pen(filename, num): img = cairo.SVGSurface("%s.svg" % filename, 500, 500) cr = cairo.Context(img) sh = Shapes.RegPoly(200, num) sh.set_radius = (200) sh.set_center(250, 250) sh.vertices(0) draw_PENROSE(cr, sh, 20) cr.show_page()
def sierpinski_sq_rest(cr, sq, it): #Draws remainder of the squares of Sierpinski Carpet if it == 0: return new_sq = Shapes.RegPoly(sq.side_length / 3, 4) for x in range(4): a = sq.side_length * math.cos(math.pi / 4 + x * math.pi / 2) b = sq.side_length * math.sin(math.pi / 4 + x * math.pi / 2) c = sq.radius * 2 * math.cos(x * math.pi / 2) d = sq.radius * 2 * math.sin(x * math.pi / 2) new_sq.set_center(sq.centerx + a, sq.centery + b) draw_RegPoly(cr, new_sq) sierpinski_sq_rest(cr, new_sq, it - 1) new_sq.set_center(sq.centerx + c, sq.centery + d) draw_RegPoly(cr, new_sq) sierpinski_sq_rest(cr, new_sq, it - 1)
def sierpinski_gasket(cr, sh, it): if it == 0: return else: draw_RegPoly(cr, sh) #side_l = float(sh.side_length/(sh.num_sides-1)) side_l = float(sh.side_length / 2) new_sh = Shapes.RegPoly(side_l, sh.num_sides) if sh.num_sides % 2 == 0: radius = 2 * new_sh.radius else: radius = 0 for x in range(sh.num_sides): theta = 2 * math.pi * x / sh.num_sides a = math.cos(theta) * radius b = math.sin(theta) * radius new_sh.set_center(sh.centerx + a, sh.centery + b) sierpinski_gasket(cr, new_sh, it - 1)
def main(): Length, Width = canvas_input() sides = int(raw_input("Enter number of sides: ")) side_l = int(raw_input("Enter Polygon side length:")) shape = Shapes.RegPoly(side_l, sides) size_check = min(Length, Width) while shape.radius > size_check: print "Shape too large!" side_l = int(raw_input("Enter Polygon side length:")) shape.set_side_len(side_l) offset = int(raw_input("Enter offset: ")) shape.set_center(Length / 2, Width / 2) img = cairo.SVGSurface("Penrose%s.svg" % sides, Length, Width) cr = cairo.Context(img) draw_PENROSE(cr, shape, offset) cr.show_page()