mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-19 18:41:23 +01:00
feat: zones - render zones as continuius line
This commit is contained in:
parent
492bcd9c9b
commit
a63a60c0ea
5 changed files with 169 additions and 50 deletions
|
|
@ -1892,37 +1892,7 @@ function drawZones() {
|
|||
}
|
||||
|
||||
function drawZone({i, cells, type, color}) {
|
||||
// find a path connecting all cells of zone
|
||||
const path = getZonePath(cells);
|
||||
if (!path) return;
|
||||
|
||||
function getZonePath(cells) {
|
||||
const used = new Set();
|
||||
const vertices = cells.map(c => pack.cells.v[c]).flat();
|
||||
const points = vertices.map(v => pack.vertices.p[v]);
|
||||
const boundary = getBoundaryPoints(points, used);
|
||||
return boundary.length > 2 ? "M" + boundary.join("L") + "Z" : null;
|
||||
}
|
||||
|
||||
function getBoundaryPoints(points, used) {
|
||||
const boundary = [];
|
||||
let currentPoint = points[0];
|
||||
|
||||
while (true) {
|
||||
boundary.push(currentPoint);
|
||||
used.add(currentPoint.toString());
|
||||
let nextPoint = findNextPoint(currentPoint, points, used);
|
||||
if (!nextPoint || nextPoint === boundary[0]) break;
|
||||
currentPoint = nextPoint;
|
||||
}
|
||||
|
||||
return boundary;
|
||||
}
|
||||
|
||||
function findNextPoint(current, points, used) {
|
||||
return points.find(p => !used.has(p.toString()) && Math.hypot(p[0] - current[0], p[1] - current[1]) < 20);
|
||||
}
|
||||
|
||||
const path = getVertexPath(cells);
|
||||
return `<path id="zone${i}" data-id="${i}" data-type="${type}" d="${path}" fill="${color}" />`;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue