poisson

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 2 Imported by: 0

README

Poisson Disk Sampling

An implementation of Bridson's algorithm for Poisson sampling.

This generates points with a minimum distance between them. This can give an organic feel to the resulting distribution.

Based on code and information from https://sighack.com/post/poisson-disk-sampling-bridsons-algorithm.

Usage

package main

import (
	"github.com/derekmu/poisson"
	"log"
	"math/rand/v2"
	"time"
)

func main() {
	points := poisson.Sample2D(10.0, 10, &poisson.Bounds{
		MinX: 0,
		MinY: 0,
		MaxX: 100,
		MaxY: 100,
	}, rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano()>>1)))

	for i, point := range points {
		log.Printf("Point #%d: %+v", i+1, point)
	}
}

Example

Here's an example of a Voronoi diagram used to generate a texture with points generated from this library:

img.png

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bounds

type Bounds struct {
	MinX float64
	MinY float64
	MaxX float64
	MaxY float64
}

Bounds is a rectangular area.

type Point

type Point struct {
	X float64
	Y float64
}

Point is a 2D point.

func Sample2D

func Sample2D(distance float64, kTries int, bounds Bounds, start *Point, source rand.Source) []Point

Sample2D generates a set of 2D points using Poisson-disc sampling, which ensures that no two points are closer than a specified minimum distance.

Parameters:

  • distance: The minimum distance between any two points.
  • kTries: The number of attempts to generate a new point around an active point before removing the active point from consideration.
  • bounds: The rectangular bounds within which points should be generated.
  • start: An optional starting point. If nil, a random point within the bounds is used.
  • source: A random source for repeatability.

Returns:

  • A slice of points that satisfy the Poisson-disc sampling criteria.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL