Browse Source

added curves.py

master
Fr3deric 6 years ago
parent
commit
43dcd7a620
  1. 107
      generators/curves.py

107
generators/curves.py

@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
#!/usr/bin/env python3
import sys
import random
import math
import argparse
import collections
import blup.frame
import blup.output
import blup.animation
import blup.writebml
import colorsys
import functools
WIDTH = 22
HEIGHT = 16
DEPTH = 256
class Point(collections.namedtuple('Point', ['x', 'y'])):
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __mul__(self, other):
if isinstance(other, Point):
raise Exception('not implemented')
else:
return Point(self.x * other, self.y * other)
def __truediv__(self, other):
if isinstance(other, Point):
raise ValueError()
else:
return self * (1/other)
@property
def len(self):
return math.sqrt(self.x**2 + self.y**2)
@property
def n(self):
n = Point(self.y, -self.x)
return n / n.len
@property
def intp(self):
return Point(int(round(self.x)), int(round(self.y)))
def convert_color(c):
return list(map(lambda x: int(round(x*(DEPTH-1))), c))
def get_random_color():
return colorsys.hsv_to_rgb(random.random(), 0.5 + random.random() / 2, 1)
class CurveFunction():
def __init__(self, p1, p2):
self.p1, self.p2 = p1, p2
d = p2 - p1
self.pp = p1 + d/2 + d.n * (d.len * 0.8)
def __call__(self, t):
ret = (self.p1 - self.pp*2 + self.p2) * t**2
ret += (self.p1 * -2 + self.pp * 2) * t
ret += self.p1
return ret
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate animations')
parser.add_argument('-d', '--delay', type=int, default=50)
parser.add_argument('-t', '--time', type=int, default=15)
parser.add_argument('output_file')
args = parser.parse_args()
dim = blup.frame.FrameDimension(WIDTH, HEIGHT, DEPTH, 3)
anim = blup.animation.Animation(dim)
anim.tags['description'] = ' '.join(sys.argv)
t = 0
dt = args.delay / 1000
while t < args.time:
t += dt
p1 = p2 = None
while p1 == p2 or (p1 - p2).len < max(WIDTH, HEIGHT) / 2:
p1 = Point(random.randint(0, WIDTH-1), random.randint(0, HEIGHT-1))
p2 = Point(random.randint(0, WIDTH-1), random.randint(0, HEIGHT-1))
cf = CurveFunction(p1, p2)
color = convert_color(get_random_color())
tt = 0
points = []
while tt <= 1:
t += dt
tt += 0.025
p = cf(tt).intp
points.append(p)
frame = blup.animation.AnimationFrame(dim, args.delay)
for p in points:
try:
frame.setPixel(p.x, p.y, color)
except ValueError:
pass
anim.addFrame(frame)
blup.writebml.writeBml(anim, args.output_file)
Loading…
Cancel
Save