perlin.html


001| '''Perlin Noise.'''
002| ####################################################################################################
003| from os import path
004| from math import pi,sin,cos,radians
005| from random import random
006| from vector import vector
007| from canvas import Canvas
008| ####################################################################################################
009| BasePath = r'C:\CJS\prj\Python\ws\PIL'
010| ImgsPath = path.join(BasePath, 'img')
011| 
012| fname = 'perlin.png'
013| 
014| dims = (600,400)
015| view = ((0.0, 12.0), (0.0, 8.0))
016| 
017| ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~##
018| class vcircle (object):
019|     def __init__ (self, angle, limit=0):
020|         self.v = complex(1.0, 0.0)
021|         self.r = complex(cos(radians(angle)), sin(radians(angle)))
022|         self.n = 0
023|         self.lim = limit
024| 
025|     def __next__ (self):
026|         if 0 < self.lim < self.n:
027|             raise StopIteration
028|         vec = vector(self.v.real, self.v.imag)
029|         self.v *= self.r
030|         self.n += 1
031|         return vec
032| 
033|     def __iter__ (self):
034|         return self
035| 
036| 
037| ##================================================================================================##
038| def generate_grid ():
039|     grid = []
040|     for px in range(int(view[0][1])+2):
041|         grid.append([])
042|         for py in range(int(view[1][1])+2):
043|             vec = vector.randvect(2).unit()
044|             #vec = next(VC)
045|             grid[px].append(vec)
046|             print('[%2d,%2d] %s' % (px,py,grid[px][py]), file=fp)
047|     print(file=fp)
048|     return grid
049| 
050| ##================================================================================================##
051| def generate_points (grid, im):
052|     data = []
053|     for px in range(dims[0]):
054|         data.append([])
055|         for py in range(dims[1]):
056|             sx = im.vx(px)
057|             sy = im.vy(py)
058|             ax = int(sx)
059|             ay = int(sy)
060|             vx = sx - ax
061|             vy = sy - ay
062|             v0 = vector(vx    ,vy    )
063|             v1 = vector(vx-1.0,vy    )
064|             v2 = vector(vx    ,vy-1.0)
065|             v3 = vector(vx-1.0,vy-1.0)
066|             a0 = v0.dot(grid[ax][ay])
067|             a1 = v1.dot(grid[ax+1][ay])
068|             a2 = v2.dot(grid[ax][ay+1])
069|             a3 = v3.dot(grid[ax+1][ay+1])
070|             ds = (a0, a1, a2, a3)
071|             data[px].append(ds)
072|             t = (px,py,ax,ay, a0,a1,a2,a3, vx,vy, v0,v1,v2,v3)
073|             s = '[%3d,%3d][%d,%d] %+6.3f %+6.3f %+6.3f %+6.3f (%+6.3f, %+6.3f) %s %s %s %s'
074|             print(s%t, file=fp)
075|             fp.flush()
076|     print(file=fp)
077|     return data
078| 
079| 
080| ##================================================================================================##
081| def main (*args):
082|     fp = open(path.join(BasePath,'perlin.out'), mode='w', encoding='utf-8')
083|     try:
084|         im = Canvas(dims, view)
085| 
086|         grid = generate_grid()
087|         data = generate_points(grid, im)
088| 
089|         for px,row in enumerate(data):
090|             for py,pixel in enumerate(row):
091|                 avg = sum(pixel)/float(len(pixel))
092|                 c0 = (abs(int(avg*255)),abs(int(avg*191)),0) if avg<0 else (0,int(avg*127),int(avg*255))
093|                 im.draw.point((px,py), fill=c0)
094| 
095|         for px,row in enumerate(grid):
096|             for py,vec in enumerate(row):
097|                 vx = vec.x * 0.5
098|                 vy = vec.y * 0.5
099|                 im.line((px,py), (px+vx,py+vy), c=(0,255,0), lw=1)
100|                 im.point5((px,py), c=(0,0,0))
101| 
102|         im.border([(0,0,0)], lw=1)
103|         im.save(path.join(BasePath,fname))
104|         print('Done!')
105|     except:
106|         raise
107|     finally:
108|         fp.close()
109| 
110|     VC = vcircle(3.0)
111| 
112|     return VC
113| ####################################################################################################
114| if __name__ == '__main__':
115|     print(argv[0])
116|     print()
117|     rv = main(*argv[1:])
118|     print()
119| ####################################################################################################
120| '''eof'''
121|