Convert Figma logo to code with AI

gorhill logoJavascript-Voronoi

A Javascript implementation of Fortune's algorithm to compute Voronoi cells

1,050
169
1,050
15

Top Related Projects

An incredibly fast JavaScript library for Delaunay triangulation of 2D points

57,141

uBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.

Quick Overview

The gorhill/Javascript-Voronoi repository is a JavaScript implementation of Steven J. Fortune's algorithm to compute Voronoi diagrams. It provides a lightweight and efficient solution for generating Voronoi diagrams in web applications, allowing developers to create visually appealing geometric patterns and spatial partitioning.

Pros

  • Fast and efficient implementation of Fortune's algorithm
  • Easy to integrate into web projects
  • Supports custom site objects for flexible diagram generation
  • Includes a demo page for quick visualization and testing

Cons

  • Limited documentation and examples
  • Not actively maintained (last update was in 2013)
  • Lacks advanced features found in more comprehensive libraries
  • No built-in support for modern module systems (e.g., ES6 modules)

Code Examples

  1. Creating a Voronoi object and computing the diagram:
var voronoi = new Voronoi();
var bbox = {xl: 0, xr: 800, yt: 0, yb: 600};
var sites = [{x: 200, y: 200}, {x: 50, y: 250}, {x: 400, y: 100}];
var diagram = voronoi.compute(sites, bbox);
  1. Accessing Voronoi edges and cells:
diagram.edges.forEach(function(edge) {
    console.log("Edge:", edge.va, edge.vb);
});

diagram.cells.forEach(function(cell) {
    console.log("Cell site:", cell.site);
    console.log("Cell halfedges:", cell.halfedges);
});
  1. Clearing the Voronoi diagram:
voronoi.recycle(diagram);

Getting Started

To use the Javascript-Voronoi library in your project:

  1. Download the rhill-voronoi-core.js file from the repository.
  2. Include the script in your HTML file:
<script src="path/to/rhill-voronoi-core.js"></script>
  1. Create a Voronoi object and compute a diagram:
var voronoi = new Voronoi();
var bbox = {xl: 0, xr: 800, yt: 0, yb: 600};
var sites = [{x: 100, y: 100}, {x: 200, y: 200}, {x: 300, y: 300}];
var diagram = voronoi.compute(sites, bbox);

// Use the diagram object to access Voronoi edges and cells
console.log(diagram.edges);
console.log(diagram.cells);

Competitor Comparisons

An incredibly fast JavaScript library for Delaunay triangulation of 2D points

Pros of Delaunator

  • Faster performance for large datasets
  • Smaller codebase, making it easier to maintain and integrate
  • Focuses specifically on Delaunay triangulation, which can be more efficient for certain use cases

Cons of Delaunator

  • Limited to Delaunay triangulation, while Javascript-Voronoi offers both Voronoi diagrams and Delaunay triangulation
  • Less feature-rich compared to Javascript-Voronoi
  • May require additional processing to generate Voronoi diagrams if needed

Code Comparison

Javascript-Voronoi:

var voronoi = new Voronoi();
var bbox = {xl: 0, xr: 800, yt: 0, yb: 600};
var sites = [{x: 200, y: 200}, {x: 50, y: 250}, {x: 400, y: 100}];
var diagram = voronoi.compute(sites, bbox);

Delaunator:

const points = [[200, 200], [50, 250], [400, 100]];
const delaunay = Delaunator.from(points);
const triangles = delaunay.triangles;

Both libraries provide efficient solutions for computational geometry problems, but they cater to different specific needs. Javascript-Voronoi offers a more comprehensive solution for both Voronoi diagrams and Delaunay triangulation, while Delaunator focuses on optimized Delaunay triangulation for larger datasets.

57,141

uBlock Origin - An efficient blocker for Chromium and Firefox. Fast and lean.

Pros of uBlock

  • Actively maintained with frequent updates
  • Large user base and community support
  • Comprehensive ad-blocking and privacy protection features

Cons of uBlock

  • More complex codebase due to its extensive functionality
  • Higher resource usage compared to simpler libraries

Code Comparison

uBlock (content blocker):

µBlock.staticNetFilteringEngine.matchString(
    fctxt,
    requestType,
    requestURL
);

Javascript-Voronoi (Voronoi diagram generator):

Voronoi.prototype.compute = function(sites, bbox) {
    this.reset();
    this.sites = sites;
    this.bbox = bbox;
    this.fortune(sites);
};

Key Differences

  • Purpose: uBlock is a content blocker, while Javascript-Voronoi generates Voronoi diagrams
  • Complexity: uBlock has a more extensive codebase with multiple components
  • Maintenance: uBlock is actively maintained, while Javascript-Voronoi hasn't been updated recently
  • Community: uBlock has a larger user base and more contributors
  • Performance impact: uBlock may have a higher impact on browser performance due to its real-time filtering

Use Cases

  • uBlock: Web browsing, ad-blocking, privacy protection
  • Javascript-Voronoi: Computational geometry, graphics, data visualization

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Javascript-Voronoi

