Skip to content

Commit

Permalink
Add separate API-s for drawing directed and undirected edges
Browse files Browse the repository at this point in the history
  • Loading branch information
FeldrinH committed Oct 23, 2023
1 parent 139676c commit 757270d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 24 deletions.
16 changes: 13 additions & 3 deletions src/main/java/ee/ut/dendroloj/Dendrologist.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.awt.Color;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

public final class Dendrologist {
Expand Down Expand Up @@ -224,11 +225,20 @@ private static void drawAdjacencyMatrixGraph(int vertexCount, WeightProvider wei
GraphCanvas<Number> graphCanvas = new GraphCanvas<>();
for (int i = 0; i < vertexCount; i++) {
graphCanvas.drawVertex(i, labels == null ? String.valueOf(i) : labels[i]);
for (int j = 0; j < vertexCount; j++) {
Number selfWeight = weights.getWeight(i, i);
if (selfWeight != null && selfWeight.doubleValue() != 0.0) {
graphCanvas.drawEdge(i, i, selfWeight.toString());
}
for (int j = i + 1; j < vertexCount; j++) {
Number weight = weights.getWeight(i, j);
if (weight != null && (i != j || weight.doubleValue() != 0.0)) {
graphCanvas.drawEdge(i, j, weight.toString());
Number backwardsWeight = weights.getWeight(j, i);
if (Objects.equals(weight, backwardsWeight)) {
if (weight != null) graphCanvas.drawEdge(i, j, weight.toString());
} else {
if (weight != null) graphCanvas.drawDirectedEdge(i, j, weight.toString());
if (backwardsWeight != null) graphCanvas.drawDirectedEdge(j, i, backwardsWeight.toString());
}

}
}
Graph graph = GenericGraphLayout.assembleGraph(graphCanvas);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/ee/ut/dendroloj/GenericGraphLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.graphstream.ui.layout.Layout;
import org.graphstream.ui.layout.springbox.implementations.SpringBox;

import java.util.concurrent.atomic.AtomicLong;

class GenericGraphLayout {

public static Layout autoLayout() {
Expand All @@ -23,7 +21,7 @@ public static Graph assembleGraph(GraphCanvas<?> graphCanvas) {
if (vertex.color != null) node.setAttribute("ui.color", vertex.color);
}
for (var edge : graphCanvas.edges) {
Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), IdHelper.getNodeId(edge.from), IdHelper.getNodeId(edge.to), true);
Edge graphEdge = graph.addEdge(IdHelper.getNewEdgeId(), IdHelper.getNodeId(edge.v1), IdHelper.getNodeId(edge.v2), edge.directed);
if (edge.label != null) graphEdge.setAttribute("label", edge.label);
if (edge.color != null) graphEdge.setAttribute("ui.color", edge.color);
}
Expand Down
64 changes: 47 additions & 17 deletions src/main/java/ee/ut/dendroloj/GraphCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,60 @@ public final class GraphCanvas<T> {

public void drawVertex(T vertex) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
vertices.add(new Vertex<>(vertex, vertex.toString(), null));
drawVertex(vertex, vertex.toString(), null);
}

public void drawVertex(T vertex, String label) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
vertices.add(new Vertex<>(vertex, label, null));
drawVertex(vertex, label, null);
}

public void drawVertex(T vertex, String label, Color color) {
if (vertex == null) throw new NullPointerException("Vertex must not be null");
vertices.add(new Vertex<>(vertex, label, color));
}

public void drawEdge(T from, T to) {
if (from == null || to == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(from, to, null, null));
/**
* Draws an undirected edge from vertex v1 to vertex v2.
*/
public void drawEdge(T v1, T v2) {
drawEdge(v1, v2, null, null);
}

/**
* Draws an undirected edge from vertex v1 to vertex v2.
*/
public void drawEdge(T v1, T v2, String label) {
drawEdge(v1, v2, label, null);
}

/**
* Draws an undirected edge from vertex v1 to vertex v2.
*/
public void drawEdge(T v1, T v2, String label, Color color) {
if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(false, v1, v2, label, color));
}

/**
* Draws a directed edge from vertex v1 to vertex v2.
*/
public void drawDirectedEdge(T v1, T v2) {
drawDirectedEdge(v1, v2, null, null);
}

public void drawEdge(T from, T to, String label) {
if (from == null || to == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(from, to, label, null));
/**
* Draws a directed edge from vertex v1 to vertex v2.
*/
public void drawDirectedEdge(T v1, T v2, String label) {
drawDirectedEdge(v1, v2, label, null);
}

public void drawEdge(T from, T to, String label, Color color) {
if (from == null || to == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(from, to, label, color));
/**
* Draws a directed edge from vertex v1 to vertex v2.
*/
public void drawDirectedEdge(T v1, T v2, String label, Color color) {
if (v1 == null || v2 == null) throw new NullPointerException("Target vertices must not be null");
edges.add(new Edge<>(true, v1, v2, label, color));
}

static final class Vertex<T> {
Expand All @@ -60,14 +88,16 @@ private Vertex(T vertex, String label, Color color) {
}

static final class Edge<T> {
public final T from;
public final T to;
public final boolean directed;
public final T v1;
public final T v2;
public final String label;
public final Color color;

private Edge(T from, T to, String label, Color color) {
this.from = from;
this.to = to;
private Edge(boolean directed, T v1, T v2, String label, Color color) {
this.directed = directed;
this.v1 = v1;
this.v2 = v2;
this.label = label;
this.color = color;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/GraafKatsed.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void main(String[] args) {
{4, 0, 2, -1, 6},
{-1, 2, 0, 3, -1},
{12, -1, -1, 0, -1},
{-1, -1, 5, 4, 0}
{-1, -1, 5, 4, 123}
};
Dendrologist.drawGraph(M, nimed);
// Dendrologist.drawGraph(M, null);
Expand Down

0 comments on commit 757270d

Please sign in to comment.