Skip to content

Commit 305c105

Browse files
author
DavidQ
committed
Consolidate duplicate wrap math utility through shared mathUtils - PR_26140_040-consolidate-wrap-math-util
1 parent bdeb9bd commit 305c105

2 files changed

Lines changed: 9 additions & 25 deletions

File tree

games/Asteroids/utils/math.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
/*
2-
Toolbox Aid
3-
David Quesenberry
4-
03/22/2026
5-
math.js
6-
*/
7-
export { randomRange } from '../../../src/shared/utils/mathUtils.js';
1+
export {
2+
wrap,
3+
randomRange
4+
} from '../../../src/shared/utils/mathUtils.js';
85

96
export const TAU = Math.PI * 2;
10-
11-
export function wrap(value, max) {
12-
const low = Math.min(0, max);
13-
const high = Math.max(0, max);
14-
const span = high - low;
15-
if (span === 0) {
16-
return low;
17-
}
18-
19-
if (value >= low && value <= high) {
20-
return value;
21-
}
22-
23-
return ((((value - low) % span) + span) % span) + low;
24-
}

src/shared/utils/mathUtils.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export function near(a, b, epsilon = 0.5) {
1818
return Math.abs(a - b) <= epsilon;
1919
}
2020

21-
export function wrap(value, min, max) {
22-
const low = Math.min(min, max);
23-
const high = Math.max(min, max);
21+
export function wrap(value, min = 0, max) {
22+
const rangeMin = max === undefined ? 0 : min;
23+
const rangeMax = max === undefined ? min : max;
24+
const low = Math.min(rangeMin, rangeMax);
25+
const high = Math.max(rangeMin, rangeMax);
2426
const span = high - low;
2527
if (span === 0) {
2628
return low;

0 commit comments

Comments
 (0)