From b1309eae71ae2be1d91dc032388517c54c14e18b Mon Sep 17 00:00:00 2001 From: Azgaar Date: Wed, 5 Sep 2018 22:26:15 +0300 Subject: [PATCH] v. 0.59.12b --- script.js | 81 +++++++++++-------------------------------------------- 1 file changed, 15 insertions(+), 66 deletions(-) diff --git a/script.js b/script.js index 57e4fe04..86613b76 100644 --- a/script.js +++ b/script.js @@ -16,7 +16,7 @@ // And particularry: // Migrate from d3-voronoi to mapbox-delunator or d3-delaunay - // Use typed array instead of array of objects + // Use typed arrays instead of array of objects // Get rid of jQuery as d3.js can almost all the same and more // Re-build UI on reactive approach, vue.js @@ -444,13 +444,8 @@ function fantasyMap() { console.time("placePoints"); points = []; const mod = rn((graphWidth + graphHeight) / 1500, 2); // screen size modifier - const radius = rn(5.2 * mod / graphSize, 2); // 5.2 is a radius to get 10k cells on 960 x 540 screen and size 1 - const sampler = poissonDiscSampler(graphWidth, graphHeight, radius); - while (sample = sampler()) { - const x = rn(sample[0], 2); - const y = rn(sample[1], 2); - points.push([x, y]); - } + const spacing = rn(7.5 * mod / graphSize, 2); // space between points before jirrering + points = getJitteredGrid(spacing); heights = new Uint8Array(points.length); console.timeEnd("placePoints"); } @@ -6313,66 +6308,20 @@ function fantasyMap() { console.timeEnd("loadMap"); } - // Poisson-disc sampling for a points - // Source: bl.ocks.org/mbostock/99049112373e12709381; Based on https://www.jasondavies.com/poisson-disc - function poissonDiscSampler(width, height, radius) { - var k = 5, // maximum number of points before rejection - radius2 = radius * radius, - R = 3 * radius2, - cellSize = radius * Math.SQRT1_2, - gridWidth = Math.ceil(width / cellSize), - gridHeight = Math.ceil(height / cellSize), - grid = new Array(gridWidth * gridHeight), - queue = [], - queueSize = 0, - sampleSize = 0; - return function() { - if (!sampleSize) return sample(Math.random() * width, Math.random() * height); - // Pick a random existing sample and remove it from the queue - while (queueSize) { - var i = Math.random() * queueSize | 0, - s = queue[i]; - // Make a new candidate between [radius, 2 * radius] from the existing sample. - for (let j = 0; j < k; ++j) { - var a = 2 * Math.PI * Math.random(), - r = Math.sqrt(Math.random() * R + radius2), - x = s[0] + r * Math.cos(a), - y = s[1] + r * Math.sin(a); - // Reject candidates that are outside the allowed extent, or closer than 2 * radius to any existing sample - if (0 <= x && x < width && 0 <= y && y < height && far(x, y)) return sample(x, y); - } - queue[i] = queue[--queueSize]; - queue.length = queueSize; + // get square grid with some jirrering + function getJitteredGrid(spacing) { + const radius = spacing / 2; // square radius + const jittering = radius * 0.9; // max deviation + const jitter = function() {return Math.random() * 2 * jittering - jittering;} + let points = []; + for (let y = radius; y < graphHeight; y += spacing) { + for (let x = radius; x < graphWidth; x += spacing) { + let xj = rn(x + jitter(), 2); + let yj = rn(y + jitter(), 2); + points.push([xj, yj]); } - }; - function far(x, y) { - var i = x / cellSize | 0, - j = y / cellSize | 0, - i0 = Math.max(i - 2, 0), - j0 = Math.max(j - 2, 0), - i1 = Math.min(i + 3, gridWidth), - j1 = Math.min(j + 3, gridHeight); - for (j = j0; j < j1; ++j) { - var o = j * gridWidth; - for (i = i0; i < i1; ++i) { - if (s = grid[o + i]) { - var s, - dx = s[0] - x, - dy = s[1] - y; - if (dx * dx + dy * dy < radius2) return false; - } - } - } - return true; - } - function sample(x, y) { - var s = [x, y]; - queue.push(s); - grid[gridWidth * (y / cellSize | 0) + (x / cellSize | 0)] = s; - ++sampleSize; - ++queueSize; - return s; } + return points; } // Hotkeys, see github.com/Azgaar/Fantasy-Map-Generator/wiki/Hotkeys