npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

poisson-disk

v1.1.1

Published

A fast Poisson Disk sampling algorithm for random 2D points generation

Downloads

2

Readme

Poisson Disk Sampling

A fast Poisson Disk sampling algorithm for random 2D points generation.

Introduction

The Poisson Disk sampling algorithm is used to create random points coordinates, so that each point is separated from all other points by a specified minimum distance. This result in a tighlty-packed and homogeneous set of points.

The implementation is based on this paper by Robert Bridson, and run in O(n) time.

Example

A sample of points generated in the full extent of a Canvas:

Points generated with the Poisson Disk algorithm

Each points is represented by a blue circle, and the minimum distance is visible in gray.

Usage

var PoissonDisk = require('poisson-disk');

var viewport = [0, 0, 100, 100];
var minDistance = 10;
var Sampling = new PoissonDisk(viewport, minDistance);

// Create a set of random points
var allPoints = Sampling.all();
console.log(allPoints);
// output: [{x: 12, y: 57}, {x: 96, y: 68}, ...]

Sampling.reset();

// Create each point one by one
var eachPoints = new Array(0);
while (true) {
  var point = Sampling.next();
  if (Sampling.done()) {
    break;
  }
  console.log(point);
  // output: {x: 54, y: 35}
  eachPoints.push(point);
}
console.log(eachPoints);
// output: [{x: 54, y: 35}, {x: 46, y: 62}, ...]

The Web demonstration in the example folder can be opened with raw.githack.

API

Constructor

new PoissonDisk(viewport, minimumDistance [, maxTries [, rng]])

The creator accepts 4 arguments:

  • viewport: An array of 4 values that defines the points bounding box (format: [xMin, yMin, xMax, yMax])

  • minimumDistance: The minimum distance between each points (minimum: 1)

  • maxTries: The maximum number of tries to generate a new point (default: 30)

  • rng: The random number generator, with output in [0, 1) (default: Math.random)

Creation Methods

PoissonDisk.next()

Returns the {x, y} coordinates of a new random point in the viewport. This point will be distant from all previously generated points by at least minimumDistance.

Returns null if it is not possible to create a new point that respect the minimum distance condition.

Note: The algorithm can not predict in advance if there is enough free space to create a new point. Therefore, make sure to test for null value when the methods is used in a loop.

PoissonDisk.all()

Returns an Array with the {x, y} coordinates of all random points created.

Utility Methods

PoissonDisk.reset()

Reset the internal state of the PoissonDisk sample, by removing all informations on previously generated points.

This internal reset is automatically executed when then all() method is called.

PoissonDisk.done()

Returns a Boolean that indicates if the sampling is finished, meaning that all values from the next() methods will be null

Data format

The points are Object with the following format: {x: number, y: number}.

Installation

The module can be installed from npm

npm install poisson-disk

It can also be installed by cloning the repository & including the poisson-disk.js file in your project.

Installation

You can install the module with npm

npm install poisson-disk

You can import the module with a CDN like unpkg

<script type="text/javascript" src="https://unpkg.com/poisson-disk@latest"></script>

You can clone the repository & include the poisson-disk.js file in your project:

git clone https://github.com/ogus/poisson-disk.git

License

This project is licensed under the WTFPL - see LICENSE for more details