Crate force_graph[][src]

Expand description

A Rust implementation of the force-directed graph algorithm from Graphoon.

Example

use force_graph::{ForceGraph, Node, NodeData};

// create a force graph with default parameters
let mut graph = <ForceGraph>::new(Default::default());

// create nodes
let n1_idx = graph.add_node(NodeData {
    x: 250.0,
    y: 250.0,
    ..Default::default()
});
let n2_idx = graph.add_node(NodeData {
    x: 750.0,
    y: 250.0,
    ..Default::default()
});
let n3_idx = graph.add_node(NodeData {
    x: 250.0,
    y: 750.0,
    ..Default::default()
});
let n4_idx = graph.add_node(NodeData {
    x: 750.0,
    y: 750.0,
    ..Default::default()
});
let n5_idx = graph.add_node(NodeData {
    x: 500.0,
    y: 500.0,
    is_anchor: true,
    ..Default::default()
});

// set up links between nodes
graph.add_edge(n1_idx, n5_idx, Default::default());
graph.add_edge(n2_idx, n5_idx, Default::default());
graph.add_edge(n3_idx, n5_idx, Default::default());
graph.add_edge(n4_idx, n5_idx, Default::default());

// --- your game loop would start here ---

// draw edges with your own drawing function
fn draw_edge(x1: f32, y1: f32, x2: f32, y2: f32) {}

graph.visit_edges(|node1, node2, _edge| {
    draw_edge(node1.x(), node1.y(), node2.x(), node2.y());
});

// draw nodes with your own drawing function
fn draw_node(x: f32, y: f32) {}

graph.visit_nodes(|node| {
    draw_node(node.x(), node.y());
});

// calculate dt with your own timing function
let dt = 0.1;
graph.update(dt);

// --- your game loop would repeat here ---

Structs

Stores data associated with an edge that can be modified by the user.

The main force graph structure.

References a node in the ForceGraph. Can not be constructed by the user.

Stores data associated with a node that can be modified by the user.

Parameters to control the simulation of the force graph.

Type Definitions