6 releases

0.3.2 Jul 3, 2021
0.3.1 Jul 3, 2021
0.2.0 Dec 27, 2020
0.1.1 Apr 30, 2020

#26 in #force

Download history 9/week @ 2024-09-21

112 downloads per month

MIT license

14KB
255 lines

force-graph-rs

Crates.io Docs Status

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

See the API documentation and run the example with cargo run --example demo.


lib.rs:

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 ---

Dependencies

~2MB
~32K SLoC