from idorudraw import line_from, line_to import math M = math def cell(x1, y1, x5, y5): vect = [(x5-x1), (y5-y1)] norm = [-vect[1], vect[0]] a = M.atan2(vect[1], vect[0]) len = M.sqrt(vect[0]**2 + vect[1]**2) height = (len/6.0)*M.sqrt(3.0) x2 = x1 + (vect[0])/3.0 y2 = y1 + (vect[1])/3.0 x3 = x1 + (vect[0])/2.0 +norm[0]*M.sqrt(3.0)/6.0 y3 = y1 + (vect[1])/2.0 + norm[1]*M.sqrt(3.0)/6.0 x4 = x1 + 2.0*(vect[0])/3.0 y4 = y1 + 2.0*(vect[1])/3.0 return [x1, y1, x2, y2, x3, y3, x4, y4, x5, y5] def curve(x1, y1, x2, y2, n = 4): line_from(x1, y1) s = cell(x1, y1, x2, y2) for i in range(1, n+1): r = s s = [] t = 4.0**i for j in range(0, t): s.extend(cell(r[j*2], r[j*2+1], r[j*2+2], r[j*2+3])) if(j < (t-1)): s.pop(); s.pop() for i in range(1, 4**(n+1) + 1): u = 2*i v = (2*i) + 1 line_to(s[u], s[v]) def flake(n = 4, l = 65.0): curve(-l*0.5, l*M.sqrt(3.0)/6.0, l*0.5, l*M.sqrt(3.0)/6.0, n) curve(l*0.5, l*M.sqrt(3.0)/6.0, 0.0, -l*M.sqrt(3)/3.0, n) curve(0.0, -l*M.sqrt(3)/3.0, -l*0.5, l*M.sqrt(3.0)/6.0, n)