Advertisement
phjoe

Sierpinski

Jan 23rd, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import appuifw as A
  2. import graphics as G
  3.  
  4. lock,run=A.e32.Ao_lock(),1
  5. w,h=G.sysinfo.display_pixels()
  6. img=G.Image.new((w,h))
  7. def exit():
  8.  lock.signal()
  9. def draw(x):
  10.  if img:canvas.blit(img)
  11. A.app.screen='full'
  12. canvas=A.Canvas(redraw_callback=draw)
  13. A.app.body=canvas
  14. A.app.exit_key_handler=exit
  15.  
  16. def h2r(h,s,v):
  17.  ''' HSV > RGB '''
  18.  if s==0.0: return v,v,v
  19.  i=int(h*6.0)
  20.  f=(h*6.0)-i
  21.  p=v*(1.0-s)
  22.  q=v*(1.0-s*f)
  23.  t=v*(1.0-s*(1.0-f))
  24.  if i%6==0:return v,t,p
  25.  if i==1:return q,v,p
  26.  if i==2:return p,v,t
  27.  if i==3:return p,q,v
  28.  if i==4:return t,p,v
  29.  if i==5:return v,p,q
  30.  
  31. def gen_color(n=256):
  32.  hsv=[(x*0.9/n,0.9,0.9) for x in xrange(n)]
  33.  rgb=map(lambda x: tuple(map(lambda x:int(x*255), h2r(*x))),hsv)
  34.  return rgb
  35.  
  36. def ssf(x,y,a,c):
  37.  img.line((x,y,x+a,y),c)
  38.  img.line((x,y,x,y+a),c)
  39.  img.line((x,y+a,x+a,y+a),c)
  40.  img.line((x+a,y,x+a,y+a),c)
  41.  canvas.blit(img)
  42.  a=float(a)/3
  43.  if a<1:return
  44.  for i in range(9):
  45.   if i!=4:
  46.    ssf(x+a*(i%3),y+a*(i/3),a,c)
  47.  A.e32.ao_sleep(1e-08)
  48.  
  49. iter,colstep=0,9
  50. color=gen_color(colstep)
  51. size=160
  52. n=size/3
  53. px=(w-size)/2
  54. img.clear(0)
  55. for i in range(9):
  56.  ssf(px+((i%3)*n),5+((i/3)*n),n,color[iter])
  57.  iter+=1
  58. img.save(u'e:\\sierpinski.png')
  59. lock.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement