main
Felix Pankratz 3 years ago
parent 1d53c4a731
commit 774f4fd877

@ -3,6 +3,8 @@
import cairo import cairo
import math import math
import random import random
import threading
import time
from utils import circle_fill from utils import circle_fill
from utils import random_color from utils import random_color
from pixelflut import surface_to_pixelflut from pixelflut import surface_to_pixelflut
@ -13,6 +15,9 @@ ANGLE_RANDOM_MAX = 0.6
# how much to shrink each consecutive circle # how much to shrink each consecutive circle
SHRINK = 0.00002 SHRINK = 0.00002
hitmap = list()
class Branch(): class Branch():
def __init__(self, idx, ctx, x, y, r, ang): def __init__(self, idx, ctx, x, y, r, ang):
#ctx.set_source_rgb(255, 0, 0) #ctx.set_source_rgb(255, 0, 0)
@ -40,7 +45,7 @@ class Branch():
return False return False
# did we hit canvas edge? # did we hit canvas edge?
# if next_x + next_r > 1 or next_x - next_r < 0: # if next_x + next_r > 1 or next_x - next_r < 0:
if (math.pow(next_x - 0.5, 2) + math.pow(next_y - 0.5, 2)) > math.pow(0.4, 2): if (math.pow(next_x - 0.5, 2) + math.pow(next_y - 0.5, 2)) > math.pow(0.5, 2):
self.ended = True self.ended = True
return False return False
if next_y + next_r > 1 or next_y - next_r < 0: if next_y + next_r > 1 or next_y - next_r < 0:
@ -81,17 +86,12 @@ class Node():
def __ne__(self, other): def __ne__(self, other):
return self.x != other.x or self.y != other.y or self.r != other.r or self.ang != other.ang return self.x != other.x or self.y != other.y or self.r != other.r or self.ang != other.ang
def grow_subs(ctx, subs, branches): def grow_sub(ctx, branch, branches, new_subs):
source = ctx.get_source()
new_subs = []
created = False
for branch in subs:
# create a sub branch based on length # create a sub branch based on length
sub_branches = len(branch.nodes) // 7 * 2 sub_branches = len(branch.nodes) // 7 * 2
if sub_branches == 0: return
#sub_branches = 4
#print(f'creating {sub_branches} subs') #print(f'creating {sub_branches} subs')
if sub_branches > 1:
created = True
for i in range(sub_branches): for i in range(sub_branches):
for n in range(60): # attempts at growing branches for n in range(60): # attempts at growing branches
start_node = random.choice(branch.nodes) start_node = random.choice(branch.nodes)
@ -100,20 +100,46 @@ def grow_subs(ctx, subs, branches):
start_x = (start_node.r * 1.2) * math.sin(start_angle) + start_node.x start_x = (start_node.r * 1.2) * math.sin(start_angle) + start_node.x
start_y = (start_node.r * 1.2) * math.cos(start_angle) + start_node.y start_y = (start_node.r * 1.2) * math.cos(start_angle) + start_node.y
start_r = start_node.r * 0.8 start_r = start_node.r * 0.8
new_branch = Branch(i, ctx, start_x, start_y, start_r, start_angle) new_branch = Branch(0, ctx, start_x, start_y, start_r, start_angle)
if new_branch.place_next(branches): if new_branch.place_next(branches):
break break
new_branch.set_ignores(branch.nodes) new_branch.set_ignores(branch.nodes)
branches.append(new_branch) branches.append(new_branch)
new_subs.append(new_branch) new_subs.append(new_branch)
if not created:
return def grow_branch_until_ended(branch, branches):
while not all([branch.ended for branch in new_subs]): while not branch.ended:
for branch in new_subs:
branch.place_next([b for b in branches if b != branch]) branch.place_next([b for b in branches if b != branch])
def grow_subs(ctx, subs, branches):
source = ctx.get_source()
new_subs = []
threads = list()
print('spawning sub growing threads')
for branch in subs:
# x = threading.Thread(target=grow_sub, args=(ctx, branch, branches, new_subs))
# threads.append(x)
# x.start()
grow_sub(ctx, branch, branches, new_subs)
#for tidx, thread in enumerate(threads):
# thread.join()
print('all threads finished')
print('starting place_next threads')
pl_threads = list()
for branch in new_subs:
x = threading.Thread(target=grow_branch_until_ended, args=(branch, branches))
pl_threads.append(x)
x.start()
for tidx, thread in enumerate(pl_threads):
thread.join()
print('place_next done')
#while not all([branch.ended for branch in new_subs]):
# for branch in new_subs:
# branch.place_next([b for b in branches if b != branch])
return new_subs return new_subs
def main():
def main():
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
ctx = cairo.Context(surface) ctx = cairo.Context(surface)
@ -132,6 +158,7 @@ def main():
branches.append(Branch(2, ctx, 0.6, 0.5, start_r, 90)) branches.append(Branch(2, ctx, 0.6, 0.5, start_r, 90))
branches.append(Branch(3, ctx, 0.5, 0.6, start_r, 0)) branches.append(Branch(3, ctx, 0.5, 0.6, start_r, 0))
# grow initial branches # grow initial branches
print('growing initial branches')
while not all([b.ended for b in branches]): while not all([b.ended for b in branches]):
for branch in branches: for branch in branches:
branch.place_next(branches) branch.place_next(branches)
@ -145,7 +172,12 @@ def main():
r, g, b = random_color() r, g, b = random_color()
ctx.set_source_rgb(r, g, b) ctx.set_source_rgb(r, g, b)
subs = grow_subs(ctx, subs, branches) subs = grow_subs(ctx, subs, branches)
surface_to_pixelflut(surface) print(f'iteration {x} done, sending to pixelflut')
x = threading.Thread(target=surface_to_pixelflut, args=(surface,))
x.daemon = True
x.start()
x.join()
#surface_to_pixelflut(surface)
#surface.write_to_png("out/hyphae.png") # Output to PNG #surface.write_to_png("out/hyphae.png") # Output to PNG
#input('next') #input('next')
finally: finally:

@ -42,3 +42,4 @@ def surface_to_pixelflut(surface):
if hexpx[6:8] == '00': if hexpx[6:8] == '00':
continue continue
s.sendall(pxstr.encode()) s.sendall(pxstr.encode())
print('transmission to pixelflut finished')

@ -104,8 +104,8 @@ def create_wpotd(output):
ctx.line_to(*points[0]) ctx.line_to(*points[0])
ctx.fill() ctx.fill()
lastpoints = points lastpoints = points
return surface
surface.write_to_png(output) # Output to PNG #surface.write_to_png(output) # Output to PNG
def main(): def main():
create_wpotd('out/waves.png') create_wpotd('out/waves.png')

Loading…
Cancel
Save