Browse Source

added brute-force algorithm; experimenting with colours

Brandon Wong 7 years ago
parent
commit
342ac48c43
1 changed files with 125 additions and 1 deletions
  1. 125 1
      voronoi.js

+ 125 - 1
voronoi.js

@@ -66,9 +66,133 @@ function generateSites(n = 10) {
     return sites;
 }
 
-const sites = generateSites(15);
+function randIndivC(a = 0, b = 255) {
+    return Math.floor(Math.random() * (b-a) + a);
+}
+function cts(c) {
+    const r = c.r < 16 ? '0' + c.r.toString(16) : c.r.toString(16),
+        g = c.g < 16 ? '0' + c.g.toString(16) : c.g.toString(16),
+        b = c.b < 16 ? '0' + c.b.toString(16) : c.b.toString(16);
+    return '#' + r + g + b;
+}
+function primaryOrSecondary() {
+    const t = Math.random(),
+        w = Math.floor(Math.random() * 3);
+    if( t < 0.45 ) {
+        const r = w === 0 ? 255 : 0,
+            g = w === 1 ? 255 : 0,
+            b = w === 2 ? 255 : 0;
+        return {r, g, b};
+    }
+    else if( t < 0.9 ) {
+        const r = w !== 0 ? 255 : 0,
+            g = w !== 1 ? 255 : 0,
+            b = w !== 2 ? 255 : 0;
+        return {r, g, b};
+    }
+    else {
+        //return {r: 255, g:255, b:255};
+        const r = randIndivC(100, 255),
+            g = randIndivC(100, 255),
+            b = randIndivC(100, 255);
+        return {r, g, b};
+    }
+}
+function randomColor() {
+    const t = Math.random(),
+        w = Math.floor(Math.random() * 3);
+    if( t < 0.45 ) {
+        const r = w === 0 ? randIndivC(200, 255) : randIndivC(10, 100),
+            g = w === 1 ? randIndivC(200, 255) : randIndivC(10, 100),
+            b = w === 2 ? randIndivC(200, 255) : randIndivC(10, 100);
+        return {r, g, b};
+    }
+    else if( t < 0.9 ) {
+        const r = w !== 0 ? randIndivC(200, 255) : randIndivC(10, 100),
+            g = w !== 1 ? randIndivC(200, 255) : randIndivC(10, 100),
+            b = w !== 2 ? randIndivC(200, 255) : randIndivC(10, 100);
+        return {r, g, b};
+    }
+    else {
+        const r = randIndivC(100, 200),
+            g = randIndivC(100, 200),
+            b = randIndivC(100, 200);
+        return {r, g, b};
+    }
+}
+function mixedColor(base) {
+    let randr = Math.floor(Math.random() * 200 + 55),
+        randg = Math.floor(Math.random() * 200 + 55),
+        randb = Math.floor(Math.random() * 200 + 55);
+    if( base ) {
+        randr = Math.floor((base.r + randr) / 2);
+        randg = Math.floor((base.g + randg) / 2);
+        randb = Math.floor((base.b + randb) / 2);
+    }
+    return cts({r:randr, g:randg, b:randb});
+}
+
+const sites = generateSites(50).map(function assignRandomColour(site) {
+    //site.color = cts(randomColor());
+    //site.color = cts(primaryOrSecondary());
+    site.color = mixedColor(primaryOrSecondary());
+    site.sectors = [];
+    return site;
+});
 
 sites.forEach(function renderSites(site) {
     drawSite(site.x, site.y);
 });
 
+
+function bruteForceRow(row, sites) {
+    sites = sites.map(function addNewRow(site) {
+        site.sectors[row] = [];
+        return site;
+    })
+    for( let i = 0; i < canvwidth; i += 1 ) {
+        const point = {x:i, y:row},
+            cind = sites.slice(1).reduce(function check(closest, site, ind) {
+                const d = distance(site, point);
+                if( d < closest.d ) {
+                    return {c:(ind + 1), d};
+                }
+                return closest;
+            }, {c:0, d:distance(sites[0], point)});
+        sites[cind.c].sectors[row].push(point);
+    }
+    return sites;
+}
+function spot(x, y, colour) {
+    ctx.save();
+    ctx.fillStyle = colour;
+    ctx.fillRect(x, y, 1, 1);
+    ctx.restore();
+}
+
+function goRow(i, sites) {
+    const newSites = bruteForceRow(i, sites);
+    newSites.filter(x => x.sectors[i].length)
+        .forEach(function parse(leader) {
+            leader.sectors[i].forEach(function draw(place) {
+                spot(place.x, place.y, leader.color);
+            })
+        });
+    if( i < canvheight ) {
+        setTimeout(goRow, 10, i + 1, newSites);
+    }
+    else {
+        setTimeout(function action() {
+            // temp redraw sites
+            sites.forEach(function renderSites(site) {
+                drawSite(site.x, site.y);
+            });
+        }, 100);
+    }
+}
+goRow(0, sites);
+
+
+
+
+