From 7b5f6b93d8b5ed8bd50802b07e47c73e7d6855ce Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Thu, 29 Oct 2026 19:41:56 +0200 Subject: [PATCH] lab3-rc2 --- main.py | 50 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/main.py b/main.py index 583d11e..883fac2 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,8 @@ write_files = False n = 0 nn_total_levels = 0 +edge_groups = {} + def y(i): # backwards compatibility if i.__class__ == str: @@ -45,7 +47,7 @@ def nn(*args): return str(i(x, y)) + '-' + l -def join_nodes(g, a, b, target_degree = 6): +def join_nodes(g, a, b, group = 'default', target_degree = 6): if a == b: return @@ -54,6 +56,12 @@ def join_nodes(g, a, b, target_degree = 6): if g.degree[a] > target_degree or g.degree[b] > target_degree: g.remove_edge(a, b) + return + + if group not in edge_groups: + edge_groups[group] = [] + + edge_groups[group].append((a, b)) def start_file(p): if not write_files: @@ -135,11 +143,11 @@ def build_network(l_n): #print('---', i, start.x, y, step, y + step, n) if step == 0: exit(1) - join_nodes(g, nn(start.x, y, 'll'), nn(start.x - step, y + step, 'll')) - join_nodes(g, nn(start.x, y, 'rr'), nn(start.x + step, y + step, 'rr')) + join_nodes(g, nn(start.x, y, 'll'), nn(start.x - step, y + step, 'll'), f"gf{i}") + join_nodes(g, nn(start.x, y, 'rr'), nn(start.x + step, y + step, 'rr'), f"gf{i}") y += step * 2 - join_nodes(g, nn(start.x, y, 'lr'), nn(start.x - step, y - step, 'lr')) - join_nodes(g, nn(start.x, y, 'rl'), nn(start.x + step, y - step, 'rl')) + join_nodes(g, nn(start.x, y, 'lr'), nn(start.x - step, y - step, 'lr'), f"gf{i}") + join_nodes(g, nn(start.x, y, 'rl'), nn(start.x + step, y - step, 'rl'), f"gf{i}") start.x += step * 2 @@ -152,17 +160,17 @@ def build_network(l_n): # inline skips for x in range(n): for y in range(n): - join_nodes(g, nn(x, y, 'ld'), nn((x + n//2) % n, y, 'ld')) - join_nodes(g, nn(x, y, 'rd'), nn((x + n//3) % n, y, 'rd')) + join_nodes(g, nn(x, y, 'ld'), nn((x + n//2) % n, y, 'ld'), "is1") + join_nodes(g, nn(x, y, 'rd'), nn((x + n//3) % n, y, 'rd'), "is2") - join_nodes(g, nn(x, y, 'lu'), nn(x, (y + n//5) % n, 'lu')) - join_nodes(g, nn(x, y, 'ru'), nn(x, (y + n//6) % n, 'ru')) + join_nodes(g, nn(x, y, 'lu'), nn(x, (y + n//5) % n, 'lu'), "is3") + join_nodes(g, nn(x, y, 'ru'), nn(x, (y + n//6) % n, 'ru'), "is4") - join_nodes(g, nn(x, y, 'll'), nn((x + round(n//3)) % n, (y + round(n//3)) % n, 'll')) - join_nodes(g, nn(x, y, 'rr'), nn((x + round(n//2)) % n, (y + round(n//2)) % n, 'rr')) + join_nodes(g, nn(x, y, 'll'), nn((x + round(n//3)) % n, (y + round(n//3)) % n, 'll'), "is5") + join_nodes(g, nn(x, y, 'rr'), nn((x + round(n//2)) % n, (y + round(n//2)) % n, 'rr'), "is6") - join_nodes(g, nn(x, y, 'lr'), nn((x + round(n//5)) % n, (y - round(n//5)) % n, 'lr')) - join_nodes(g, nn(x, y, 'rl'), nn((x + round(n//6)) % n, (y - round(n//6)) % n, 'rl')) + join_nodes(g, nn(x, y, 'lr'), nn((x + round(n//5)) % n, (y - round(n//5)) % n, 'lr'), "is7") + join_nodes(g, nn(x, y, 'rl'), nn((x + round(n//6)) % n, (y - round(n//6)) % n, 'rl'), "is8") return g @@ -188,6 +196,11 @@ def analyze(G: nx.Graph) -> dict: def visualize(g): + def draw_edge_groups(): + colors = ["#FF0000", "#00FF00", "#0000FF", "#AAAA00", "#BB00BB", "#1ab278", "#ea44fc", "#c9c204", "#49af5a", "#692c8e"] + for i, v in enumerate(edge_groups.values()): + nx.draw_networkx_edges(g, exp_pos, edgelist = v, connectionstyle='arc3,rad=0.2', arrows=True, width = 2.0, edge_color = colors[i % len(colors)]) + def draw_edges_between(a, b, x_min = 0, x_max = False, y_min = 0, y_max = False, color = 'black', width = 1.0, debug = False, direct = False, fmode = True, fmask = [1, 1, 1, 1], fsplit = False): edgelist = [e for e in g.edges() if all([True if (a in i) or (b in i) else False for i in e])] edgelist_filtered = [] @@ -255,17 +268,19 @@ def visualize(g): draw_edges_between('', '', x_max = 0, y_max = 0, color = '#888888', width = 0.5, direct = True) # regular grid - draw_edges_between('', '', x_min = 1, x_max = 1, y_min = 1, y_max = 1, color = '#FF0000', width = 2.0, fsplit = True) + #draw_edges_between('', '', x_min = 1, x_max = 1, y_min = 1, y_max = 1, color = '#FF0000', width = 2.0, fsplit = True) # anything (debug) - draw_edges_between('', '', x_min = 1, y_min = 1, color = '#00FF00', width = 2.0, direct = True) + #draw_edges_between('', '', x_min = 1, y_min = 1, color = '#00FF00', width = 2.0, direct = True) + + draw_edge_groups() plt.show() def main(): global write_files - write_files = False + write_files = True i = 0 @@ -299,7 +314,8 @@ def test2(): print(set([v for i, v in g.degree()])) def test3(): - g = build_network(9) + g = build_network(2) + print(analyze(g)) visualize(g) if __name__=="__main__":