Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: None  |  size: 2.39 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/pypy
  2. # encoding: utf-8
  3.  
  4. from random import Random
  5. import math
  6. import sys
  7. import time
  8.  
  9. def lerp(a, b, v):
  10.         return a * (1 - v) + b * v
  11.  
  12. def smooth(v):
  13.         return v * v * (3 - 2 * v)
  14.  
  15. def random_gradient(r):
  16.         v = r.random() * math.pi * 2.0
  17.         return Vec2(math.cos(v), math.sin(v))
  18.  
  19. def gradient(orig, grad, p):
  20.         sp = Vec2(p.x - orig.x, p.y - orig.y)
  21.         return grad.x * sp.x + grad.y * sp.y
  22.  
  23. class Vec2(object):
  24.         __slots__ = ('x', 'y')
  25.  
  26.         def __init__(self, x, y):
  27.                 self.x = x
  28.                 self.y = y
  29.  
  30. class Noise2DContext(object):
  31.         __slots__ = ('rgradients', 'permutations', 'gradients', 'origins')
  32.  
  33.         def __init__(self, seed):
  34.                 self.rgradients = []
  35.                 self.permutations = []
  36.                 self.gradients = [None, None, None, None]
  37.                 self.origins = [None, None, None, None]
  38.  
  39.                 r = Random(seed)
  40.                 for i in xrange(256):
  41.                         self.rgradients.append(random_gradient(r))
  42.  
  43.                 for i in xrange(256):
  44.                         self.permutations.append(i)
  45.                 r.shuffle(self.permutations)
  46.  
  47.         def get_gradient(self, x, y):
  48.                 idx = self.permutations[x & 255] + self.permutations[y & 255]
  49.                 return self.rgradients[idx & 255]
  50.  
  51.         def get_gradients(self, x, y):
  52.                 x0f = math.floor(x)
  53.                 y0f = math.floor(y)
  54.                 x0 = int(x0f)
  55.                 y0 = int(y0f)
  56.                 x1 = x0 + 1
  57.                 y1 = y0 + 1
  58.  
  59.                 self.gradients[0] = self.get_gradient(x0, y0)
  60.                 self.gradients[1] = self.get_gradient(x1, y0)
  61.                 self.gradients[2] = self.get_gradient(x0, y1)
  62.                 self.gradients[3] = self.get_gradient(x1, y1)
  63.  
  64.                 self.origins[0] = Vec2(x0f + 0.0, y0f + 0.0)
  65.                 self.origins[1] = Vec2(x0f + 1.0, y0f + 0.0)
  66.                 self.origins[2] = Vec2(x0f + 0.0, y0f + 1.0)
  67.                 self.origins[3] = Vec2(x0f + 1.0, y0f + 1.0)
  68.  
  69.         def get(self, x, y):
  70.                 p = Vec2(x, y)
  71.                 self.get_gradients(x, y)
  72.                 v0 = gradient(self.origins[0], self.gradients[0], p)
  73.                 v1 = gradient(self.origins[1], self.gradients[1], p)
  74.                 v2 = gradient(self.origins[2], self.gradients[2], p)
  75.                 v3 = gradient(self.origins[3], self.gradients[3], p)
  76.  
  77.                 fx = smooth(x - self.origins[0].x)
  78.                 vx0 = lerp(v0, v1, fx)
  79.                 vx1 = lerp(v2, v3, fx)
  80.                 fy = smooth(y - self.origins[0].y)
  81.                 return lerp(vx0, vx1, fy)
  82.  
  83.  
  84. symbols = [' ', '░', '▒', '▓', '█', '█']
  85.  
  86. pixels = [['' for i in xrange(256)] for i in xrange(256)]
  87.  
  88. n2d = Noise2DContext(time.time())
  89. for i in xrange(100):
  90.         for y in xrange(256):
  91.                 for x in xrange(256):
  92.                         v = n2d.get(x * 0.1, (y + (i * 128)) * 0.1) * 0.5 + 0.5
  93.                         s = symbols[int(v / 0.2)]
  94.                         pixels[y][x] = s
  95.  
  96. for y in xrange(256):
  97.         for x in xrange(256):
  98.                 sys.stdout.write(pixels[y][x])
  99.         sys.stdout.write('\n')