diff --git a/hyphae_pixelflut.py b/hyphae_pixelflut.py index e1583e7..80fb33d 100644 --- a/hyphae_pixelflut.py +++ b/hyphae_pixelflut.py @@ -3,6 +3,8 @@ import cairo import math import random +import threading +import time from utils import circle_fill from utils import random_color from pixelflut import surface_to_pixelflut @@ -13,6 +15,9 @@ ANGLE_RANDOM_MAX = 0.6 # how much to shrink each consecutive circle SHRINK = 0.00002 +hitmap = list() + + class Branch(): def __init__(self, idx, ctx, x, y, r, ang): #ctx.set_source_rgb(255, 0, 0) @@ -40,7 +45,7 @@ class Branch(): return False # did we hit canvas edge? # 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 return False if next_y + next_r > 1 or next_y - next_r < 0: @@ -81,39 +86,60 @@ class Node(): def __ne__(self, other): return self.x != other.x or self.y != other.y or self.r != other.r or self.ang != other.ang +def grow_sub(ctx, branch, branches, new_subs): + # create a sub branch based on length + sub_branches = len(branch.nodes) // 7 * 2 + if sub_branches == 0: return + #sub_branches = 4 + #print(f'creating {sub_branches} subs') + for i in range(sub_branches): + for n in range(60): # attempts at growing branches + start_node = random.choice(branch.nodes) + # start perpendicular to our last angle + start_angle = start_node.ang + random.choice([90, -90]) + random.uniform(-30, 30) + 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_r = start_node.r * 0.8 + new_branch = Branch(0, ctx, start_x, start_y, start_r, start_angle) + if new_branch.place_next(branches): + break + new_branch.set_ignores(branch.nodes) + branches.append(new_branch) + new_subs.append(new_branch) + +def grow_branch_until_ended(branch, branches): + while not branch.ended: + branch.place_next([b for b in branches if b != branch]) + def grow_subs(ctx, subs, branches): source = ctx.get_source() new_subs = [] - created = False + threads = list() + print('spawning sub growing threads') for branch in subs: - # create a sub branch based on length - sub_branches = len(branch.nodes) // 7 * 2 - #print(f'creating {sub_branches} subs') - if sub_branches > 1: - created = True - - for i in range(sub_branches): - for n in range(60): # attempts at growing branches - start_node = random.choice(branch.nodes) - # start perpendicular to our last angle - start_angle = start_node.ang + random.choice([90, -90]) + random.uniform(-30, 30) - 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_r = start_node.r * 0.8 - new_branch = Branch(i, ctx, start_x, start_y, start_r, start_angle) - if new_branch.place_next(branches): - break - new_branch.set_ignores(branch.nodes) - branches.append(new_branch) - new_subs.append(new_branch) - if not created: - return - 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]) + # 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 + def main(): - surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) 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(3, ctx, 0.5, 0.6, start_r, 0)) # grow initial branches + print('growing initial branches') while not all([b.ended for b in branches]): for branch in branches: branch.place_next(branches) @@ -145,7 +172,12 @@ def main(): r, g, b = random_color() ctx.set_source_rgb(r, g, b) 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 #input('next') finally: diff --git a/pixelflut.py b/pixelflut.py index fc6bbf2..b4161fb 100644 --- a/pixelflut.py +++ b/pixelflut.py @@ -42,3 +42,4 @@ def surface_to_pixelflut(surface): if hexpx[6:8] == '00': continue s.sendall(pxstr.encode()) + print('transmission to pixelflut finished') diff --git a/waves.py b/waves.py index 04c8199..e426d6d 100755 --- a/waves.py +++ b/waves.py @@ -104,8 +104,8 @@ def create_wpotd(output): ctx.line_to(*points[0]) ctx.fill() lastpoints = points - - surface.write_to_png(output) # Output to PNG + return surface + #surface.write_to_png(output) # Output to PNG def main(): create_wpotd('out/waves.png')