Visualize.Geo.Voronoi (Visualize v0.2.25)

Copy Markdown View Source

Voronoi diagram for spatial partitioning.

Computes the Voronoi tessellation from a Delaunay triangulation. Each cell contains all points closer to its site than to any other site.

Examples

points = [{0, 0}, {100, 0}, {50, 100}, {25, 50}, {75, 50}]

voronoi = Visualize.Geo.Voronoi.new(points)
  |> Visualize.Geo.Voronoi.bounds([0, 0, 200, 200])

# Get cell for a specific point
cell = Visualize.Geo.Voronoi.cell(voronoi, 0)

# Render all cells as SVG paths
paths = Visualize.Geo.Voronoi.render_cells(voronoi)

# Find which cell contains a point
i = Visualize.Geo.Voronoi.find(voronoi, {30, 30})

Summary

Functions

Sets the clipping bounds [x0, y0, x1, y1]

Returns the Voronoi cell polygon for a given point index

Returns a cell's vertices without clipping to bounds.

Returns all Voronoi cells as lists of vertices

Returns the edges of the Voronoi diagram

Finds which cell contains the given point

Creates a new Voronoi diagram from points

Generates SVG path data for a specific cell

Generates SVG path data for all cells

Generates SVG with clipPath for rendering cells.

Generates SVG path data for all Voronoi edges

Types

bounds()

@type bounds() :: {number(), number(), number(), number()}

point()

@type point() :: {number(), number()}

t()

@type t() :: %Visualize.Geo.Voronoi{
  bounds: bounds() | nil,
  circumcenters: [point()],
  delaunay: Visualize.Geo.Delaunay.t()
}

Functions

bounds(voronoi, arg2)

@spec bounds(t(), [number()] | bounds()) :: t()

Sets the clipping bounds [x0, y0, x1, y1]

cell(voronoi, point_index)

@spec cell(t(), non_neg_integer()) :: [point()]

Returns the Voronoi cell polygon for a given point index

cell_unclipped(voronoi, point_index)

@spec cell_unclipped(t(), non_neg_integer()) :: [point()]

Returns a cell's vertices without clipping to bounds.

Useful when you want to use SVG clipPath for rendering.

cells(voronoi)

@spec cells(t()) :: [[point()]]

Returns all Voronoi cells as lists of vertices

edges(voronoi)

@spec edges(t()) :: [{point(), point()}]

Returns the edges of the Voronoi diagram

find(voronoi, arg)

@spec find(t(), point()) :: integer()

Finds which cell contains the given point

new(points)

@spec new([point()]) :: t()

Creates a new Voronoi diagram from points

render_cell(voronoi, point_index)

@spec render_cell(t(), non_neg_integer()) :: String.t()

Generates SVG path data for a specific cell

render_cells(voronoi)

@spec render_cells(t()) :: String.t()

Generates SVG path data for all cells

render_cells_clipped(voronoi, opts \\ [])

@spec render_cells_clipped(t(), keyword()) :: String.t()

Generates SVG with clipPath for rendering cells.

Uses SVG's native clipPath instead of algorithmic clipping. More efficient for rendering, but doesn't compute actual clipped vertices.

Options

  • :clip_id - ID for the clipPath element (default: "voronoi-clip")
  • :stroke - Stroke color for cell edges (default: "#ccc")
  • :fill - Fill color for cells (default: "none")

Example

voronoi = Voronoi.new(points) |> Voronoi.bounds([0, 0, 400, 400])
svg = Voronoi.render_cells_clipped(voronoi)

render_edges(voronoi)

@spec render_edges(t()) :: String.t()

Generates SVG path data for all Voronoi edges