Refactor getMiddlePoint function to getCloseToEdgePoint

This commit is contained in:
Azgaar 2024-08-02 11:34:54 +02:00
parent ff27937cc4
commit e6741b3a3b
4 changed files with 40 additions and 21 deletions

View file

@ -131,9 +131,8 @@ window.BurgsAndStates = (() => {
while (burgsAdded < burgsNumber && spacing > 1) { while (burgsAdded < burgsNumber && spacing > 1) {
for (let i = 0; burgsAdded < burgsNumber && i < sorted.length; i++) { for (let i = 0; burgsAdded < burgsNumber && i < sorted.length; i++) {
if (cells.burg[sorted[i]]) continue; if (cells.burg[sorted[i]]) continue;
const cell = sorted[i], const cell = sorted[i];
x = cells.p[cell][0], const [x, y] = cells.p[cell];
y = cells.p[cell][1];
const s = spacing * gauss(1, 0.3, 0.2, 2, 2); // randomize to make placement not uniform const s = spacing * gauss(1, 0.3, 0.2, 2, 2); // randomize to make placement not uniform
if (burgsTree.find(x, y, s) !== undefined) continue; // to close to existing burg if (burgsTree.find(x, y, s) !== undefined) continue; // to close to existing burg
const burg = burgs.length; const burg = burgs.length;
@ -181,7 +180,7 @@ window.BurgsAndStates = (() => {
if (b.port) { if (b.port) {
b.population = b.population * 1.3; // increase port population b.population = b.population * 1.3; // increase port population
const [x, y] = getMiddlePoint(i, haven); const [x, y] = getCloseToEdgePoint(i, haven);
b.x = x; b.x = x;
b.y = y; b.y = y;
} }
@ -222,6 +221,23 @@ window.BurgsAndStates = (() => {
TIME && console.timeEnd("specifyBurgs"); TIME && console.timeEnd("specifyBurgs");
}; };
function getCloseToEdgePoint(cell1, cell2) {
const {cells, vertices} = pack;
const [x0, y0] = cells.p[cell1];
const commonVertices = cells.v[cell1].filter(vertex => vertices.c[vertex].some(cell => cell === cell2));
const [x1, y1] = vertices.p[commonVertices[0]];
const [x2, y2] = vertices.p[commonVertices[1]];
const xEdge = (x1 + x2) / 2;
const yEdge = (y1 + y2) / 2;
const x = rn(x0 + 0.95 * (xEdge - x0), 2);
const y = rn(y0 + 0.95 * (yEdge - y0), 2);
return [x, y];
}
const getType = (i, port) => { const getType = (i, port) => {
const cells = pack.cells; const cells = pack.cells;
if (port) return "Naval"; if (port) return "Naval";

View file

@ -395,7 +395,7 @@ window.Submap = (function () {
return; return;
} }
DEBUG && console.info(`Moving ${b.name} from ${cityCell} to ${newCell} near ${neighbor}.`); DEBUG && console.info(`Moving ${b.name} from ${cityCell} to ${newCell} near ${neighbor}.`);
[b.x, b.y] = b.port ? getMiddlePoint(newCell, neighbor) : cells.p[newCell]; [b.x, b.y] = b.port ? getCloseToEdgePoint(newCell, neighbor) : cells.p[newCell];
if (b.port) b.port = cells.f[neighbor]; // copy feature number if (b.port) b.port = cells.f[neighbor]; // copy feature number
b.cell = newCell; b.cell = newCell;
if (b.port && !isWater(pack, neighbor)) console.error("betrayal! negihbor must be water!", b); if (b.port && !isWater(pack, neighbor)) console.error("betrayal! negihbor must be water!", b);
@ -407,6 +407,23 @@ window.Submap = (function () {
}); });
} }
function getCloseToEdgePoint(cell1, cell2) {
const {cells, vertices} = pack;
const [x0, y0] = cells.p[cell1];
const commonVertices = cells.v[cell1].filter(vertex => vertices.c[vertex].some(cell => cell === cell2));
const [x1, y1] = vertices.p[commonVertices[0]];
const [x2, y2] = vertices.p[commonVertices[1]];
const xEdge = (x1 + x2) / 2;
const yEdge = (y1 + y2) / 2;
const x = rn(x0 + 0.95 * (xEdge - x0), 2);
const y = rn(y0 + 0.95 * (yEdge - y0), 2);
return [x, y];
}
// export // export
return {resample, findNearest}; return {resample, findNearest};
})(); })();

View file

@ -264,8 +264,8 @@ function editRoute(id) {
if (candidateRoutes.length) { if (candidateRoutes.length) {
const options = candidateRoutes.map(r => { const options = candidateRoutes.map(r => {
r.name = r.name || Routes.generateName(r); r.name = r.name || Routes.generateName(r);
r.length = r.length || getRouteLength(r.i); r.length = r.length || Routes.getLength(r.i);
const length = rn(r.length * distanceScale) + " " + unit; const length = rn(r.length * distanceScale) + " " + distanceUnitInput.value;
return `<option value="${r.i}">${r.name} (${length})</option>`; return `<option value="${r.i}">${r.name} (${length})</option>`;
}); });
alertMessage.innerHTML = /* html */ `<div>Route to join with: alertMessage.innerHTML = /* html */ `<div>Route to join with:

View file

@ -37,20 +37,6 @@ function getSegmentId(points, point, step = 10) {
return minSegment; return minSegment;
} }
// return center point of common edge of 2 pack cells
function getMiddlePoint(cell1, cell2) {
const {cells, vertices} = pack;
const commonVertices = cells.v[cell1].filter(vertex => vertices.c[vertex].some(cell => cell === cell2));
const [x1, y1] = vertices.p[commonVertices[0]];
const [x2, y2] = vertices.p[commonVertices[1]];
const x = (x1 + x2) / 2;
const y = (y1 + y2) / 2;
return [x, y];
}
function debounce(func, ms) { function debounce(func, ms) {
let isCooldown = false; let isCooldown = false;