diff options
author | sanine <sanine.not@pm.me> | 2022-05-29 14:50:55 -0500 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-05-29 14:50:55 -0500 |
commit | 3bc2838360627f78a91a7cad81f3bc97711410c2 (patch) | |
tree | 54d260b45895c1923a785efce817b94836660f35 /modules/Terrain.js | |
parent | d35e02ecdcef6d4f120bd572f2aae36b467b7761 (diff) |
refactor: move everything into src/ subdirectory
Diffstat (limited to 'modules/Terrain.js')
-rw-r--r-- | modules/Terrain.js | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/modules/Terrain.js b/modules/Terrain.js deleted file mode 100644 index ca0ab77..0000000 --- a/modules/Terrain.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; - -import Voronoi from './3rdparty/rhill-voronoi-core.js'; - -import { useAverage } from './Util.js'; -import { QuadTree } from './Geometry.js'; - - -/* from here on up, we always assume that points live in the range [(0,0), (1,1)) */ - -function lloydRelax(point_set, density) { - /* setup quadtree and averages */ - let tree = new QuadTree(1,1); - let averages = {}; - for (let i=0; i<point_set.length; i++) { - const point = point_set[i]; - point.index = i; - tree.insert(point); - - let [avg, append] = useAverage(); - const cent_x = { avg, append }; - [avg, append] = useAverage(); - const cent_y = { avg, append }; - averages[i] = { cent_x, cent_y }; - } - - /* compute average centroids */ - for (let x=0; x<1; x += 1/density) { - for (let y=0; y<1; y += 1/density) { - const point = { x, y }; - const closest = tree.closest(point); - const { cent_x, cent_y } = averages[closest.index]; - cent_x.append(point.x); - cent_y.append(point.y); - } - } - - /* return centroid points */ - const result = []; - for (let i=0; i<point_set.length; i++) { - const point = { x: averages[i].cent_x.avg(), y: averages[i].cent_y.avg() }; - result.push(point); - } - return result; -} - - -class Terrain { - constructor() { - const N_SEED_POINTS = 2**14; - const N_RELAX_ITERATIONS = 1; - const RELAX_DENSITY = 400; - const randomPoint = () => ({x: Math.random(), y: Math.random()}); - - let seed_points = []; - for (let i=0; i<N_SEED_POINTS; i++) seed_points.push(randomPoint()); - - for (let i=0; i<N_RELAX_ITERATIONS; i++) - lloydRelax(seed_points, RELAX_DENSITY); - - const v = new Voronoi(); - this.graph = v.compute(seed_points, {xl: 0, xr: 1, yt: 0, yb: 1}); - - this.tree = new QuadTree(1,1); - for (let v of this.graph.vertices) this.tree.insert(v); - } - - - renderGrid(ct) { - ct.lineWidth = 0.001; - for (let edge of this.graph.edges) { - ct.beginPath(); - ct.moveTo(edge.va.x, edge.va.y); - ct.lineTo(edge.vb.x, edge.vb.y); - ct.closePath(); - ct.stroke(); - } - } -} - -export { lloydRelax }; -export default Terrain; |