works
This commit is contained in:
@@ -22,7 +22,6 @@ class Graph {
|
|||||||
adj.add(new ArrayList<Integer>());
|
adj.add(new ArrayList<Integer>());
|
||||||
coords.add(new ArrayList<Integer>());
|
coords.add(new ArrayList<Integer>());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_vis(boolean[] vis) {
|
void draw_vis(boolean[] vis) {
|
||||||
@@ -32,7 +31,6 @@ class Graph {
|
|||||||
if (!vis[i]) continue;
|
if (!vis[i]) continue;
|
||||||
point(coords.get(i).get(0), coords.get(i).get(1));
|
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);
|
text(i+1, coords.get(i).get(0)-4, coords.get(i).get(1)+5);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +49,7 @@ class Graph {
|
|||||||
for (int i = 0; i < adj.size(); i++) {
|
for (int i = 0; i < adj.size(); i++) {
|
||||||
for (int idx = 0; idx < adj.get(i).size(); idx++) {
|
for (int idx = 0; idx < adj.get(i).size(); idx++) {
|
||||||
int e = adj.get(i).get(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));
|
draw_arrow(coords.get(i).get(0), coords.get(i).get(1), coords.get(e).get(0), coords.get(e).get(1), 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,8 +64,11 @@ class Graph {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Graph g;
|
||||||
|
ArrayList<int[]> edges;
|
||||||
|
boolean[] vis;
|
||||||
|
|
||||||
void draw_arrow(float x1, float y1, float x2, float y2) {
|
void draw_arrow_2(float x1, float y1, float x2, float y2, float offset) {
|
||||||
float triangle_size = dist(x1, y1, x2, y2) / 50;
|
float triangle_size = dist(x1, y1, x2, y2) / 50;
|
||||||
pushMatrix();
|
pushMatrix();
|
||||||
translate(x2, y2); //changes (0, 0)
|
translate(x2, y2); //changes (0, 0)
|
||||||
@@ -77,57 +78,61 @@ void draw_arrow(float x1, float y1, float x2, float y2) {
|
|||||||
line(x1, y1, x2, y2);
|
line(x1, y1, x2, y2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw_arrow(float x1, float y1, float x2, float y2, float offset) {
|
||||||
|
float angle = atan2(y2-y1, x2 - x1);
|
||||||
|
float offset_x = offset;
|
||||||
|
float offset_y = offset;
|
||||||
|
//if (x2 < x1) offset_x *= -1;
|
||||||
|
//if (y2 < y1) offset_y *= -1;
|
||||||
|
line(x1 + offset_x * sqrt(1-sin(angle)), y1 + offset_y * sin(angle),
|
||||||
|
x2 - offset_x * sqrt(1-sin(angle)), y2 - offset_y * sin(angle));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
size(1000, 1000);
|
size(1000, 1000);
|
||||||
frameRate(60);
|
frameRate(60);
|
||||||
background(0);
|
background(255);
|
||||||
test();
|
test();
|
||||||
|
edges = bfs(0, 6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void bfs(Graph g, boolean[] vis, int start, int end) {
|
ArrayList<int[]> bfs(int start, int end) {
|
||||||
ArrayList<Integer> q = new ArrayList<Integer>();
|
ArrayList<Integer> q = new ArrayList<Integer>();
|
||||||
|
ArrayList<int[]> res = new ArrayList<int[]>();
|
||||||
q.add(start);
|
q.add(start);
|
||||||
|
int[] edge = new int[] {0, 0};
|
||||||
|
res.add(edge);
|
||||||
int q_size = 1;
|
int q_size = 1;
|
||||||
while (q_size > 0) {
|
while (q_size > 0) {
|
||||||
int next = q.get(0);
|
int next = q.get(0);
|
||||||
q.remove(0);
|
q.remove(0);
|
||||||
q_size--;
|
q_size--;
|
||||||
vis[next] = true;
|
vis[next] = true;
|
||||||
for (int neighbor: g.adj.get(next)) {
|
g.add_edge(0, 0);
|
||||||
background(0);
|
print(g.adj.size());
|
||||||
g.draw_graph();
|
for (int neighbor : g.adj.get(next)) {
|
||||||
g.draw_points();
|
print(neighbor);
|
||||||
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;
|
if (vis[neighbor]) continue;
|
||||||
q.add(neighbor);
|
q.add(neighbor);
|
||||||
|
edge = new int[]{next, neighbor};
|
||||||
|
res.add(edge);
|
||||||
q_size++;
|
q_size++;
|
||||||
if (neighbor == end) return;
|
if (neighbor == end) return res;
|
||||||
print("next");
|
|
||||||
}
|
|
||||||
if (q_size == 0) {
|
|
||||||
print("end");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
//if (q_size == 0) {
|
||||||
|
// print("end");
|
||||||
|
// return res;
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void test() {
|
void test() {
|
||||||
int size = 7;
|
int size = 7;
|
||||||
Graph g = new Graph(size);
|
g = new Graph(size);
|
||||||
g.add_edge(0, 1);
|
g.add_edge(0, 1);
|
||||||
g.add_edge(1, 3);
|
g.add_edge(1, 3);
|
||||||
g.add_edge(1, 4);
|
g.add_edge(1, 4);
|
||||||
@@ -141,24 +146,21 @@ void test() {
|
|||||||
g.set_coords(4, 300, 250);
|
g.set_coords(4, 300, 250);
|
||||||
g.set_coords(5, 500, 250);
|
g.set_coords(5, 500, 250);
|
||||||
g.set_coords(6, 700, 250);
|
g.set_coords(6, 700, 250);
|
||||||
|
vis = new boolean[]{false, false, false, false, false, false, false};
|
||||||
boolean[] vis = new boolean[size];
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
vis[i] = false;
|
|
||||||
}
|
|
||||||
bfs(g, vis, 0, size-1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw() {
|
void draw() {
|
||||||
|
if (frameCount % 60 == 0) {
|
||||||
//stroke(#FF00FF);
|
g.draw_graph();
|
||||||
//strokeWeight(50);
|
g.draw_points();
|
||||||
//ellipse(500, 500, 100, 100);
|
stroke(#00FF00);
|
||||||
//delay(1000);
|
if (!edges.isEmpty()) {
|
||||||
//strokeWeight(20);
|
int[] edge = edges.get(0);
|
||||||
//stroke(#550000);
|
edges.remove(0);
|
||||||
//draw_arrow(width/2, height/2, width/3, height/2);
|
strokeWeight(20);
|
||||||
|
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), 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user