<div id="euclidean"></div>
<div id="manhattan"></div>
<div id="minkowski_3"></div>
Voronoi = function(opts) {
container: opts.container || "voronoi",
distance_function: opts.distance_function,
num_verticies: opts.num_vericies || 8,
verbose: opts.verbose || 0,
var initRandomVerticies = function() {
for(var i = 0; i < my.num_verticies; i++) {
x: Math.floor(Math.random()*my.x),
y: Math.floor(Math.random()*my.y),
r: Math.floor((Math.random()*255)+1),
g: Math.floor((Math.random()*255)+1),
b: Math.floor((Math.random()*255)+1),
if(my.verbose) { console.log(my.verticies);}
var voronoi = function(distance_function) {
for(var y = 0; y < my.y; y++) {
for(var x = 0; x < my.x; x++) {
var dmin = distance_function(my.x,my.y);
for(var i = 0; i < my.verticies.length; i++ ) {
var cell = my.verticies[i];
var d = distance_function(cell.x -x, cell.y - y);
if(d < dmin) { dmin = d; j = i; }
my.voronoi_map[y] = my.voronoi_map[y] || [];
if(my.verbose) { console.log(x + "," + y + " is closest to " + my.verticies[j].x + "," + my.verticies[j].y + " with a dmin of " + dmin);}
var content = "<table id='voronoi_table'>";
for(var y =0; y < my.y; y++) {
for(var x = 0; x < my.x; x++) {
var cell = my.voronoi_map[y][x];
content += "<td style='background-color:rgb(" + cell.r + "," + cell.g + "," + cell.b + ")'</td>";
document.getElementById(my.container).innerHTML = content;
// Creates random rgb data for testing draw method
var initRandom = function() {
for(var y = 0; y < my.y; y++) {
for(var x = 0; x < my.x; x++) {
r: Math.floor((Math.random()*255)+1),
g: Math.floor((Math.random()*255)+1),
b: Math.floor((Math.random()*255)+1),
my.voronoi_map[y] = my.voronoi_map[y] || [];
my.voronoi_map[y][x] = cell;
voronoi(my.distance_function);
// Set up various distance functions
euclidean: function(delta_x, delta_y) {
return Math.sqrt(delta_x * delta_x + delta_y * delta_y);
manhattan: function(delta_x,delta_y) {
return Math.abs(delta_x) + Math.abs(delta_y);
minkowski_3: function(delta_x, delta_y) {
return 0.33 * ( Math.pow(Math.abs(delta_x), 3) +
Math.pow(Math.abs(delta_y), 3));
euclidean = new Voronoi({
distance_function: distance_functions.euclidean,
manhattan = new Voronoi({
distance_function: distance_functions.manhattan,
minkowski_3 = new Voronoi({
container: "minkowski_3",
distance_function: distance_functions.minkowski_3,