A Javascript implementation of Steven J. Fortune's algorithm to efficiently compute Voronoi diagrams. The Voronoi object's purpose is to solely compute a Voronoi diagram, it is completely standalone, with no dependency on external code: it contains no rendering code: that is left to the user of the library.

Core files

  • rhill-voronoi-core.js

Where the Voronoi object is implemented. This is a standalone library, there is no dependency.

  • rhill-voronoi-core.min.js

The minimized version (using YUI compressor)

Demo files

  • rhill-voronoi-demo1.html
  • rhill-voronoi-demo2.html
  • rhill-voronoi-demo3.php
  • rhill-voronoi-demo4.html
  • rhill-voronoi-demo5.html

Demo pages to demonstrate usage of the Voronoi object.

  • excanvas/*

Used by demo pages.

ExplorerCanvas, giving pre-HTML5 Internet Explorer the ability to make sense of HTML5's canvas element. Pulled from http://code.google.com/p/explorercanvas/

  • mootools/*

Used by rhill-voronoi-demo3.php

Main object: Voronoi

A Javascript object which allows to compute a Voronoi diagram. The Voronoi object doesn't render the resulting Voronoi diagram, the user is responsible for rendering the diagram.

Usage

Roughly:

var voronoi = new Voronoi();
var bbox = {xl: 0, xr: 800, yt: 0, yb: 600}; // xl is x-left, xr is x-right, yt is y-top, and yb is y-bottom
var sites = [ {x: 200, y: 200}, {x: 50, y: 250}, {x: 400, y: 100} /* , ... */ ];

// a 'vertex' is an object exhibiting 'x' and 'y' properties. The
// Voronoi object will add a unique 'voronoiId' property to all
// sites. The 'voronoiId' can be used as a key to lookup the associated cell
// in diagram.cells.

var diagram = voronoi.compute(sites, bbox);

The returned 'diagram' variable is a Javascript object with the following properties:

diagram.vertices

An array of unordered, unique Voronoi.Vertex objects making up the Voronoi diagram. Each Voronoi.Vertex object in the list is shared by many Voronoi.Edge objects.

diagram.edges

An array of unordered, unique Voronoi.Edge objects making up the Voronoi diagram. Voronoi.Edges are defined by two vertices, va and vb, which vertices are shared by connected edges. This mean that if you change one vertex belonging to an edge, other connected edges will also be changed.

diagram.cells

An array of Voronoi.Cell objects making up the Voronoi diagram. A Voronoi.Cell object might have an empty array of halfedges, meaning no Voronoi cell could be computed for a particular cell.

diagram.execTime

The time it took to compute the Voronoi diagram, in milliseconds.

Added on October 12, 2013: In order to help improve performance, Voronoi.recycle() has been added to allow the recycling of a returned Voronoi diagram. Usage:

var diagram;
...

// some kind of loop starting here (whether outright or through a timer)
...

voronoi.recycle(diagram);
// diagram.vertices, diagram.edges and diagram.cells can no longer be used!
diagram = voronoi.compute(sites, bbox);

// do stuff with content of `diagram`
...

This new method helps performance significantly when re-computing a Voronoi diagram, as it saves on memory allocation, and associated garbage collection.

Public objects

Voronoi

The Voronoi object which computes a Voronoi diagram.

Voronoi.Vertex
  • x: no explanation required.

  • y: no explanation required.

Voronoi.Edge
  • lSite: the Voronoi site object at the left of this Voronoi.Edge object. The site object is just a reference to a site in the array of sites supplied by the user when Voronoi.compute() was called.

  • rSite: the Voronoi site object at the right of this Voronoi.Edge object (can be null, when this is a border edge). The site object is just a reference to a site in the array of sites supplied by the user when Voronoi.compute() was called.

  • va: a Voronoi.Vertex object with an x and a y property defining the start point (relative to the Voronoi site on the left) of this Voronoi.Edge object.

  • vb: a Voronoi.Vertex object with an x and a y property defining the end point (relative to Voronoi site on the left) of this Voronoi.Edge object.

Voronoi.Cell
  • site: the Voronoi site object associated with the Voronoi cell.

  • halfedges: an array of Voronoi.Halfedge objects, ordered counterclockwise, defining the polygon for this Voronoi cell.

Voronoi.Halfedge
  • site: the Voronoi site object owning this Voronoi.Halfedge object.

  • edge: a reference to the unique Voronoi.Edge object underlying this Voronoi.Halfedge object.

  • getStartpoint(): a method returning a Voronoi.Vertex of the start point of this halfedge. Keep in mind halfedges are always counterclockwise.

  • getEndpoint(): a method returning a Voronoi.Vertex object with an x and a y property for the end point of this halfedge. Keep in mind halfedges are always counterclockwise.

License

Copyright (c) 2010-2013 Raymond Hill https://github.com/gorhill/Javascript-Voronoi

Licensed under The MIT License http://en.wikipedia.org/wiki/MIT_License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.