pixelserver2/apps/quadratic.py
2024-10-28 18:10:03 +01:00

83 lines
1.8 KiB
Python
Executable File

#!/usr/bin/env python3
import numpy as np
import os
import sys
import time
import colorsys
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
iterations = 10
class Slider:
def __init__(self, lo, hi, step, pos=0):
self.lo = lo
self.hi = hi
self.stepSize = step
self.pos = pos
self.dir = 1
def step(self):
if self.pos >= self.hi:
self.dir = -1
elif self.pos <= self.lo:
self.dir = 1
self.pos += self.dir * self.stepSize
return self.pos
Sa = Slider(-10, 10, 0.1/iterations, np.random.uniform(-2, 2))
Sb = Slider(-10, 10, 0.2/iterations, np.random.uniform(-2, 2))
Sc = Slider(-10, 10, 0.2/iterations, np.random.uniform(-2, 2))
Sd = Slider(-10, 10, 0.1/iterations, np.random.uniform(-2, 2))
Se = Slider(-10, 10, 0.2/iterations, np.random.uniform(-2, 2))
Sf = Slider(0, 6, 0.05/iterations, 0)
x, y = np.meshgrid(
np.linspace(-4, 4, Nx),
np.linspace(-4, 4, Ny))
color = 1.0
img = np.zeros( [Ny, Nx, 3] )
cr = 1.0
cg = 0.5
cb = 0.25
step = 0
while True:
a = Sa.step()
b = Sb.step()
c = Sc.step()
d = Sd.step()
e = Se.step()
f = Sf.step()
Sa.pos += b*0.001
Sb.pos += c*0.001
Sc.pos += d*0.001
Sd.pos += e*0.001
Se.pos += f*0.001
Sf.pos += a*0.001
curve = (np.abs(a*x**2 + b*x*y + c*y**2 + d*x + e*y + f) <= 1.5)*1
cr, cg, cb = colorsys.hsv_to_rgb(color, 1, 1)
img[:,:,0] = np.where(curve > 0, curve*cr*255, img[:,:,0])
img[:,:,1] = np.where(curve > 0, curve*cg*255, img[:,:,1])
img[:,:,2] = np.where(curve > 0, curve*cb*255, img[:,:,2])
step += 1
if step % iterations == 0:
out = img.reshape((Nx*Ny*3,)).astype(np.uint8)
os.write(1, out.tobytes())
time.sleep(0.01)
color = (color+0.01/iterations ) % 1