A fun, real-time interactive Voronoi stippling visualizer written in Go!
This project uses Lloyd's Relaxation Algorithm to distribute points across an image based on its luminance (darkness). The points gracefully animate and settle into a beautiful stippling pattern, creating more dots in darker areas and fewer dots in lighter areas.
- Real-time Animation: Smooth interpolation of points using
raylib-go. - Image-Driven Density: Uses an underlying image (
gown.jpg) to calculate weighted centroids. Darker pixels exert a stronger "gravitational pull" on the Voronoi sites. - Optimized Rendering: Pre-calculates a luminance map and uses bounding-box point-in-polygon checks for high performance.
- Interactive: Press
Enterto instantly randomize and restart the simulation!
- Luminance Mapping: The app loads an image and converts it into a 1D array of luminance values (0.0 to 1.0) for ultra-fast pixel lookups.
- Initial Placement: Random dots are generated. Dots falling on lighter pixels are rejected, naturally biasing the initial distribution toward darker areas of the image.
- Voronoi Diagram: The
pzsz/voronoilibrary calculates the Voronoi cells for the current sites. - Weighted Centroids: For each cell, the algorithm calculates the "Center of Mass" (weighted by the luminance map).
- Relaxation (Lloyd's Algorithm): The sites are smoothly interpolated (
Lerp) toward their new weighted centroids. This loop runs every frame, creating a mesmerizing animation as the points snap into their optimal stippling positions.
This project uses raylib-go, which requires the Raylib C library to be installed on your system.
sudo apt-get install libraylib-devbrew install raylibFollow the raylib-go Windows setup guide (usually involves installing via MSYS2 or using the pre-compiled binaries).
-
Clone the repository:
git clone https://github.com/yourusername/voronoi-stippling.git cd voronoi-stippling -
Add an image: Place a
.jpgimage in the root directory and name itgown.jpg. (Note: The screen size is hardcoded to736x736. For best results, use a square image of this resolution, or the app will scale/sample it!) -
Install dependencies:
go mod init voronoi-stippling go get github.com/gen2brain/raylib-go/raylib go get github.com/pzsz/voronoi go mod tidy
-
Run the project: Pass the desired number of dots as a command-line argument (e.g.,
2000):go run main.go 2000
| Key | Action |
|---|---|
Enter |
Randomize dots and restart the relaxation animation. |
Esc / Close Window |
Exit the application. |
- raylib-go - Go bindings for raylib (rendering and window management).
- pzsz/voronoi - Go implementation of Fortune's algorithm for Voronoi diagrams.
- Standard Go
imageandimage/jpegpackages for image decoding.
This project is open-source and available under the MIT License. Feel free to use, modify, and distribute!
Made with ❤️ and Go
