Skip to content

Instantly share code, notes, and snippets.

@tareq-si-salem
Created August 6, 2016 16:06
Show Gist options
  • Save tareq-si-salem/dcc6b9c9b74000d5e28dd71ca325266f to your computer and use it in GitHub Desktop.
Save tareq-si-salem/dcc6b9c9b74000d5e28dd71ca325266f to your computer and use it in GitHub Desktop.
Visualizing DFS with a GUI version
import java.util.ArrayList;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MainGUI extends Application {
ArrayList<Circle> nodes = new ArrayList<>();
enum EdgeType {
TREE,
CROSS,
BACK,
FORWARD
}
@Override
public void start(Stage stage) throws Exception {
DiGraph diGraph = new DiGraph();
System.out.println(diGraph);
Pane root = new Pane();
// root.setStyle("-fx-background-color: #aaa");
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
int nodesInOneLine = 4;
for (int i = 0; i < diGraph.vertices.length; i++) {
Circle circle = new Circle(15);
double level = 0.98f / (diGraph.vertices.length);
circle.setFill(new Color(
diGraph.dfs_numbers[i] * (level),
diGraph.dfs_numbers[i] * (level),
diGraph.dfs_numbers[i] * (level),
1.0f)
);
Text text = new Text("" + i);
text.setFont(Font.font(17));
text.setFill(Color.DARKORANGE);
circle.setStroke(Color.DARKORANGE);
text.layoutXProperty().bind(circle.centerXProperty().add(-text.getLayoutBounds().getWidth() / 2));
text.layoutYProperty().bind(circle.centerYProperty().add(5));
nodes.add(circle);
root.getChildren().addAll(circle, text);
}
if (!diGraph.planarLayout) {
int i = 0;
for (Circle circle : nodes) {
circle.setCenterX(400 + 250 * Math.cos(i * 2 * Math.PI / diGraph.vertices.length));
circle.setCenterY(300 + 250 * (Math.sin(i * 2 * Math.PI / diGraph.vertices.length)));
i++;
}
} else {
int i = 0;
int j = 0;
for (Circle circle : nodes) {
circle.setCenterX(50 + i * (750 / nodesInOneLine));
circle.setCenterY(50 + j * (550 / nodesInOneLine));
i++;
if (i % nodesInOneLine == 0) {
j++;
i = 0;
}
}
}
diGraph.treeEdges.stream().forEach(edge -> {
addEdge(root, edge, EdgeType.TREE);
});
diGraph.forwardEdges.stream().forEach(edge -> {
addEdge(root, edge, EdgeType.FORWARD);
});
diGraph.backEdges.stream().forEach(edge -> {
addEdge(root, edge, EdgeType.BACK);
});
diGraph.crossEdges.stream().forEach(edge -> {
addEdge(root, edge, EdgeType.CROSS);
});
}
void addEdge(Pane root, Edge edge, EdgeType type) {
Line line = new Line();
Circle u, v;
u = nodes.get(edge.u);
v = nodes.get(edge.v);
line.setStartX(u.getCenterX());
line.setStartY(u.getCenterY());
line.setEndX(v.getCenterX());
line.setEndY(v.getCenterY());
Text t = new Text(type.name().toLowerCase() + " Edge");
switch (type) {
case TREE:
t.setFill(Color.GREEN);
line.setStroke(Color.GREEN);
break;
case BACK:
t.setFill(Color.BLUE);
line.setStroke(Color.BLUE);
break;
case FORWARD:
t.setFill(Color.ORANGE);
line.setStroke(Color.ORANGE);
break;
case CROSS:
t.setFill(Color.RED);
line.setStroke(Color.RED);
break;
}
double arrowAngle = (Math.atan2(line.getEndY() - line.getStartY(), line.getEndX() - line.getStartX()));
t.setLayoutX((line.getEndX() - line.getStartX()) / 2 + line.getStartX());
t.setLayoutY((line.getEndY() - line.getStartY()) / 2 + line.getStartY());
double scale = 2.5;
Polygon arrowHead = new Polygon(-4.33 * scale, 2.5 * scale, 5.0 * scale, 0, -4.33 * scale, -2.5 * scale, -4.33 * scale, 2.5 * scale);
arrowHead.setRotate(Math.toDegrees(arrowAngle));
arrowHead.setLayoutX(line.getEndX() - 20 * Math.cos(arrowAngle));
arrowHead.setLayoutY(line.getEndY() - 20 * Math.sin(arrowAngle));
arrowHead.setFill(line.getStroke());
root.getChildren().add(0, arrowHead);
root.getChildren().add(0, line);
root.getChildren().add(0, t);
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment