def cookieExample(): # construct a christmas cookie with the help of the shapes star = Star(radius=2.0, center=(1.0, 3.0), beams=5, iradius=1.4) circle = Circle(radius=1.0, center=(1.0, 3.0), points=64) cookie = star - circle # shift star and circle to the right to plot all polygons # on one page star.shift(5.0, 0.0) circle.shift(10.0, 0.0) # plot all three to an svg file writeSVG('Cookie.svg', (cookie, star, circle)) # break a polygon object into a list of polygons by arranging # it on tiles # tile into 3x3 parts plist = tileEqual(cookie, 3, 3) writeSVG('CookieTiled1.svg', plist) # test tile at x = 0.3, 0.5 and y = 2.7, 3.1 plist = tile(cookie, [0.3, 0.5], [2.7, 3.1]) writeSVG('CookieTiled2.svg', plist) # let's simulate an explosion, move all parts away # from the cookie's center, small parts are faster xc, yc = cookie.center() for p in plist: if p: # speed/distance dval = 0.1 / p.area() x, y = p.center() # move the part a little bit p.shift(dval * (x - xc), dval * (y - yc)) # and rotate it slightly ;-) p.rotate(0.2 * math.pi * (random.random() - 0.5)) writeSVG('CookieExploded.svg', plist)
def cookieExample(): # construct a christmas cookie with the help of the shapes star = Star(radius=2.0, center=(1.0, 3.0), beams=5, iradius=1.4) circle = Circle(radius=1.0, center=(1.0, 3.0), points=64) cookie = star-circle # shift star and circle to the right to plot all polygons # on one page star.shift(5.0, 0.0) circle.shift(10.0, 0.0) # plot all three to an svg file writeSVG('Cookie.svg', (cookie, star, circle)) # break a polygon object into a list of polygons by arranging # it on tiles # tile into 3x3 parts plist = tileEqual(cookie, 3, 3) writeSVG('CookieTiled1.svg', plist) # test tile at x = 0.3, 0.5 and y = 2.7, 3.1 plist = tile(cookie, [0.3, 0.5], [2.7, 3.1]) writeSVG('CookieTiled2.svg', plist) # let's simulate an explosion, move all parts away # from the cookie's center, small parts are faster xc, yc = cookie.center() for p in plist: if p: # speed/distance dval = 0.1 / p.area() x, y = p.center() # move the part a little bit p.shift(dval*(x-xc), dval*(y-yc)) # and rotate it slightly ;-) p.rotate(0.2*math.pi*(random.random()-0.5)) writeSVG('CookieExploded.svg', plist)
def gridExample(): starGrid = cloneGrid(Star(beams=5), 0, 20, 20, 4, 4) starGrid.shift(-50, -50) cookie = Star(radius=30.0, beams=5, iradius=20.0) - Circle(radius=15.0) starCookie = cookie - starGrid writeSVG('StarCookie.svg', (starCookie,)) if hasPDFExport: writePDF('StarCookie.pdf', (starCookie,))
def moonExample(): # a high-resolution, softly flickering moon, # constructed by the difference of two stars ... moon = Star(radius=3, center=(1.0, 2.0), beams=140, iradius=2.90) \ - Star(radius=3, center=(-0.3, 2.0), beams=140, iradius=2.90) # plot the moon and its convex hull writeSVG('MoonAndHull.svg', (moon, convexHull(moon)), height=400, fill_opacity=(1.0, 0.3)) # test point containment d = ['outside', 'inside'] c = moon.center() print 'Did you know that the center of gravitation of my moon is %s?' % d[moon.isInside(c[0], c[1])]
def testCoverOverlap(self): p1 = Star(radius=1.0, beams=6) p2 = Polygon.Polygon(p1) p2.scale(0.9, 0.9) self.assertEqual(p1.covers(p2), 1) self.assertEqual(p1.overlaps(p2), 1) p2.shift(0.2, 0.2) self.assertEqual(p1.covers(p2), 0) self.assertEqual(p1.overlaps(p2), 1) p2.shift(5.0, 0.0) self.assertEqual(p1.covers(p2), 0) self.assertEqual(p1.overlaps(p2), 0)
def star(pos=(0,0), radius=1.0, n=5, iradius=None, rotate=0.0, thickness=0.0, roundness=0.0, invert=False, scale=1.0, xscale=1.0, yscale=1.0): if iradius == None: iradius = radius*0.5 if thickness == 0.0: pstar = Star(radius=radius, center=pos, beams=n, iradius=iradius) cp = pstar[0] cp.append(cp[0]) cp.reverse() # Polygon Star goes around clockwise, so reverse to go CCW cp = rotatecp(cp, pos, rotate) if scale != 1.0: xscale = yscale = scale pp = Polygon(cp) if xscale != 1.0 or yscale != 1.0: pp.scale(xscale,yscale) if roundness > 0: cp = roundc(pp.contour(0), roundness=roundness, invert=invert) return Polygon(cp) else: return pp else: pp = sframe(pos=pos, radius=radius, iradius=iradius, rotate=rotate, thickness=thickness, roundness=roundness, invert=invert, scale=scale, xscale=xscale, yscale=yscale, n=n) return pp
def setUp(self): star = Star(radius=2.0, center=(1.0, 3.0), beams=5, iradius=1.4) circle = Circle(radius=1.0, center=(1.0, 3.0), points=64) self.cookie = star-circle self.cont = [(0.0, 0.0), (2.0, 1.0), (1.0, 0.0), (-2.0, 1.0), (0.0, 0.0)]
def gnuplotExample(): cookie = Star(radius=2.0, center=(1.0, 3.0), beams=5, iradius=1.4) \ - Circle(radius=1.0, center=(1.0, 3.0)) writeGnuplot('cookie.gp', (cookie,)) writeGnuplotTriangles('cookieTri.gp', (cookie,))
def xmlExample(): cookie = Star(radius=2.0, center=(1.0, 3.0), beams=5, iradius=1.4) \ - Circle(radius=1.0, center=(1.0, 3.0)) writeXML('cookie.xml', (cookie,), withHeader=True) p = readXML('cookie.xml')
from Polygon import * from Polygon.Shapes import Circle, Star, Rectangle from Polygon.IO import writeSVG p = Circle(5.5, points=128) p -= Circle(4.4, points=128) p += Circle(3.3, points=128) p -= Circle(2.2, points=128) p += Star(1.1) l = Rectangle(4, 14) l.shift(-4.3, -10.5) r = p ^ l writeSVG('logo.svg', [r], width=800)