Skip to content

Commit 2484705

Browse files
committed
Visualize local codes
1 parent c7f4dee commit 2484705

File tree

2 files changed

+66
-6
lines changed

2 files changed

+66
-6
lines changed

src/main/java/org/algorithm_visualizer/Tracer.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
import com.google.gson.Gson;
44

5+
import java.awt.*;
6+
import java.io.BufferedReader;
7+
import java.io.DataOutputStream;
58
import java.io.FileWriter;
6-
import java.io.IOException;
9+
import java.io.InputStreamReader;
710
import java.io.PrintWriter;
11+
import java.net.HttpURLConnection;
12+
import java.net.URL;
13+
import java.net.URLEncoder;
814
import java.util.ArrayList;
915

1016
public abstract class Tracer {
@@ -55,11 +61,33 @@ protected Tracer(String title) {
5561
static {
5662
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
5763
try {
58-
FileWriter fileWriter = new FileWriter("traces.json");
59-
PrintWriter printWriter = new PrintWriter(fileWriter);
60-
printWriter.print(gson.toJson(traces));
61-
printWriter.close();
62-
} catch (IOException e) {
64+
String content = gson.toJson(traces);
65+
if (System.getenv("ALGORITHM_VISUALIZER") == null) {
66+
URL postUrl = new URL("https://algorithm-visualizer.org/api/visualizations");
67+
String params = "content=" + URLEncoder.encode(content, "UTF-8");
68+
69+
HttpURLConnection conn = (HttpURLConnection) postUrl.openConnection();
70+
conn.setRequestMethod("POST");
71+
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
72+
conn.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length));
73+
conn.setDoOutput(true);
74+
75+
DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
76+
writer.writeBytes(params);
77+
writer.close();
78+
79+
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
80+
URL openUrl = new URL(reader.readLine());
81+
reader.close();
82+
83+
Desktop.getDesktop().browse(openUrl.toURI());
84+
} else {
85+
FileWriter fileWriter = new FileWriter("traces.json");
86+
PrintWriter printWriter = new PrintWriter(fileWriter);
87+
printWriter.print(content);
88+
printWriter.close();
89+
}
90+
} catch (Exception e) {
6391
e.printStackTrace();
6492
}
6593
}));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.algorithm_visualizer;
2+
3+
class Test {
4+
static GraphTracer tracer = new GraphTracer().log(new LogTracer());
5+
static int G[][] = { // G[i][j] indicates whether the path from the i-th node to the j-th node exists or not
6+
{0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
7+
{0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
8+
{0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0},
9+
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0},
10+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
11+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
12+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
13+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
14+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
15+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
16+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
17+
};
18+
19+
static void DFS(int node, int parent) { // node = current node, parent = previous node
20+
tracer.visit(node, parent).delay();
21+
for (int i = 0; i < G[node].length; i++) {
22+
if (G[node][i] == 1) { // if current node has the i-th node as a child
23+
DFS(i, node); // recursively call DFS
24+
}
25+
}
26+
}
27+
28+
public static void main(String[] args) {
29+
tracer.set(G).layoutTree(0).delay();
30+
DFS(0, -1);
31+
}
32+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy