Coder Social home page Coder Social logo

quasivoronoi's Introduction

QuasiVoronoiGrid

Standalone class to split up a grid into irregular regions using the basic principles of Voronoi diagrams. Useful for biome/zone generation in procedurally generated maps. Not an exact implementation, leaves a few artifacts occasionally.

Usage

See http://codepen.io/njmcode/pen/ptJhC for an example using Canvas.

/**
 * Some consts and canvas setup
**/
var CELL_SIZE = 10,
    NUM_REGIONS = 30,
    c = document.getElementById('my-canvas'),
    ctx = c.getContext('2d');

/**
 * Util methods
**/
function drawCell(x, y, color) {
  ctx.fillStyle = color;
  ctx.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}

function rndColor() {
  return '#' + Math.floor(Math.random() * 16777215).toString(16);
}

/**
 * Create a new Voronoi and set the canvas to its size
**/
var Voronoi = new QuasiVoronoiGrid({
  numRegions: NUM_REGIONS
});

var w = Voronoi.width,
    h = Voronoi.height;

c.width = w * CELL_SIZE;
c.height = h * CELL_SIZE;

/**
 * Store a random color for each region
**/
var points = Voronoi.origins,
    regionColors = [];
for (var i = 0; i < points.length; i++) {
    regionColors.push(rndColor());
}

/**
 * Render out the cells of the grid
**/
for (var y = 0; y < h; y++) {
    for (var x = 0; x < w; x++) {
      drawCell(x, y, regionColors[Voronoi.getOrigin(x, y)]);
    }
}

See the source comments for further documentation.

quasivoronoi's People

Contributors

njmcode avatar njmcode-rehab avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.