simplify image generation

This commit is contained in:
Florian Stecker 2022-06-18 14:54:50 +02:00
parent 920a530385
commit f89545e995
1 changed files with 17 additions and 39 deletions

View File

@ -3,65 +3,43 @@
import sys
import math
def sharpen(x):
alpha = 1
y = abs(2*x-1)**alpha # between 0 and 1
if x > 0.499 and x < 0.501:
return 0.5
elif x > 0.5:
return (y+1)/2
else:
return (1-y)/2
f = open(sys.argv[1])
lines = [x.split() for x in f]
f.close()
res1 = int(sys.argv[2]) # 400 pixel per unit
res2 = int(sys.argv[3]) # 50 pixel in picture (acutally one quadrant)
res = int(sys.argv[2]) # pixel per unit
data = {(round(float(l[0])*res1), round(float(l[1])*res1)) : float(l[5]) for l in lines}
data = {(round(float(l[0])*res), round(float(l[1])*res)) : float(l[5]) for l in lines}
data[(0,0)] = 2.0
xmin = min([p[0] for p in data.keys()])
xmax = max([p[0] for p in data.keys()])
ymin = min([p[1] for p in data.keys()])
ymax = max([p[1] for p in data.keys()])
yrange=max([-ymin,ymax])
print("P3")
print("1000 1000")
print("{dx:d} {dy:d}".format(dx=xmax-xmin+1, dy=2*yrange+1))
print("255")
for i in range (-500,500):
for j in range(-500,500):
x = j/500*res2
y = i/500*res2
for i in range(-yrange,yrange+1):
for j in range(xmin,xmax+1):
x = j
y = i if i >= ymin and i <= ymax else -i
if y < 0:
y = -y
x0 = math.floor(x)
y0 = math.floor(y)
x1 = math.ceil(x)
y1 = math.ceil(y)
tx = sharpen(x-x0)
ty = sharpen(y-y0)
if not (x0,y0) in data or not (x0,y1) in data or not (x1,y0) in data or not (x1,y1) in data:
if not (x,y) in data:
value = 0
else:
value = 0.0
value += data[(x0,y0)]*(1-tx)*(1-ty)
value += data[(x0,y1)]*(1-tx)*ty
value += data[(x1,y0)]*tx*(1-ty)
value += data[(x1,y1)]*tx*ty
value -= 1
value = data[(x,y)]-1
value = 0 if value < 0 else 1 if value > 1 else value
r = round(255*math.sqrt(value))
r = round(255*value**0.5)
g = round(255*(value)**3)
b = round(255*math.sin(2*math.pi*(value)))
b = round(255*(math.sin(2*math.pi*value)))
r = 0 if r < 0 else 255 if r > 255 else r
g = 0 if g < 0 else 255 if g > 255 else g
b = 0 if b < 0 else 255 if b > 255 else b
print("%d %d %d" % (r,g,b))
#print(data)