ds-heightmap
Advanced tools
Comparing version 1.2.2 to 1.2.4
@@ -10,2 +10,60 @@ "use strict"; | ||
}; | ||
// A simple implement of beta distribution: https://stackoverflow.com/a/13569020 | ||
function beta(alpha, beta) { | ||
const SG_MAGICCONST = 1 + Math.log(4.5); | ||
const alpha_gamma = gamma(alpha, 1); | ||
return alpha_gamma / (alpha_gamma + gamma(beta, 1)); | ||
function gamma(alpha, beta) { | ||
if (alpha > 1) { | ||
const ainv = Math.sqrt(2.0 * alpha - 1.0); | ||
const bbb = alpha - Math.log(4.0); | ||
const ccc = alpha + ainv; | ||
while (true) { | ||
const u1 = Math.random(); | ||
if (!(1e-7 < u1 && u1 < 0.9999999)) { | ||
continue; | ||
} | ||
const u2 = 1.0 - Math.random(); | ||
const v = Math.log(u1 / (1.0 - u1)) / ainv; | ||
const x = alpha * Math.exp(v); | ||
const z = u1 * u1 * u2; | ||
const r = bbb + ccc * v - x; | ||
if (r + SG_MAGICCONST - 4.5 * z >= 0.0 || r >= Math.log(z)) { | ||
return x * beta; | ||
} | ||
} | ||
} | ||
else if (alpha == 1.0) { | ||
let u = Math.random(); | ||
while (u <= 1e-7) { | ||
u = Math.random(); | ||
} | ||
return -Math.log(u) * beta; | ||
} | ||
else { | ||
let x = 0; | ||
while (true) { | ||
const u3 = Math.random(); | ||
const b = (Math.E + alpha) / Math.E; | ||
const p = b * u3; | ||
if (p <= 1.0) { | ||
x = Math.pow(p, 1.0 / alpha); | ||
} | ||
else { | ||
x = -Math.log((b - p) / alpha); | ||
} | ||
const u4 = Math.random(); | ||
if (p > 1.0) { | ||
if (u4 <= Math.pow(x, alpha - 1.0)) { | ||
break; | ||
} | ||
} | ||
else if (u4 <= Math.exp(-x)) { | ||
break; | ||
} | ||
} | ||
return x * beta; | ||
} | ||
} | ||
} | ||
function ds(config = DEFAULT_CONFIG) { | ||
@@ -38,14 +96,14 @@ const conf = { | ||
const p = side - 1; | ||
data[0][0] = Math.random() * depth; | ||
data[0][p] = Math.random() * depth; | ||
data[p][0] = Math.random() * depth; | ||
data[p][p] = Math.random() * depth; | ||
data[0][0] = beta(3, 3) * depth; | ||
data[0][p] = beta(3, 3) * depth; | ||
data[p][0] = beta(3, 3) * depth; | ||
data[p][p] = beta(3, 3) * depth; | ||
shape(side, side); | ||
const output = { data, max, min }; | ||
if (side !== width) { | ||
data.length = width; | ||
if (data.length !== width) { | ||
data.splice(width); | ||
} | ||
if (side !== height) { | ||
if (data[0].length !== height) { | ||
for (const col of data) { | ||
col.length = height; | ||
col.splice(height); | ||
} | ||
@@ -70,3 +128,3 @@ } | ||
} | ||
let n = randomize(corners.reduce((p, c) => p + c, 0) / corners.length, halfW + halfH); | ||
const n = randomize(corners.reduce((p, c) => p + c, 0) / corners.length, halfW + halfH); | ||
if (x < width && y < height) { | ||
@@ -89,11 +147,3 @@ if (n < min) | ||
return randomizer(base, range); | ||
const n = base + | ||
((((Math.random() - base / depth) * | ||
Math.max(base, depth - base) * | ||
range) / | ||
(side + side)) * | ||
rough * | ||
base) / | ||
depth; | ||
return n > depth ? depth : n < 0 ? 0 : n; | ||
return base + (Math.random() - base / depth) * range * rough; | ||
} | ||
@@ -126,3 +176,3 @@ function shape(sizeW, sizeH) { | ||
return; | ||
let n = randomize((data[x - halfW][y - halfH] + | ||
const n = randomize((data[x - halfW][y - halfH] + | ||
data[x + halfW][y - halfH] + | ||
@@ -129,0 +179,0 @@ data[x + halfW][y + halfH] + |
{ | ||
"name": "ds-heightmap", | ||
"version": "1.2.2", | ||
"version": "1.2.4", | ||
"description": "Use diamond-square algorithm to generate heightmaps.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -7,3 +7,3 @@ { | ||
"description": "Using diamond-square algorithm to generate heightmaps which stored in a 2D-array.", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"license": "MIT", | ||
@@ -17,3 +17,2 @@ "repository": { | ||
"ds_heightmap.js", | ||
"ds_heightmap_bg.js", | ||
"ds_heightmap.d.ts" | ||
@@ -24,7 +23,3 @@ ], | ||
"types": "ds_heightmap.d.ts", | ||
"sideEffects": false, | ||
"keywords": [ | ||
"diamond-square", | ||
"heightmap" | ||
] | ||
"sideEffects": false | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
74339
465