mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 17:51:24 +01:00
refactor(es modules): migrate numberUtils
This commit is contained in:
parent
b425a9daf6
commit
ad252b54e6
63 changed files with 97 additions and 56 deletions
|
|
@ -22,11 +22,14 @@ import {
|
|||
isLand,
|
||||
shouldRegenerateGrid
|
||||
} from "./utils/graphUtils";
|
||||
import {rn, minmax, normalize} from "./utils/numberUtils";
|
||||
import {byId} from "./utils/shorthands";
|
||||
import "./components";
|
||||
|
||||
addGlobalListeners();
|
||||
|
||||
const d3 = window.d3;
|
||||
|
||||
window.fmg = {
|
||||
modules: {}
|
||||
};
|
||||
|
|
@ -51,8 +54,8 @@ rulers = new Rulers();
|
|||
biomesData = Biomes.getDefault();
|
||||
nameBases = Names.getNameBases(); // cultures-related data
|
||||
|
||||
color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
|
||||
lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
|
||||
// color = d3.scaleSequential(d3.interpolateSpectral); // default color scheme
|
||||
// lineGen = d3.line().curve(d3.curveBasis); // d3 line generator with default curve interpolation
|
||||
|
||||
// voronoi graph extension, cannot be changed after generation
|
||||
graphWidth = +byId("mapWidthInput").value;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import {rn} from "../utils/numberUtils";
|
||||
|
||||
export function drawLegend(name: string, data: unknown[]) {
|
||||
legend.selectAll("*").remove(); // fully redraw every time
|
||||
legend.attr("data", data.join("|")); // store data
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import {findCell} from "/src/utils/graphUtils";
|
||||
import {last} from "/src/utils/arrayUtils";
|
||||
import {rn} from "/src/utils/numberUtils";
|
||||
|
||||
export class Rulers {
|
||||
constructor() {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,16 @@
|
|||
import {TIME} from "../config/logging";
|
||||
import {createTypedArray} from "./arrayUtils";
|
||||
import {rn} from "./numberUtils";
|
||||
import {byId} from "./shorthands";
|
||||
|
||||
const Delaunator = window.Delaunator;
|
||||
const Voronoi = window.Voronoi;
|
||||
const graphWidth = window.graphWidth;
|
||||
const graphHeight = window.graphHeight;
|
||||
|
||||
// check if new grid graph should be generated or we can use the existing one
|
||||
export function shouldRegenerateGrid(grid) {
|
||||
const cellsDesired = +byId("pointsInput").dataset.cells;
|
||||
const cellsDesired = Number(byId("pointsInput")?.dataset.cells);
|
||||
if (cellsDesired !== grid.cellsDesired) return true;
|
||||
|
||||
const newSpacing = rn(Math.sqrt((graphWidth * graphHeight) / cellsDesired), 2);
|
||||
|
|
|
|||
21
src/utils/numberUtils.ts
Normal file
21
src/utils/numberUtils.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
// round value to d decimals
|
||||
export function rn(value: number, decimals: number = 0) {
|
||||
const multiplier = Math.pow(10, decimals);
|
||||
return Math.round(value * multiplier) / multiplier;
|
||||
}
|
||||
|
||||
export function minmax(value: number, min: number, max: number) {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
}
|
||||
|
||||
// return value clamped to [0, 100]
|
||||
export function lim(value: number) {
|
||||
return minmax(value, 0, 100);
|
||||
}
|
||||
|
||||
// normalization function
|
||||
export function normalize(val: number, min: number, max: number) {
|
||||
return minmax((val - min) / (max - min), 0, 1);
|
||||
}
|
||||
|
||||
// import {rn, minmax, lim, normalize} from '/src/utils/numberUtils';
|
||||
Loading…
Add table
Add a link
Reference in a new issue