Skip to main content

circular/
circular.rs

1//! Example demonstrating circular/bidirectional edges between nodes.
2//! 
3//! This example shows how parallel edges (edges between the same pair of nodes)
4//! are rendered with vertical offset to prevent overlapping.
5
6use ascii_petgraph::{BoxBorder, RenderedGraph};
7use petgraph::graph::DiGraph;
8
9fn main() {
10    // Create a graph with circular edges (T <-> U)
11    let mut graph: DiGraph<&str, &str> = DiGraph::new();
12
13    let t = graph.add_node("T: Task");
14    let u = graph.add_node("U: User");
15
16    // Bidirectional edges - these would overlap without parallel edge handling
17    graph.add_edge(t, u, "question");
18    graph.add_edge(u, t, "answer");
19
20    let mut rendered = RenderedGraph::builder()
21        .graph(graph)
22        .border_style(BoxBorder::Single)
23        .gravity(1.0)
24        .repulsion_constant(15000.0) // Increased to separate nodes horizontally
25        .build();
26
27    println!("Running physics simulation...");
28    rendered.run_simulation();
29    println!("Converged after {} iterations\n", rendered.iterations());
30
31    let grid = rendered.render_to_grid();
32    let (width, height) = grid.size();
33
34    println!("Grid size: {}x{}\n", width, height);
35
36    grid.print();
37}