From ec236d146b663f5c1c35848a52d92b4e1bde9f32 Mon Sep 17 00:00:00 2001 From: Azgaar Date: Fri, 6 Sep 2024 02:37:44 +0200 Subject: [PATCH] refactor: drawCoastline --- modules/renderers/draw-coastline.js | 34 ++++++++--------------------- utils/pathUtils.js | 5 +++++ 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/modules/renderers/draw-coastline.js b/modules/renderers/draw-coastline.js index 43093f88..09ab07a3 100644 --- a/modules/renderers/draw-coastline.js +++ b/modules/renderers/draw-coastline.js @@ -15,20 +15,26 @@ function drawCoastline() { for (const i of cells.i) { const startFromEdge = !i && cells.h[i] >= 20; if (!startFromEdge && cells.t[i] !== -1 && cells.t[i] !== 1) continue; // non-edge cell + const f = cells.f[i]; if (used[f]) continue; // already connected if (features[f].type === "ocean") continue; // ocean cell const type = features[f].type === "lake" ? 1 : -1; // type value to search for - const start = findStart(i, type); - if (start === -1) continue; // cannot start here - let vchain = connectVertices(start, type); + const ofSameType = cellId => cells.t[cellId] === type || cellId >= n; + + const startingVertex = findStart(i, type); + if (startingVertex === -1) continue; // cannot start here + + let vchain = connectVertices({vertices, startingVertex, ofSameType}); if (features[f].type === "lake") relax(vchain, 1.2); used[f] = 1; + let points = clipPoly( vchain.map(v => vertices.p[v]), 1 ); + const area = d3.polygonArea(points); // area with lakes/islands if (area > 0 && features[f].type === "lake") { points = points.reverse(); @@ -81,28 +87,6 @@ function drawCoastline() { return index === -1 ? index : cells.v[i][index]; } - // connect vertices to chain - function connectVertices(start, t) { - const chain = []; // vertices chain to form a path - for (let i = 0, current = start; i === 0 || (current !== start && i < 50000); i++) { - const prev = chain[chain.length - 1]; // previous vertex in chain - chain.push(current); // add current vertex to sequence - const c = vertices.c[current]; // cells adjacent to vertex - const v = vertices.v[current]; // neighboring vertices - const c0 = c[0] >= n || cells.t[c[0]] === t; - const c1 = c[1] >= n || cells.t[c[1]] === t; - const c2 = c[2] >= n || cells.t[c[2]] === t; - if (v[0] !== prev && c0 !== c1) current = v[0]; - else if (v[1] !== prev && c1 !== c2) current = v[1]; - else if (v[2] !== prev && c0 !== c2) current = v[2]; - if (current === chain[chain.length - 1]) { - ERROR && console.error("Next vertex is not found"); - break; - } - } - return chain; - } - // move vertices that are too close to already added ones function relax(vchain, r) { const p = vertices.p, diff --git a/utils/pathUtils.js b/utils/pathUtils.js index 34f9ee14..83101f60 100644 --- a/utils/pathUtils.js +++ b/utils/pathUtils.js @@ -159,6 +159,11 @@ function connectVertices({vertices, startingVertex, ofSameType, addToChecked, cl else if (v2 !== previous && c2 !== c3) next = v2; else if (v3 !== previous && c1 !== c3) next = v3; + if (!vertices.c[next]) { + ERROR && console.error("ConnectVertices: next vertex is out of bounds"); + break; + } + if (next === current) { ERROR && console.error("ConnectVertices: next vertex is not found"); break;