From 6644c8f1f52d24e9a05ac8186c048927566d18ea Mon Sep 17 00:00:00 2001 From: DarianTr Date: Thu, 16 Nov 2023 00:14:14 +0100 Subject: [PATCH] todo: umstrukturieren --- sketch_231115a/sketch_231115a.pde | 203 ++++++++++++++++++++---------- 1 file changed, 134 insertions(+), 69 deletions(-) diff --git a/sketch_231115a/sketch_231115a.pde b/sketch_231115a/sketch_231115a.pde index 10e2519..95a883d 100644 --- a/sketch_231115a/sketch_231115a.pde +++ b/sketch_231115a/sketch_231115a.pde @@ -1,105 +1,170 @@ - -void setup() { - size(1500, 1500); +class Delay { + int limit; + + Delay (int l) { + limit = millis() + l; + } + + boolean expired () { + return millis() > limit; + } } +class Graph { + ArrayList> adj; + ArrayList> coords; + int V; + Graph(int v) { + V = v; + adj = new ArrayList>(V); + coords = new ArrayList>(V); + for (int i = 0; i < V; i++) { + adj.add(new ArrayList()); + coords.add(new ArrayList()); + } + + } + + void draw_vis(boolean[] vis) { + strokeWeight(20); + stroke(255); + for (int i = 0; i < coords.size(); i++) { + if (!vis[i]) continue; + point(coords.get(i).get(0), coords.get(i).get(1)); + text(i+1, coords.get(i).get(0)-4, coords.get(i).get(1)+5); + + } + } + + void draw_points() { + strokeWeight(20); + stroke(#FF0000); + for (int i = 0; i < coords.size(); i++) { + point(coords.get(i).get(0), coords.get(i).get(1)); + text(i+1, coords.get(i).get(0)-4, coords.get(i).get(1)+5); + } + } + + void draw_graph() { + stroke(#550000); + strokeWeight(10); + for (int i = 0; i < adj.size(); i++) { + for (int idx = 0; idx < adj.get(i).size(); idx++) { + int e = adj.get(i).get(idx); + draw_arrow(coords.get(i).get(0), coords.get(i).get(1), coords.get(e).get(0), coords.get(e).get(1)); + } + } + } + + void set_coords(int idx, int x, int y) { + coords.get(idx).add(x); + coords.get(idx).add(y); + } + + void add_edge(int u, int v) { + adj.get(u).add(v); + } +} + + void draw_arrow(float x1, float y1, float x2, float y2) { - line(x1, y1, x2, y2); float triangle_size = dist(x1, y1, x2, y2) / 50; + pushMatrix(); translate(x2, y2); //changes (0, 0) rotate(atan2(y2 - y1, x2 - x1)); triangle( -triangle_size * 2, - triangle_size, 0, 0, -triangle_size * 2, triangle_size); + popMatrix(); + line(x1, y1, x2, y2); +} + + +void setup() { + size(1000, 1000); + frameRate(60); + background(0); + test(); + } -void draw_points(ArrayList> coords) { - strokeWeight(20); - stroke(#FF0000); - print(coords.size()); - for (int i = 0; i < coords.size(); i++) { - print(i); - point(coords.get(i).get(0), coords.get(i).get(1)); - //text(i+1, coords.get(i).get(0), coords.get(i).get(1)); - } -} -void draw_vis(ArrayList> coords, boolean[] vis) { - strokeWeight(20); - stroke(#555555); - for (int i = 0; i < coords.size(); i++) { - if (!vis[i]) continue; - point(coords.get(i).get(0), coords.get(i).get(1)); - } -} -void draw_graph(ArrayList> adj, ArrayList> coords) { - - draw_points(coords); - stroke(#550000); - strokeWeight(10); - for (int i = 0; i < adj.size(); i++) { - for (int idx = 0; idx < adj.get(i).size(); idx++) { - int e = adj.get(i).get(idx); - print(adj.get(i).size()); - draw_arrow(coords.get(i).get(0), coords.get(i).get(1), coords.get(e).get(0), coords.get(e).get(1)); - } - } -} - -void bfs(ArrayList> adj, ArrayList> coords, boolean[] vis, int start, int end) { +void bfs(Graph g, boolean[] vis, int start, int end) { ArrayList q = new ArrayList(); q.add(start); - while (q.size() != 0) { + int q_size = 1; + while (q_size > 0) { int next = q.get(0); q.remove(0); + q_size--; vis[next] = true; - for (int neighbor: adj.get(next)) { + for (int neighbor: g.adj.get(next)) { + background(0); + g.draw_graph(); + g.draw_points(); + g.draw_vis(vis); + stroke(#00FF00); + draw_arrow(g.coords.get(next).get(0), g.coords.get(next).get(1), g.coords.get(neighbor).get(0), g.coords.get(neighbor).get(1)); + Delay timer = new Delay(1000); + while (!timer.expired()) { + // nothing + } + //int target_millis = millis() + 1000; + //while (millis() < target_millis) { + // print(millis()); + //} if (vis[neighbor]) continue; q.add(neighbor); - print("start"); - draw_graph(adj, coords); - print("start"); - draw_vis(coords, vis); - print("start"); - stroke(#00FF00); - draw_arrow(coords.get(next).get(0), coords.get(next).get(1), coords.get(neighbor).get(0), coords.get(neighbor).get(1)); + q_size++; if (neighbor == end) return; - delay(1000); + print("next"); + } + if (q_size == 0) { + print("end"); + return; } } } void test() { - int size = 5; - ArrayList empty = new ArrayList(); - ArrayList> adj = new ArrayList>(); - ArrayList> coords = new ArrayList>(); - for (int i = 0; i < size; i++) { - adj.add(empty); - coords.add(empty); - } - adj.get(0).add(2); - adj.get(1).add(4); - adj.get(1).add(3); - adj.get(2).add(0); - coords.get(0).add(width/10); coords.get(0).add(height-100); - coords.get(1).add(width/5); coords.get(1).add(height-100); - coords.get(3).add(width/2); coords.get(3).add(height-100); - coords.get(4).add(width/2); coords.get(4).add(height/2); - coords.get(2).add(width/5); coords.get(2).add(height/2); + int size = 7; + Graph g = new Graph(size); + g.add_edge(0, 1); + g.add_edge(1, 3); + g.add_edge(1, 4); + g.add_edge(2, 0); + g.add_edge(4, 5); + g.add_edge(5, 6); + g.set_coords(0, 100, 500); + g.set_coords(1, 300, 500); + g.set_coords(2, 300, 750); + g.set_coords(3, 500, 500); + g.set_coords(4, 300, 250); + g.set_coords(5, 500, 250); + g.set_coords(6, 700, 250); + boolean[] vis = new boolean[size]; for (int i = 0; i < size; i++) { vis[i] = false; } - bfs(adj, coords, vis, 0, 4); - + bfs(g, vis, 0, size-1); } void draw() { - background(0); - print("a"); - test(); + + //stroke(#FF00FF); + //strokeWeight(50); + //ellipse(500, 500, 100, 100); + //delay(1000); //strokeWeight(20); //stroke(#550000); //draw_arrow(width/2, height/2, width/3, height/2); } + + + + + + + +// TODO: So umstrukturieren, dass man einen Algorithmus hat, der einem die nächste Kante gibt, die man dann im draw malen muss.