#!/usr/bin/python import math import random from math import sqrt, sin, cos, atan2, pi def relprime(n,m): if m < n: return relprime(m,n) if n == 1 and m != 1: return True a = m % n if a == 0: return False else: return relprime(a,n) def frange(limit1, limit2 = None, increment = 1.): """ Range function that accepts floats (and integers). Usage: frange(-2, 2, 0.1) frange(10) frange(10, increment = 0.5) The returned value is an iterator. Use list(frange) for a list. """ if limit2 is None: limit2, limit1 = limit1, 0. else: limit1 = float(limit1) count = int(math.ceil(limit2 - limit1)/increment) return (limit1 + n*increment for n in range(count)) def drawepi(rstep, thstep, offset): print "% r's: " + repr(rstep) print "% th's: " + repr(thstep) print "% offset's: " + repr(offset) epsilon = 0.001 t = 0 initpoint = True while True: t += epsilon x = 0 y = 0 for s in range(len(rstep)): x += rstep[s]*cos(thstep[s]*t + offset[s]) y += rstep[s]*sin(thstep[s]*t + offset[s]) if initpoint: print "%f %f moveto" % (x,y) initpoint = False xinit = x yinit = y else: print "%f %f lineto" % (x,y) if t > 2*pi: break print "stroke" print "%!ps" print "300 300 translate" print "18 dup scale" print "0.05 setlinewidth" print "/Courier findfont 1 scalefont setfont" rstep = [10, random.uniform(-10,10),random.uniform(-7,7)] rsum = sum([abs(x) for x in rstep]) rstep = [x*10/rsum for x in rstep] sym = 8 thstep = [random.choice(range(1,sym))] #while not relprime(thstep[0], sym): # thstep = [random.choice(range(1,sym))] thstep.append(thstep[0] + sym*random.choice([-2,-1,1,2])) thstep.append(thstep[0] + sym*random.choice([-5,-4,-3,-2,-1,1,2,3,4,5])) offset = [0, random.choice([0,pi]), random.choice([0,pi])] print "%f %f %f sethsbcolor" % (0, 1, 0.7) # red drawepi(rstep, thstep, offset) print "%f %f %f sethsbcolor" % (0, 1, 0) # black print "-14 20 moveto (" + repr(rstep) + ") show" print "-14 19 moveto (" + repr(thstep) + ") show" print "-14 18 moveto (" + repr(offset) + ") show" #rstep = [10., 2.5, 1.2] #print "%f %f %f sethsbcolor" % (0.4, 1, 0.5) # green #drawepi(rstep, thstep, offset) # #rstep = [10., 2.5, 1.4] #print "%f %f %f sethsbcolor" % (0.6, 1, 1) # blue #drawepi(rstep, thstep, offset) print "showpage"