summaryrefslogtreecommitdiff
path: root/apps/web/src/utils/math.ts
diff options
context:
space:
mode:
authorMaze Winther <mazewinther@gmail.com>2026-03-25 16:47:22 +0100
committerMaze Winther <mazewinther@gmail.com>2026-03-29 15:48:22 +0200
commit8db3bead13441a5882d8689dfc6a9f09c82375d9 (patch)
treee9d0cafa93904ffcbec3188c37961bcc16c6422d /apps/web/src/utils/math.ts
parent39ea298a9ccba3104c549c0ca079c231981811f5 (diff)
downloadopencut-8db3bead13441a5882d8689dfc6a9f09c82375d9.tar.gz
feat: masks, properties refactor, shaders, storage migrations, and more
Diffstat (limited to 'apps/web/src/utils/math.ts')
-rw-r--r--apps/web/src/utils/math.ts45
1 files changed, 45 insertions, 0 deletions
diff --git a/apps/web/src/utils/math.ts b/apps/web/src/utils/math.ts
index d45e71c0..24f9eaf4 100644
--- a/apps/web/src/utils/math.ts
+++ b/apps/web/src/utils/math.ts
@@ -10,6 +10,41 @@ export function clamp({
return Math.max(min, Math.min(max, value));
}
+export function clampRound({
+ value,
+ min,
+ max,
+}: {
+ value: number;
+ min: number;
+ max: number;
+}): number {
+ return Math.round(clamp({ value, min, max }));
+}
+
+function getFractionDigitsForStep({ step }: { step: number }): number {
+ const normalizedStep = step.toString().toLowerCase();
+ if (normalizedStep.includes("e-")) {
+ return Number(normalizedStep.split("e-")[1] ?? 0);
+ }
+ const [, fractionalPart = ""] = normalizedStep.split(".");
+ return fractionalPart.length;
+}
+
+export function snapToStep({
+ value,
+ step,
+}: {
+ value: number;
+ step: number;
+}): number {
+ if (step <= 0) return value;
+ const snappedValue = Math.round(value / step) * step;
+ return Number(
+ snappedValue.toFixed(getFractionDigitsForStep({ step })),
+ );
+}
+
export function isNearlyEqual({
leftValue,
rightValue,
@@ -22,6 +57,16 @@ export function isNearlyEqual({
return Math.abs(leftValue - rightValue) <= epsilon;
}
+export function formatNumberForDisplay({
+ value,
+ maxFractionDigits = 6,
+}: {
+ value: number;
+ maxFractionDigits?: number;
+}): string {
+ return Number(value.toFixed(maxFractionDigits)).toString();
+}
+
export function evaluateMathExpression({
input,
}: {