From a32425724c18378c8e469641face3f9023a29c67 Mon Sep 17 00:00:00 2001 From: DarianTr Date: Fri, 17 Nov 2023 19:50:30 +0100 Subject: [PATCH] arrows fixed + colors --- bfs_visualization/Graph.pde | 12 +++++++----- bfs_visualization/draw.pde | 21 ++++++++++++++------- bfs_visualization/test.pde | 16 ++++++++-------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/bfs_visualization/Graph.pde b/bfs_visualization/Graph.pde index ab1dbb3..52a4e14 100644 --- a/bfs_visualization/Graph.pde +++ b/bfs_visualization/Graph.pde @@ -24,20 +24,22 @@ class Graph { void draw_points(int point_weight) { strokeWeight(point_weight); - stroke(#FF0000); + stroke(255); 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)-3, coords.get(i).get(1)+5); + textAlign(CENTER, CENTER); + fill(0); + text(i+1, coords.get(i).get(0), coords.get(i).get(1)); } } void draw_graph(int graph_weight, int point_weight) { - stroke(#550000); - strokeWeight(10); + stroke(255); + strokeWeight(graph_weight); 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), point_weight, graph_weight); + draw_arrow(coords.get(i).get(0), coords.get(i).get(1), coords.get(e).get(0), coords.get(e).get(1), point_weight, graph_weight, 15); } } } diff --git a/bfs_visualization/draw.pde b/bfs_visualization/draw.pde index 5dd3d1e..2c8f43d 100644 --- a/bfs_visualization/draw.pde +++ b/bfs_visualization/draw.pde @@ -1,33 +1,40 @@ -void draw_arrow(float x1, float y1, float x2, float y2, float offset, float weight) { +void draw_arrow(float x1, float y1, float x2, float y2, float offset, float weight, float distance_from_point) { if (x1 == x2 && y1 == y2) return; float angle = atan2(y2-y1, x2 - x1); + float triangle_size = dist(x1, y1, x2, y2) / 50; pushMatrix(); translate(x1, y1); + PVector start = PVector.fromAngle(angle); + start.setMag(distance_from_point/2 + offset/2 + weight/2); + pushMatrix(); + translate(start.x, start.y); PVector v = PVector.fromAngle(angle); - v.setMag(dist(x1, y1, x2, y2) - min(offset, weight) - offset/2); + v.setMag(dist(x1, y1, x2, y2) - 2* (offset/2 + distance_from_point/2 + weight/2) - weight/2); line(0, 0, v.x, v.y); pushMatrix(); translate(v.x, v.y); rotate(angle); - float triangle_size = dist(x1, y1, x2, y2) / 50; + fill(255); triangle(-triangle_size * 2, - triangle_size, 0, 0, -triangle_size * 2, triangle_size); popMatrix(); popMatrix(); + popMatrix(); } void draw() { - int edge_weight = 20; + int edge_weight = 10; int point_weight = 30; - int graph_weight = 10; + int graph_weight = 2; if (frameCount % 60 == 0) { + background(0); g.draw_graph(graph_weight, point_weight); g.draw_points(point_weight); - stroke(#00FF00); + stroke(#555555); if (!edges.isEmpty()) { int[] edge = edges.get(0); edges.remove(0); strokeWeight(edge_weight); - draw_arrow(g.coords.get(edge[0]).get(0), g.coords.get(edge[0]).get(1), g.coords.get(edge[1]).get(0), g.coords.get(edge[1]).get(1), point_weight, edge_weight); + draw_arrow(g.coords.get(edge[0]).get(0), g.coords.get(edge[0]).get(1), g.coords.get(edge[1]).get(0), g.coords.get(edge[1]).get(1), point_weight, edge_weight, 15); } } } diff --git a/bfs_visualization/test.pde b/bfs_visualization/test.pde index 5389456..79e48eb 100644 --- a/bfs_visualization/test.pde +++ b/bfs_visualization/test.pde @@ -4,16 +4,16 @@ void test() { g.add_edge(0, 1); g.add_edge(1, 3); g.add_edge(1, 4); - //g.add_edge(2, 0); + g.add_edge(2, 0); g.add_edge(4, 5); g.add_edge(5, 6); g.add_edge(0, 2); - 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); + g.set_coords(0, 500, 100); + g.set_coords(1, 200, 300); + g.set_coords(2, 780, 300); + g.set_coords(3, 100, 500); + g.set_coords(4, 300, 500); + g.set_coords(5, 300, 700); + g.set_coords(6, 300, 900); vis = new boolean[]{false, false, false, false, false, false, false}; }