cgv_layout_fr: Fruchterman-Reingold Force-Directed Layout

View source: R/layout.R

cgv_layout_frR Documentation

Fruchterman-Reingold Force-Directed Layout

Description

Computes 3D (or 2D) node positions so that connected nodes settle at approximately equal distance ideal_len, while non-adjacent nodes repel each other. Implements the Fruchterman-Reingold algorithm with linearly-cooling temperature, fully vectorized over nodes and edges.

Usage

cgv_layout_fr(
  n_nodes,
  edges,
  n_iter = 300L,
  ideal_len = NULL,
  dim = 3L,
  seed = NULL,
  init = NULL,
  cool = 0.98,
  normalize = TRUE,
  verbose = FALSE
)

Arguments

n_nodes

Integer. Number of nodes.

edges

Two-column integer matrix (from, to), 1-based. Direction is ignored (forces are symmetric).

n_iter

Integer. Number of iterations (default 300).

ideal_len

Numeric. Target edge length. If NULL, defaults to n_nodes^(1/dim) * 0.8.

dim

Integer, 2 or 3. Output dimensionality (default 3).

seed

Optional integer for reproducible initialization.

init

Optional n_nodes x dim matrix of initial positions. If NULL, uses rnorm.

cool

Numeric in (0, 1]. Per-iteration temperature decay (default 0.98).

normalize

Logical. If TRUE, recenters and scales output to fit in [-15, 15] (matches the demos' camera setup). Default TRUE.

verbose

Logical. Print progress every 50 iterations.

Value

Numeric matrix n_nodes x dim of node coordinates.

Examples

edges <- cbind(c(1, 2, 3, 4), c(2, 3, 4, 1))
pos <- cgv_layout_fr(4, edges, n_iter = 200)

cgvR documentation built on May 12, 2026, 1:06 a.m.