Some new apps (wip)

This commit is contained in:
lep 2018-12-31 23:10:43 +01:00
parent a2cd17ad92
commit abc0660f2b
4 changed files with 166 additions and 1 deletions

81
apps/pendlum.py Executable file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env python3.6
import numpy as np
import sys
import os
import time
import colorsys
Nx = int(sys.argv[1])
Ny = int(sys.argv[2])
N = 100
iterations = 2
try:
N = int(sys.argv[3])
except:
pass
def rk4(f, x, dt):
k1 = f(x)
k2 = f(x+k1*dt/2)
k3 = f(x+k2*dt/2)
k4 = f(x+k3*dt)
return x+dt*(k1+2*k2+2*k3+k4)/6.0
dt = 0.08
m = 1
l = 1
g = 1
def sq(x): return x*x
def rhs(x):
phi1 = x[0]
p1 = x[1]
phi2 = x[2]
p2 = x[3]
dphi1 = 6/(m*l*l)*(2*p1-3*np.cos(phi1-phi2)*p2)/(16-9*sq(np.cos(phi1-phi2)))
dphi2 = 6/(m*l*l)*(8*p2-3*np.cos(phi1-phi2)*p1)/(16-9*sq(np.cos(phi1-phi2)))
dp1 = -0.5*m*l*l*(dphi1*dphi2*np.sin(phi1-phi2)+3*g/l*np.sin(phi1))
dp2 = -0.5*m*l*l*(-dphi1*dphi2*np.sin(phi1-phi2)+g/l*np.sin(phi2))
return np.array([dphi1, dp1, dphi2, dp2])
s = np.array([np.pi+0.1, 0, np.pi-0.1, 0])
angles = np.linspace(np.pi, np.pi/3, N)+np.random.uniform(-np.pi/6/N, np.pi/6/N, N)
angles = np.random.uniform(-np.pi/6, np.pi/3, N)+np.pi
states = [np.array([np.pi, 0, a, 0]) for a in angles]
def clamp(x, min_, max_):
return max(min_, min(max_, x))
buffer = bytearray(b"\x00"*Nx*Ny*3)
def setpixel(x, y, r, g, b):
xi = int(Nx*(x+1.2)/2.4)
yi = int(Ny*(y+1.2)/2.4)
if xi < 0 or xi >= Nx or yi < 0 or yi >= Ny:
return
idx = xi+Nx*yi
buffer[3*idx+0] = r
buffer[3*idx+1] = g
buffer[3*idx+2] = b
timestep = 0
timefac = 0.03
while True:
timestep += 1
for s, i in zip(states, range(len(states))):
phi1 = s[0]
phi2 = s[2]
x1 = np.sin(phi1)*l
y1 = np.cos(phi1)*l
x2 = np.sin(phi2)*l+x1
y2 = np.cos(phi2)*l+y1
h = 0.2*i/N+0*timestep*timefac/iterations
r, g, b = colorsys.hsv_to_rgb(h%1, 1, 1)
setpixel(x2, y2, int(r*255), int(g*255), int(b*255))
states[i] = rk4(rhs, s, dt/iterations)
if timestep > 10*iterations and timestep % iterations == 0:
#time.sleep(0.01)
os.write(1, buffer)

80
apps/quadratic.py Executable file
View File

@ -0,0 +1,80 @@
#!/usr/local/bin/python3.6
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 = 0
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, 0) #1.1
Sb = Slider(-10, 10, 0.2/iterations, 1) #-2.1
Sc = Slider(-10, 10, 0.2/iterations, 2) #-0.33
Sd = Slider(-10, 10, 0.1/iterations, 3) #1.7
Se = Slider(-10, 10, 0.2/iterations, 4)
Sf = Slider(-10, 10, 0.01/iterations, 5)
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()
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)
s = np.shape(curve)
#img[:,:,0] = curve
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
cb = (cb + 0.05) % 1

View File

@ -28,6 +28,8 @@ Apps = [
{"guiname": "Show Framebuffer", "name": "fbcp", "cmd": ["apps/fbcp", "/dev/fb0"]},
{"guiname": "Strobo", "name": "strobo", "cmd": "apps/strobo.py"},
{"guiname": "Beispiel", "name": "example", "cmd": "apps/example.py"},
{"guiname": "Quadratisch", "name": "quadratic", "cmd": "apps/quadratic.py"},
{"guiname": "Pendel", "name": "pendulum", "cmd": "apps/pendlum.py"},
# juergen/pixelfoo
{"guiname": "Colored noise", "name": "cnoise", "cmd": "apps/cnoise", "persistent": False},

View File

@ -7,6 +7,8 @@ chmod +x apps/swifthohenberg.py
chmod +x apps/strobo.py
chmod +x apps/example.py
chmod +x apps/youtubedl.sh
chmod +x apps/quadratic.py
chmod +x apps/pendlum.py
echo "Building c/c++ apps"