@audio-ui/utils 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +135 -0
- package/dist/index.js +322 -0
- package/dist/index.js.map +1 -0
- package/package.json +10 -4
- package/.turbo/turbo-build$colon$pkg.log +0 -15
- package/CHANGELOG.md +0 -7
- package/src/geom.ts +0 -335
- package/src/index.ts +0 -3
- package/src/math.ts +0 -34
- package/src/std.ts +0 -49
- package/tsconfig.json +0 -7
- package/tsup.config.ts +0 -11
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
type Circle = Point & {
|
|
2
|
+
r: number;
|
|
3
|
+
};
|
|
4
|
+
type Size = {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
};
|
|
8
|
+
type Client = {
|
|
9
|
+
clientX: number;
|
|
10
|
+
clientY: number;
|
|
11
|
+
};
|
|
12
|
+
declare enum Axis {
|
|
13
|
+
T = 0,
|
|
14
|
+
R = 1,
|
|
15
|
+
B = 2,
|
|
16
|
+
L = 3
|
|
17
|
+
}
|
|
18
|
+
declare enum Corner {
|
|
19
|
+
TL = 0,
|
|
20
|
+
TR = 1,
|
|
21
|
+
BR = 2,
|
|
22
|
+
BL = 3
|
|
23
|
+
}
|
|
24
|
+
declare namespace Geom {
|
|
25
|
+
const outerTangentPoints: (a: Circle, b: Circle) => [Point, Point];
|
|
26
|
+
}
|
|
27
|
+
type Point = {
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
};
|
|
31
|
+
declare namespace Point {
|
|
32
|
+
const zero: () => Point;
|
|
33
|
+
const create: (x: number, y: number) => Point;
|
|
34
|
+
const clone: (point: Point) => Point;
|
|
35
|
+
const floor: (point: Point) => Point;
|
|
36
|
+
const length: (point: Point) => number;
|
|
37
|
+
const distance: (a: Point, b: Point) => number;
|
|
38
|
+
const add: (a: Point, b: Point) => Point;
|
|
39
|
+
const subtract: (a: Point, b: Point) => Point;
|
|
40
|
+
const scaleBy: (point: Point, scale: number) => Point;
|
|
41
|
+
const scaleTo: (point: Point, scale: number) => Point;
|
|
42
|
+
const fromClient: (object: {
|
|
43
|
+
clientX: number;
|
|
44
|
+
clientY: number;
|
|
45
|
+
}) => Point;
|
|
46
|
+
}
|
|
47
|
+
type Rect = Point & Size;
|
|
48
|
+
declare namespace Rect {
|
|
49
|
+
const Empty: Readonly<Rect>;
|
|
50
|
+
const corners: (rectangle: Rect) => Array<Point>;
|
|
51
|
+
const inflate: (rect: Rect, amount: number) => Rect;
|
|
52
|
+
const contains: (outer: Rect, inner: Rect) => boolean;
|
|
53
|
+
const isPointInside: (point: Point, rect: Rect) => boolean;
|
|
54
|
+
const intersect: (a: Rect, b: Rect) => boolean;
|
|
55
|
+
const axis: (rectangle: Rect, axisValue: Axis) => number;
|
|
56
|
+
const corner: (rectangle: Rect, cornerValue: Corner) => Point;
|
|
57
|
+
const center: (rectangle: Rect) => Point;
|
|
58
|
+
const isEmpty: (rectangle: Rect) => boolean;
|
|
59
|
+
const union: (a: Rect, b: Readonly<Rect>) => void;
|
|
60
|
+
}
|
|
61
|
+
interface AABB {
|
|
62
|
+
xMin: number;
|
|
63
|
+
xMax: number;
|
|
64
|
+
yMin: number;
|
|
65
|
+
yMax: number;
|
|
66
|
+
}
|
|
67
|
+
declare namespace AABB {
|
|
68
|
+
const width: (aabb: AABB) => number;
|
|
69
|
+
const height: (aabb: AABB) => number;
|
|
70
|
+
const from: (aabb: AABB, that: AABB) => void;
|
|
71
|
+
const extend: (aabb: AABB, offset: number) => void;
|
|
72
|
+
const padding: (aabb: AABB, [top, right, bottom, left]: Readonly<Padding>) => AABB;
|
|
73
|
+
const intersectPoint: (aabb: AABB, point: Point) => boolean;
|
|
74
|
+
const intersectThat: (aabb: AABB, that: AABB) => boolean;
|
|
75
|
+
const center: (aabb: AABB) => Point;
|
|
76
|
+
}
|
|
77
|
+
type Padding = [number, number, number, number];
|
|
78
|
+
declare namespace Padding {
|
|
79
|
+
const Identity: Readonly<Padding>;
|
|
80
|
+
}
|
|
81
|
+
declare namespace CohenSutherland {
|
|
82
|
+
const intersects: (xMin: number, xMax: number, yMin: number, yMax: number, x0: number, y0: number, x1: number, y1: number) => boolean;
|
|
83
|
+
}
|
|
84
|
+
interface ValueAxis {
|
|
85
|
+
valueToAxis(value: number): number;
|
|
86
|
+
axisToValue(axis: number): number;
|
|
87
|
+
}
|
|
88
|
+
declare namespace ValueAxis {
|
|
89
|
+
const Identity: ValueAxis;
|
|
90
|
+
const toClamped: (valueAxis: ValueAxis, min: number, max: number) => ValueAxis;
|
|
91
|
+
const createClamped: (min: number, max: number) => ValueAxis;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type int = number;
|
|
95
|
+
type unitValue = number;
|
|
96
|
+
type Optional<T> = T | undefined;
|
|
97
|
+
type Nullable<T> = T | null;
|
|
98
|
+
type Maybe<T> = T | undefined | null;
|
|
99
|
+
type ValueOrProvider<T> = T | (() => T);
|
|
100
|
+
type Procedure<T> = (value: T) => void;
|
|
101
|
+
type Func<U, T> = (value: U) => T;
|
|
102
|
+
type AnyFunc = (...args: any[]) => any;
|
|
103
|
+
type AssertType<T> = (value: unknown) => value is T;
|
|
104
|
+
type Exec = () => void;
|
|
105
|
+
declare const isDefined: <T>(value: Maybe<T>) => value is T;
|
|
106
|
+
declare const isUndefined: (value: unknown) => value is undefined;
|
|
107
|
+
declare const isNotUndefined: <T>(value: Optional<T>) => value is T;
|
|
108
|
+
declare const asDefined: <T>(value: Maybe<T>, fail?: ValueOrProvider<string>) => T;
|
|
109
|
+
declare const Unhandled: <R>(empty: never) => R;
|
|
110
|
+
declare const panic: (issue?: string | Error | unknown) => never;
|
|
111
|
+
declare const getOrProvide: <T>(value: ValueOrProvider<T>) => T;
|
|
112
|
+
declare const EmptyExec: Exec;
|
|
113
|
+
declare const EmptyProcedure: Procedure<any>;
|
|
114
|
+
declare function assertType<T>(_value: unknown): asserts _value is T;
|
|
115
|
+
|
|
116
|
+
declare const TAU: number;
|
|
117
|
+
declare const PI_HALF: number;
|
|
118
|
+
declare const PI_QUART: number;
|
|
119
|
+
declare const INVERSE_SQRT_2: number;
|
|
120
|
+
declare const clamp: (value: number, min: number, max: number) => number;
|
|
121
|
+
declare const clampUnit: (value: number) => unitValue;
|
|
122
|
+
declare const squashUnit: (value: unitValue, margin: unitValue) => unitValue;
|
|
123
|
+
declare const quantizeFloor: (value: number, interval: number) => number;
|
|
124
|
+
declare const quantizeCeil: (value: number, interval: number) => number;
|
|
125
|
+
declare const quantizeRound: (value: number, interval: number) => number;
|
|
126
|
+
declare const linear: (y1: number, y2: number, mu: number) => number;
|
|
127
|
+
declare const exponential: (y1: number, y2: number, mu: number) => number;
|
|
128
|
+
declare const cosine: (y1: number, y2: number, mu: number) => number;
|
|
129
|
+
declare const mod: (value: number, range: number) => number;
|
|
130
|
+
declare const fract: (value: number) => number;
|
|
131
|
+
declare const nextPowOf2: (n: int) => int;
|
|
132
|
+
declare const radToDeg: (rad: number) => number;
|
|
133
|
+
declare const degToRad: (deg: number) => number;
|
|
134
|
+
|
|
135
|
+
export { AABB, type AnyFunc, type AssertType, Axis, type Circle, type Client, CohenSutherland, Corner, EmptyExec, EmptyProcedure, type Exec, type Func, Geom, INVERSE_SQRT_2, type Maybe, type Nullable, type Optional, PI_HALF, PI_QUART, Padding, Point, type Procedure, Rect, type Size, TAU, Unhandled, ValueAxis, type ValueOrProvider, asDefined, assertType, clamp, clampUnit, cosine, degToRad, exponential, fract, getOrProvide, type int, isDefined, isNotUndefined, isUndefined, linear, mod, nextPowOf2, panic, quantizeCeil, quantizeFloor, quantizeRound, radToDeg, squashUnit, type unitValue };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
// src/math.ts
|
|
2
|
+
var TAU = Math.PI * 2;
|
|
3
|
+
var PI_HALF = Math.PI / 2;
|
|
4
|
+
var PI_QUART = Math.PI / 4;
|
|
5
|
+
var INVERSE_SQRT_2 = 1 / Math.sqrt(2);
|
|
6
|
+
var clamp = (value, min, max) => Math.max(min, Math.min(value, max));
|
|
7
|
+
var clampUnit = (value) => Math.max(0, Math.min(value, 1));
|
|
8
|
+
var squashUnit = (value, margin) => margin + (1 - 2 * margin) * Math.max(0, Math.min(value, 1));
|
|
9
|
+
var quantizeFloor = (value, interval) => Math.floor(value / interval) * interval;
|
|
10
|
+
var quantizeCeil = (value, interval) => Math.ceil(value / interval) * interval;
|
|
11
|
+
var quantizeRound = (value, interval) => Math.round(value / interval) * interval;
|
|
12
|
+
var linear = (y1, y2, mu) => y1 + (y2 - y1) * mu;
|
|
13
|
+
var exponential = (y1, y2, mu) => y1 * (y2 / y1) ** mu;
|
|
14
|
+
var cosine = (y1, y2, mu) => {
|
|
15
|
+
const mu2 = (1 - Math.cos(mu * Math.PI)) * 0.5;
|
|
16
|
+
return y1 * (1 - mu2) + y2 * mu2;
|
|
17
|
+
};
|
|
18
|
+
var mod = (value, range) => fract(value / range) * range;
|
|
19
|
+
var fract = (value) => value - Math.floor(value);
|
|
20
|
+
var nextPowOf2 = (n) => 2 ** Math.ceil(Math.log(n) / Math.log(2));
|
|
21
|
+
var radToDeg = (rad) => rad * 180 / Math.PI;
|
|
22
|
+
var degToRad = (deg) => deg / 180 * Math.PI;
|
|
23
|
+
|
|
24
|
+
// src/std.ts
|
|
25
|
+
var isDefined = (value) => value !== void 0 && value !== null;
|
|
26
|
+
var isUndefined = (value) => value === void 0;
|
|
27
|
+
var isNotUndefined = (value) => value !== void 0;
|
|
28
|
+
var asDefined = (value, fail = "asDefined failed") => value === null || value === void 0 ? panic(getOrProvide(fail)) : value;
|
|
29
|
+
var Unhandled = (empty) => {
|
|
30
|
+
throw new Error(`Unhandled ${empty}`);
|
|
31
|
+
};
|
|
32
|
+
var panic = (issue) => {
|
|
33
|
+
throw typeof issue === "string" ? new Error(issue) : issue;
|
|
34
|
+
};
|
|
35
|
+
var getOrProvide = (value) => value instanceof Function ? value() : value;
|
|
36
|
+
var EmptyExec = () => {
|
|
37
|
+
};
|
|
38
|
+
var EmptyProcedure = (_) => {
|
|
39
|
+
};
|
|
40
|
+
function assertType(_value) {
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// src/geom.ts
|
|
44
|
+
var Axis = /* @__PURE__ */ ((Axis2) => {
|
|
45
|
+
Axis2[Axis2["T"] = 0] = "T";
|
|
46
|
+
Axis2[Axis2["R"] = 1] = "R";
|
|
47
|
+
Axis2[Axis2["B"] = 2] = "B";
|
|
48
|
+
Axis2[Axis2["L"] = 3] = "L";
|
|
49
|
+
return Axis2;
|
|
50
|
+
})(Axis || {});
|
|
51
|
+
var Corner = /* @__PURE__ */ ((Corner2) => {
|
|
52
|
+
Corner2[Corner2["TL"] = 0] = "TL";
|
|
53
|
+
Corner2[Corner2["TR"] = 1] = "TR";
|
|
54
|
+
Corner2[Corner2["BR"] = 2] = "BR";
|
|
55
|
+
Corner2[Corner2["BL"] = 3] = "BL";
|
|
56
|
+
return Corner2;
|
|
57
|
+
})(Corner || {});
|
|
58
|
+
var Geom;
|
|
59
|
+
((Geom2) => {
|
|
60
|
+
Geom2.outerTangentPoints = (a, b) => {
|
|
61
|
+
const dx = b.x - a.x;
|
|
62
|
+
const dy = b.y - a.y;
|
|
63
|
+
const angle = Math.atan2(dy, dx) + Math.acos((a.r - b.r) / Math.sqrt(dx * dx + dy * dy));
|
|
64
|
+
const cs = Math.cos(angle);
|
|
65
|
+
const sn = Math.sin(angle);
|
|
66
|
+
return [
|
|
67
|
+
{ x: a.x + a.r * cs, y: a.y + a.r * sn },
|
|
68
|
+
{ x: b.x + b.r * cs, y: b.y + b.r * sn }
|
|
69
|
+
];
|
|
70
|
+
};
|
|
71
|
+
})(Geom || (Geom = {}));
|
|
72
|
+
var Point;
|
|
73
|
+
((Point2) => {
|
|
74
|
+
Point2.zero = () => ({ x: 0, y: 0 });
|
|
75
|
+
Point2.create = (x, y) => ({ x, y });
|
|
76
|
+
Point2.clone = (point) => ({ ...point });
|
|
77
|
+
Point2.floor = (point) => ({
|
|
78
|
+
x: Math.floor(point.x),
|
|
79
|
+
y: Math.floor(point.y)
|
|
80
|
+
});
|
|
81
|
+
Point2.length = (point) => Math.sqrt(point.x * point.x + point.y * point.y);
|
|
82
|
+
Point2.distance = (a, b) => Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2);
|
|
83
|
+
Point2.add = (a, b) => ({
|
|
84
|
+
x: a.x + b.x,
|
|
85
|
+
y: a.y + b.y
|
|
86
|
+
});
|
|
87
|
+
Point2.subtract = (a, b) => ({
|
|
88
|
+
x: a.x - b.x,
|
|
89
|
+
y: a.y - b.y
|
|
90
|
+
});
|
|
91
|
+
Point2.scaleBy = (point, scale) => ({
|
|
92
|
+
x: point.x * scale,
|
|
93
|
+
y: point.y * scale
|
|
94
|
+
});
|
|
95
|
+
Point2.scaleTo = (point, scale) => {
|
|
96
|
+
const multiplier = scale / (0, Point2.length)(point);
|
|
97
|
+
return { x: point.x * multiplier, y: point.y * multiplier };
|
|
98
|
+
};
|
|
99
|
+
Point2.fromClient = (object) => ({
|
|
100
|
+
x: object.clientX,
|
|
101
|
+
y: object.clientY
|
|
102
|
+
});
|
|
103
|
+
})(Point || (Point = {}));
|
|
104
|
+
var Rect;
|
|
105
|
+
((Rect2) => {
|
|
106
|
+
Rect2.Empty = Object.freeze({
|
|
107
|
+
x: 0,
|
|
108
|
+
y: 0,
|
|
109
|
+
width: 0,
|
|
110
|
+
height: 0
|
|
111
|
+
});
|
|
112
|
+
Rect2.corners = (rectangle) => {
|
|
113
|
+
const x0 = rectangle.x;
|
|
114
|
+
const y0 = rectangle.y;
|
|
115
|
+
const x1 = x0 + rectangle.width;
|
|
116
|
+
const y1 = y0 + rectangle.height;
|
|
117
|
+
return [
|
|
118
|
+
{ x: x0, y: y0 },
|
|
119
|
+
{ x: x1, y: y0 },
|
|
120
|
+
{ x: x1, y: y1 },
|
|
121
|
+
{ x: x0, y: y1 }
|
|
122
|
+
];
|
|
123
|
+
};
|
|
124
|
+
Rect2.inflate = (rect, amount) => ({
|
|
125
|
+
x: rect.x - amount,
|
|
126
|
+
y: rect.y - amount,
|
|
127
|
+
width: rect.width + amount * 2,
|
|
128
|
+
height: rect.height + amount * 2
|
|
129
|
+
});
|
|
130
|
+
Rect2.contains = (outer, inner) => {
|
|
131
|
+
const topLeftInside = inner.x >= outer.x && inner.y >= outer.y;
|
|
132
|
+
const bottomRightInside = inner.x + inner.width <= outer.x + outer.width && inner.y + inner.height <= outer.y + outer.height;
|
|
133
|
+
return topLeftInside && bottomRightInside;
|
|
134
|
+
};
|
|
135
|
+
Rect2.isPointInside = (point, rect) => point.x >= rect.x && point.x <= rect.x + rect.width && point.y >= rect.y && point.y <= rect.y + rect.height;
|
|
136
|
+
Rect2.intersect = (a, b) => {
|
|
137
|
+
const xMin = Math.max(a.x, b.x);
|
|
138
|
+
const xMax = Math.min(a.x + a.width, b.x + b.width);
|
|
139
|
+
const yMax = Math.min(a.y + a.height, b.y + b.height);
|
|
140
|
+
const yMin = Math.max(a.y, b.y);
|
|
141
|
+
return xMax > xMin && yMax > yMin;
|
|
142
|
+
};
|
|
143
|
+
Rect2.axis = (rectangle, axisValue) => {
|
|
144
|
+
switch (axisValue) {
|
|
145
|
+
case 0 /* T */:
|
|
146
|
+
return rectangle.y;
|
|
147
|
+
case 1 /* R */:
|
|
148
|
+
return rectangle.x + rectangle.width;
|
|
149
|
+
case 2 /* B */:
|
|
150
|
+
return rectangle.y + rectangle.height;
|
|
151
|
+
case 3 /* L */:
|
|
152
|
+
return rectangle.x;
|
|
153
|
+
default:
|
|
154
|
+
return Unhandled(axisValue);
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
Rect2.corner = (rectangle, cornerValue) => {
|
|
158
|
+
switch (cornerValue) {
|
|
159
|
+
case 0 /* TL */:
|
|
160
|
+
return { x: rectangle.x, y: rectangle.y };
|
|
161
|
+
case 1 /* TR */:
|
|
162
|
+
return { x: rectangle.x + rectangle.width, y: rectangle.y };
|
|
163
|
+
case 2 /* BR */:
|
|
164
|
+
return {
|
|
165
|
+
x: rectangle.x + rectangle.width,
|
|
166
|
+
y: rectangle.y + rectangle.height
|
|
167
|
+
};
|
|
168
|
+
case 3 /* BL */:
|
|
169
|
+
return { x: rectangle.x, y: rectangle.y + rectangle.height };
|
|
170
|
+
default:
|
|
171
|
+
return Unhandled(cornerValue);
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
Rect2.center = (rectangle) => ({
|
|
175
|
+
x: rectangle.x + rectangle.width * 0.5,
|
|
176
|
+
y: rectangle.y + rectangle.height * 0.5
|
|
177
|
+
});
|
|
178
|
+
Rect2.isEmpty = (rectangle) => rectangle.width === 0 || rectangle.height === 0;
|
|
179
|
+
Rect2.union = (a, b) => {
|
|
180
|
+
if (Rect2.isEmpty(a)) {
|
|
181
|
+
if (!Rect2.isEmpty(b)) {
|
|
182
|
+
a.x = b.x;
|
|
183
|
+
a.y = b.y;
|
|
184
|
+
a.width = b.width;
|
|
185
|
+
a.height = b.height;
|
|
186
|
+
}
|
|
187
|
+
} else if (!Rect2.isEmpty(b)) {
|
|
188
|
+
const bx = b.x;
|
|
189
|
+
const by = b.y;
|
|
190
|
+
const ux = Math.min(a.x, bx);
|
|
191
|
+
const uy = Math.min(a.y, by);
|
|
192
|
+
a.width = Math.max(a.x + a.width, bx + b.width) - ux;
|
|
193
|
+
a.height = Math.max(a.y + a.height, by + b.height) - uy;
|
|
194
|
+
a.x = ux;
|
|
195
|
+
a.y = uy;
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
})(Rect || (Rect = {}));
|
|
199
|
+
var AABB;
|
|
200
|
+
((AABB2) => {
|
|
201
|
+
AABB2.width = (aabb) => aabb.xMax - aabb.xMin;
|
|
202
|
+
AABB2.height = (aabb) => aabb.yMax - aabb.yMin;
|
|
203
|
+
AABB2.from = (aabb, that) => {
|
|
204
|
+
aabb.xMin = that.xMin;
|
|
205
|
+
aabb.xMax = that.xMax;
|
|
206
|
+
aabb.yMin = that.yMin;
|
|
207
|
+
aabb.yMax = that.yMax;
|
|
208
|
+
};
|
|
209
|
+
AABB2.extend = (aabb, offset) => {
|
|
210
|
+
aabb.xMin -= offset;
|
|
211
|
+
aabb.yMin -= offset;
|
|
212
|
+
aabb.xMax += offset;
|
|
213
|
+
aabb.yMax += offset;
|
|
214
|
+
};
|
|
215
|
+
AABB2.padding = (aabb, [top, right, bottom, left]) => {
|
|
216
|
+
aabb.xMin += left;
|
|
217
|
+
aabb.yMin += top;
|
|
218
|
+
aabb.xMax -= right;
|
|
219
|
+
aabb.yMax -= bottom;
|
|
220
|
+
return aabb;
|
|
221
|
+
};
|
|
222
|
+
AABB2.intersectPoint = (aabb, point) => aabb.xMin <= point.x && point.x < aabb.xMax && aabb.yMin <= point.y && point.y < aabb.yMax;
|
|
223
|
+
AABB2.intersectThat = (aabb, that) => that.xMin < aabb.xMax && that.xMax > aabb.xMin && that.yMin < aabb.yMax && that.yMax > aabb.yMin;
|
|
224
|
+
AABB2.center = (aabb) => ({
|
|
225
|
+
x: (aabb.xMin + aabb.xMax) * 0.5,
|
|
226
|
+
y: (aabb.yMin + aabb.yMax) * 0.5
|
|
227
|
+
});
|
|
228
|
+
})(AABB || (AABB = {}));
|
|
229
|
+
var Padding;
|
|
230
|
+
((Padding2) => {
|
|
231
|
+
Padding2.Identity = Object.freeze([
|
|
232
|
+
0,
|
|
233
|
+
0,
|
|
234
|
+
0,
|
|
235
|
+
0
|
|
236
|
+
]);
|
|
237
|
+
})(Padding || (Padding = {}));
|
|
238
|
+
var CohenSutherland;
|
|
239
|
+
((CohenSutherland2) => {
|
|
240
|
+
CohenSutherland2.intersects = (xMin, xMax, yMin, yMax, x0, y0, x1, y1) => {
|
|
241
|
+
const c0 = code(xMin, xMax, yMin, yMax, x0, y0);
|
|
242
|
+
const c1 = code(xMin, xMax, yMin, yMax, x1, y1);
|
|
243
|
+
if ((c0 | c1) === 0) {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if ((c0 & c1) !== 0) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
const s = sign(x0, y0, x1, y1, xMin, yMin);
|
|
250
|
+
return s !== sign(x0, y0, x1, y1, xMax, yMin) || s !== sign(x0, y0, x1, y1, xMax, yMax) || s !== sign(x0, y0, x1, y1, xMin, yMax);
|
|
251
|
+
};
|
|
252
|
+
const code = (xMin, xMax, yMin, yMax, x, y) => {
|
|
253
|
+
let codeValue = 0;
|
|
254
|
+
if (x <= xMin) {
|
|
255
|
+
codeValue |= 1;
|
|
256
|
+
} else if (x >= xMax) {
|
|
257
|
+
codeValue |= 2;
|
|
258
|
+
}
|
|
259
|
+
if (y <= yMin) {
|
|
260
|
+
codeValue |= 8;
|
|
261
|
+
} else if (y >= yMax) {
|
|
262
|
+
codeValue |= 4;
|
|
263
|
+
}
|
|
264
|
+
return codeValue;
|
|
265
|
+
};
|
|
266
|
+
const sign = (x0, y0, x1, y1, x2, y2) => (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) >= 0;
|
|
267
|
+
})(CohenSutherland || (CohenSutherland = {}));
|
|
268
|
+
var ValueAxis;
|
|
269
|
+
((ValueAxis2) => {
|
|
270
|
+
ValueAxis2.Identity = {
|
|
271
|
+
valueToAxis: (value) => value,
|
|
272
|
+
axisToValue: (axis) => axis
|
|
273
|
+
};
|
|
274
|
+
ValueAxis2.toClamped = (valueAxis, min, max) => ({
|
|
275
|
+
valueToAxis: (value) => valueAxis.valueToAxis(clamp(value, min, max)),
|
|
276
|
+
axisToValue: (axis) => clamp(valueAxis.axisToValue(axis), min, max)
|
|
277
|
+
});
|
|
278
|
+
ValueAxis2.createClamped = (min, max) => ({
|
|
279
|
+
valueToAxis: (value) => clamp(value, min, max),
|
|
280
|
+
axisToValue: (axis) => clamp(axis, min, max)
|
|
281
|
+
});
|
|
282
|
+
})(ValueAxis || (ValueAxis = {}));
|
|
283
|
+
export {
|
|
284
|
+
AABB,
|
|
285
|
+
Axis,
|
|
286
|
+
CohenSutherland,
|
|
287
|
+
Corner,
|
|
288
|
+
EmptyExec,
|
|
289
|
+
EmptyProcedure,
|
|
290
|
+
Geom,
|
|
291
|
+
INVERSE_SQRT_2,
|
|
292
|
+
PI_HALF,
|
|
293
|
+
PI_QUART,
|
|
294
|
+
Padding,
|
|
295
|
+
Point,
|
|
296
|
+
Rect,
|
|
297
|
+
TAU,
|
|
298
|
+
Unhandled,
|
|
299
|
+
ValueAxis,
|
|
300
|
+
asDefined,
|
|
301
|
+
assertType,
|
|
302
|
+
clamp,
|
|
303
|
+
clampUnit,
|
|
304
|
+
cosine,
|
|
305
|
+
degToRad,
|
|
306
|
+
exponential,
|
|
307
|
+
fract,
|
|
308
|
+
getOrProvide,
|
|
309
|
+
isDefined,
|
|
310
|
+
isNotUndefined,
|
|
311
|
+
isUndefined,
|
|
312
|
+
linear,
|
|
313
|
+
mod,
|
|
314
|
+
nextPowOf2,
|
|
315
|
+
panic,
|
|
316
|
+
quantizeCeil,
|
|
317
|
+
quantizeFloor,
|
|
318
|
+
quantizeRound,
|
|
319
|
+
radToDeg,
|
|
320
|
+
squashUnit
|
|
321
|
+
};
|
|
322
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/math.ts","../src/std.ts","../src/geom.ts"],"sourcesContent":["import type { int, unitValue } from \"./std\";\n\nexport const TAU = Math.PI * 2.0;\nexport const PI_HALF = Math.PI / 2.0;\nexport const PI_QUART = Math.PI / 4.0;\nexport const INVERSE_SQRT_2 = 1.0 / Math.sqrt(2.0);\n\nexport const clamp = (value: number, min: number, max: number): number =>\n Math.max(min, Math.min(value, max));\nexport const clampUnit = (value: number): unitValue =>\n Math.max(0.0, Math.min(value, 1.0));\nexport const squashUnit = (value: unitValue, margin: unitValue): unitValue =>\n margin + (1.0 - 2.0 * margin) * Math.max(0.0, Math.min(value, 1.0));\nexport const quantizeFloor = (value: number, interval: number): number =>\n Math.floor(value / interval) * interval;\nexport const quantizeCeil = (value: number, interval: number): number =>\n Math.ceil(value / interval) * interval;\nexport const quantizeRound = (value: number, interval: number): number =>\n Math.round(value / interval) * interval;\nexport const linear = (y1: number, y2: number, mu: number): number =>\n y1 + (y2 - y1) * mu;\nexport const exponential = (y1: number, y2: number, mu: number): number =>\n y1 * (y2 / y1) ** mu;\nexport const cosine = (y1: number, y2: number, mu: number): number => {\n const mu2 = (1.0 - Math.cos(mu * Math.PI)) * 0.5;\n return y1 * (1.0 - mu2) + y2 * mu2;\n};\nexport const mod = (value: number, range: number): number =>\n fract(value / range) * range;\nexport const fract = (value: number): number => value - Math.floor(value);\nexport const nextPowOf2 = (n: int): int =>\n 2 ** Math.ceil(Math.log(n) / Math.log(2));\nexport const radToDeg = (rad: number): number => (rad * 180.0) / Math.PI;\nexport const degToRad = (deg: number): number => (deg / 180.0) * Math.PI;\n","export type int = number;\nexport type unitValue = number; // 0...1\nexport type Optional<T> = T | undefined;\nexport type Nullable<T> = T | null;\nexport type Maybe<T> = T | undefined | null;\nexport type ValueOrProvider<T> = T | (() => T);\nexport type Procedure<T> = (value: T) => void;\nexport type Func<U, T> = (value: U) => T;\nexport type AnyFunc = (...args: any[]) => any;\nexport type AssertType<T> = (value: unknown) => value is T;\nexport type Exec = () => void;\n\nexport const isDefined = <T>(value: Maybe<T>): value is T =>\n value !== undefined && value !== null;\n\nexport const isUndefined = (value: unknown): value is undefined =>\n value === undefined;\n\nexport const isNotUndefined = <T>(value: Optional<T>): value is T =>\n value !== undefined;\n\nexport const asDefined = <T>(\n value: Maybe<T>,\n fail: ValueOrProvider<string> = \"asDefined failed\"\n): T =>\n value === null || value === undefined ? panic(getOrProvide(fail)) : value;\n\nexport const Unhandled = <R>(empty: never): R => {\n throw new Error(`Unhandled ${empty}`);\n};\n\nexport const panic = (issue?: string | Error | unknown): never => {\n throw typeof issue === \"string\" ? new Error(issue) : issue;\n};\n\nexport const getOrProvide = <T>(value: ValueOrProvider<T>): T =>\n value instanceof Function ? value() : value;\n\nexport const EmptyExec: Exec = (): void => {\n // no-op\n};\n\nexport const EmptyProcedure: Procedure<any> = (_: any): void => {\n // no-op\n};\n\nexport function assertType<T>(_value: unknown): asserts _value is T {\n // no-op\n}\n","import { clamp } from \"./math\";\nimport { type int, Unhandled } from \"./std\";\n\nexport type Point = { x: number; y: number };\nexport type Circle = Point & { r: number };\nexport type Size = { width: number; height: number };\nexport type Rect = Point & Size;\nexport type Padding = [number, number, number, number];\nexport type Client = { clientX: number; clientY: number };\n\nexport enum Axis {\n T = 0,\n R = 1,\n B = 2,\n L = 3,\n}\n\nexport enum Corner {\n TL = 0,\n TR = 1,\n BR = 2,\n BL = 3,\n}\n\nexport namespace Geom {\n export const outerTangentPoints = (a: Circle, b: Circle): [Point, Point] => {\n const dx = b.x - a.x;\n const dy = b.y - a.y;\n const angle =\n Math.atan2(dy, dx) +\n Math.acos((a.r - b.r) / Math.sqrt(dx * dx + dy * dy));\n const cs = Math.cos(angle);\n const sn = Math.sin(angle);\n return [\n { x: a.x + a.r * cs, y: a.y + a.r * sn },\n { x: b.x + b.r * cs, y: b.y + b.r * sn },\n ];\n };\n}\n\nexport namespace Point {\n export const zero = (): Point => ({ x: 0, y: 0 });\n export const create = (x: number, y: number): Point => ({ x, y });\n export const clone = (point: Point): Point => ({ ...point });\n export const floor = (point: Point): Point => ({\n x: Math.floor(point.x),\n y: Math.floor(point.y),\n });\n export const length = (point: Point): number =>\n Math.sqrt(point.x * point.x + point.y * point.y);\n export const distance = (a: Point, b: Point): number =>\n Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2);\n export const add = (a: Point, b: Point): Point => ({\n x: a.x + b.x,\n y: a.y + b.y,\n });\n export const subtract = (a: Point, b: Point): Point => ({\n x: a.x - b.x,\n y: a.y - b.y,\n });\n export const scaleBy = (point: Point, scale: number): Point => ({\n x: point.x * scale,\n y: point.y * scale,\n });\n export const scaleTo = (point: Point, scale: number): Point => {\n const multiplier = scale / length(point);\n return { x: point.x * multiplier, y: point.y * multiplier };\n };\n export const fromClient = (object: {\n clientX: number;\n clientY: number;\n }): Point => ({\n x: object.clientX,\n y: object.clientY,\n });\n}\n\nexport namespace Rect {\n export const Empty: Readonly<Rect> = Object.freeze({\n x: 0,\n y: 0,\n width: 0,\n height: 0,\n });\n\n export const corners = (rectangle: Rect): Array<Point> => {\n const x0 = rectangle.x;\n const y0 = rectangle.y;\n const x1 = x0 + rectangle.width;\n const y1 = y0 + rectangle.height;\n return [\n { x: x0, y: y0 },\n { x: x1, y: y0 },\n { x: x1, y: y1 },\n { x: x0, y: y1 },\n ];\n };\n\n export const inflate = (rect: Rect, amount: number): Rect => ({\n x: rect.x - amount,\n y: rect.y - amount,\n width: rect.width + amount * 2.0,\n height: rect.height + amount * 2.0,\n });\n\n export const contains = (outer: Rect, inner: Rect): boolean => {\n const topLeftInside = inner.x >= outer.x && inner.y >= outer.y;\n const bottomRightInside =\n inner.x + inner.width <= outer.x + outer.width &&\n inner.y + inner.height <= outer.y + outer.height;\n return topLeftInside && bottomRightInside;\n };\n\n export const isPointInside = (point: Point, rect: Rect): boolean =>\n point.x >= rect.x &&\n point.x <= rect.x + rect.width &&\n point.y >= rect.y &&\n point.y <= rect.y + rect.height;\n\n export const intersect = (a: Rect, b: Rect): boolean => {\n const xMin = Math.max(a.x, b.x);\n const xMax = Math.min(a.x + a.width, b.x + b.width);\n const yMax = Math.min(a.y + a.height, b.y + b.height);\n const yMin = Math.max(a.y, b.y);\n return xMax > xMin && yMax > yMin;\n };\n\n export const axis = (rectangle: Rect, axisValue: Axis): number => {\n switch (axisValue) {\n case Axis.T:\n return rectangle.y;\n case Axis.R:\n return rectangle.x + rectangle.width;\n case Axis.B:\n return rectangle.y + rectangle.height;\n case Axis.L:\n return rectangle.x;\n default:\n return Unhandled(axisValue);\n }\n };\n\n export const corner = (rectangle: Rect, cornerValue: Corner): Point => {\n switch (cornerValue) {\n case Corner.TL:\n return { x: rectangle.x, y: rectangle.y };\n case Corner.TR:\n return { x: rectangle.x + rectangle.width, y: rectangle.y };\n case Corner.BR:\n return {\n x: rectangle.x + rectangle.width,\n y: rectangle.y + rectangle.height,\n };\n case Corner.BL:\n return { x: rectangle.x, y: rectangle.y + rectangle.height };\n default:\n return Unhandled(cornerValue);\n }\n };\n\n export const center = (rectangle: Rect): Point => ({\n x: rectangle.x + rectangle.width * 0.5,\n y: rectangle.y + rectangle.height * 0.5,\n });\n\n export const isEmpty = (rectangle: Rect): boolean =>\n rectangle.width === 0 || rectangle.height === 0;\n\n export const union = (a: Rect, b: Readonly<Rect>): void => {\n if (Rect.isEmpty(a)) {\n if (!Rect.isEmpty(b)) {\n a.x = b.x;\n a.y = b.y;\n a.width = b.width;\n a.height = b.height;\n }\n } else if (!Rect.isEmpty(b)) {\n const bx = b.x;\n const by = b.y;\n const ux = Math.min(a.x, bx);\n const uy = Math.min(a.y, by);\n a.width = Math.max(a.x + a.width, bx + b.width) - ux;\n a.height = Math.max(a.y + a.height, by + b.height) - uy;\n a.x = ux;\n a.y = uy;\n }\n };\n}\n\nexport interface AABB {\n xMin: number;\n xMax: number;\n yMin: number;\n yMax: number;\n}\n\nexport namespace AABB {\n export const width = (aabb: AABB): number => aabb.xMax - aabb.xMin;\n export const height = (aabb: AABB): number => aabb.yMax - aabb.yMin;\n\n export const from = (aabb: AABB, that: AABB): void => {\n aabb.xMin = that.xMin;\n aabb.xMax = that.xMax;\n aabb.yMin = that.yMin;\n aabb.yMax = that.yMax;\n };\n\n export const extend = (aabb: AABB, offset: number): void => {\n aabb.xMin -= offset;\n aabb.yMin -= offset;\n aabb.xMax += offset;\n aabb.yMax += offset;\n };\n\n export const padding = (\n aabb: AABB,\n [top, right, bottom, left]: Readonly<Padding>\n ): AABB => {\n aabb.xMin += left;\n aabb.yMin += top;\n aabb.xMax -= right;\n aabb.yMax -= bottom;\n return aabb;\n };\n\n export const intersectPoint = (aabb: AABB, point: Point): boolean =>\n aabb.xMin <= point.x &&\n point.x < aabb.xMax &&\n aabb.yMin <= point.y &&\n point.y < aabb.yMax;\n\n export const intersectThat = (aabb: AABB, that: AABB): boolean =>\n that.xMin < aabb.xMax &&\n that.xMax > aabb.xMin &&\n that.yMin < aabb.yMax &&\n that.yMax > aabb.yMin;\n\n export const center = (aabb: AABB): Point => ({\n x: (aabb.xMin + aabb.xMax) * 0.5,\n y: (aabb.yMin + aabb.yMax) * 0.5,\n });\n}\n\nexport namespace Padding {\n export const Identity: Readonly<Padding> = Object.freeze([\n 0.0, 0.0, 0.0, 0.0,\n ]);\n}\n\nexport namespace CohenSutherland {\n export const intersects = (\n xMin: number,\n xMax: number,\n yMin: number,\n yMax: number,\n x0: number,\n y0: number,\n x1: number,\n y1: number\n ): boolean => {\n const c0 = code(xMin, xMax, yMin, yMax, x0, y0);\n const c1 = code(xMin, xMax, yMin, yMax, x1, y1);\n if ((c0 | c1) === 0) {\n return false;\n }\n if ((c0 & c1) !== 0) {\n return false;\n }\n const s = sign(x0, y0, x1, y1, xMin, yMin);\n return (\n s !== sign(x0, y0, x1, y1, xMax, yMin) ||\n s !== sign(x0, y0, x1, y1, xMax, yMax) ||\n s !== sign(x0, y0, x1, y1, xMin, yMax)\n );\n };\n\n const code = (\n xMin: number,\n xMax: number,\n yMin: number,\n yMax: number,\n x: number,\n y: number\n ): int => {\n let codeValue = 0;\n if (x <= xMin) {\n codeValue |= 1;\n } else if (x >= xMax) {\n codeValue |= 2;\n }\n if (y <= yMin) {\n codeValue |= 8;\n } else if (y >= yMax) {\n codeValue |= 4;\n }\n return codeValue;\n };\n\n const sign = (\n x0: number,\n y0: number,\n x1: number,\n y1: number,\n x2: number,\n y2: number\n ): boolean => (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) >= 0;\n}\n\nexport interface ValueAxis {\n valueToAxis(value: number): number;\n axisToValue(axis: number): number;\n}\n\nexport namespace ValueAxis {\n export const Identity: ValueAxis = {\n valueToAxis: (value: number): number => value,\n axisToValue: (axis: number): number => axis,\n };\n\n export const toClamped = (\n valueAxis: ValueAxis,\n min: number,\n max: number\n ): ValueAxis => ({\n valueToAxis: (value: number): number =>\n valueAxis.valueToAxis(clamp(value, min, max)),\n axisToValue: (axis: number): number =>\n clamp(valueAxis.axisToValue(axis), min, max),\n });\n\n export const createClamped = (min: number, max: number): ValueAxis => ({\n valueToAxis: (value: number): number => clamp(value, min, max),\n axisToValue: (axis: number): number => clamp(axis, min, max),\n });\n}\n"],"mappings":";AAEO,IAAM,MAAM,KAAK,KAAK;AACtB,IAAM,UAAU,KAAK,KAAK;AAC1B,IAAM,WAAW,KAAK,KAAK;AAC3B,IAAM,iBAAiB,IAAM,KAAK,KAAK,CAAG;AAE1C,IAAM,QAAQ,CAAC,OAAe,KAAa,QAChD,KAAK,IAAI,KAAK,KAAK,IAAI,OAAO,GAAG,CAAC;AAC7B,IAAM,YAAY,CAAC,UACxB,KAAK,IAAI,GAAK,KAAK,IAAI,OAAO,CAAG,CAAC;AAC7B,IAAM,aAAa,CAAC,OAAkB,WAC3C,UAAU,IAAM,IAAM,UAAU,KAAK,IAAI,GAAK,KAAK,IAAI,OAAO,CAAG,CAAC;AAC7D,IAAM,gBAAgB,CAAC,OAAe,aAC3C,KAAK,MAAM,QAAQ,QAAQ,IAAI;AAC1B,IAAM,eAAe,CAAC,OAAe,aAC1C,KAAK,KAAK,QAAQ,QAAQ,IAAI;AACzB,IAAM,gBAAgB,CAAC,OAAe,aAC3C,KAAK,MAAM,QAAQ,QAAQ,IAAI;AAC1B,IAAM,SAAS,CAAC,IAAY,IAAY,OAC7C,MAAM,KAAK,MAAM;AACZ,IAAM,cAAc,CAAC,IAAY,IAAY,OAClD,MAAM,KAAK,OAAO;AACb,IAAM,SAAS,CAAC,IAAY,IAAY,OAAuB;AACpE,QAAM,OAAO,IAAM,KAAK,IAAI,KAAK,KAAK,EAAE,KAAK;AAC7C,SAAO,MAAM,IAAM,OAAO,KAAK;AACjC;AACO,IAAM,MAAM,CAAC,O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,WAAA,gBAAgB,CAAC,KAAa,SAA4B;AAAA,IACrE,aAAa,CAAC,UAA0B,MAAM,OAAO,KAAK,GAAG;AAAA,IAC7D,aAAa,CAAC,SAAyB,MAAM,MAAM,KAAK,GAAG;AAAA,EAC7D;AAAA,GApBe;","names":["Axis","Corner","Geom","Point","Rect","AABB","Padding","CohenSutherland","ValueAxis"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@audio-ui/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "A collection of utility functions for Audio UI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"module": "dist/index.js",
|
|
@@ -32,13 +32,19 @@
|
|
|
32
32
|
"import": "./dist/dom/index.js"
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
35
38
|
"scripts": {
|
|
36
39
|
"build:pkg": "tsup",
|
|
37
|
-
"dev": "tsup --watch"
|
|
40
|
+
"dev": "tsup --watch",
|
|
41
|
+
"lint": "biome check --write .",
|
|
42
|
+
"lint:fix": "biome check --write --unsafe .",
|
|
43
|
+
"clean": "rm -rf dist node_modules .turbo"
|
|
38
44
|
},
|
|
39
45
|
"devDependencies": {
|
|
40
|
-
"@audio-ui/typescript": "
|
|
41
|
-
"@types/bun": "
|
|
46
|
+
"@audio-ui/typescript": "latest",
|
|
47
|
+
"@types/bun": "^1.3.5",
|
|
42
48
|
"tsup": "^8.5.0"
|
|
43
49
|
}
|
|
44
50
|
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
[0m[2m[35m$[0m [2m[1mtsup[0m
|
|
3
|
-
[34mCLI[39m Building entry: {"index":"src/index.ts"}
|
|
4
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
5
|
-
[34mCLI[39m tsup v8.5.1
|
|
6
|
-
[34mCLI[39m Using tsup config: /Users/lucienloua/Desktop/audio-ui/packages/utils/tsup.config.ts
|
|
7
|
-
[34mCLI[39m Target: es2022
|
|
8
|
-
[34mCLI[39m Cleaning output folder
|
|
9
|
-
[34mESM[39m Build start
|
|
10
|
-
[32mESM[39m [1mdist/index.js [22m[32m9.31 KB[39m
|
|
11
|
-
[32mESM[39m [1mdist/index.js.map [22m[32m20.40 KB[39m
|
|
12
|
-
[32mESM[39m ⚡️ Build success in 18ms
|
|
13
|
-
DTS Build start
|
|
14
|
-
DTS ⚡️ Build success in 1684ms
|
|
15
|
-
DTS dist/index.d.ts 5.34 KB
|
package/CHANGELOG.md
DELETED
package/src/geom.ts
DELETED
|
@@ -1,335 +0,0 @@
|
|
|
1
|
-
import { clamp } from "./math";
|
|
2
|
-
import { type int, Unhandled } from "./std";
|
|
3
|
-
|
|
4
|
-
export type Point = { x: number; y: number };
|
|
5
|
-
export type Circle = Point & { r: number };
|
|
6
|
-
export type Size = { width: number; height: number };
|
|
7
|
-
export type Rect = Point & Size;
|
|
8
|
-
export type Padding = [number, number, number, number];
|
|
9
|
-
export type Client = { clientX: number; clientY: number };
|
|
10
|
-
|
|
11
|
-
export enum Axis {
|
|
12
|
-
T = 0,
|
|
13
|
-
R = 1,
|
|
14
|
-
B = 2,
|
|
15
|
-
L = 3,
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export enum Corner {
|
|
19
|
-
TL = 0,
|
|
20
|
-
TR = 1,
|
|
21
|
-
BR = 2,
|
|
22
|
-
BL = 3,
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export namespace Geom {
|
|
26
|
-
export const outerTangentPoints = (a: Circle, b: Circle): [Point, Point] => {
|
|
27
|
-
const dx = b.x - a.x;
|
|
28
|
-
const dy = b.y - a.y;
|
|
29
|
-
const angle =
|
|
30
|
-
Math.atan2(dy, dx) +
|
|
31
|
-
Math.acos((a.r - b.r) / Math.sqrt(dx * dx + dy * dy));
|
|
32
|
-
const cs = Math.cos(angle);
|
|
33
|
-
const sn = Math.sin(angle);
|
|
34
|
-
return [
|
|
35
|
-
{ x: a.x + a.r * cs, y: a.y + a.r * sn },
|
|
36
|
-
{ x: b.x + b.r * cs, y: b.y + b.r * sn },
|
|
37
|
-
];
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export namespace Point {
|
|
42
|
-
export const zero = (): Point => ({ x: 0, y: 0 });
|
|
43
|
-
export const create = (x: number, y: number): Point => ({ x, y });
|
|
44
|
-
export const clone = (point: Point): Point => ({ ...point });
|
|
45
|
-
export const floor = (point: Point): Point => ({
|
|
46
|
-
x: Math.floor(point.x),
|
|
47
|
-
y: Math.floor(point.y),
|
|
48
|
-
});
|
|
49
|
-
export const length = (point: Point): number =>
|
|
50
|
-
Math.sqrt(point.x * point.x + point.y * point.y);
|
|
51
|
-
export const distance = (a: Point, b: Point): number =>
|
|
52
|
-
Math.sqrt((b.x - a.x) ** 2 + (b.y - a.y) ** 2);
|
|
53
|
-
export const add = (a: Point, b: Point): Point => ({
|
|
54
|
-
x: a.x + b.x,
|
|
55
|
-
y: a.y + b.y,
|
|
56
|
-
});
|
|
57
|
-
export const subtract = (a: Point, b: Point): Point => ({
|
|
58
|
-
x: a.x - b.x,
|
|
59
|
-
y: a.y - b.y,
|
|
60
|
-
});
|
|
61
|
-
export const scaleBy = (point: Point, scale: number): Point => ({
|
|
62
|
-
x: point.x * scale,
|
|
63
|
-
y: point.y * scale,
|
|
64
|
-
});
|
|
65
|
-
export const scaleTo = (point: Point, scale: number): Point => {
|
|
66
|
-
const multiplier = scale / length(point);
|
|
67
|
-
return { x: point.x * multiplier, y: point.y * multiplier };
|
|
68
|
-
};
|
|
69
|
-
export const fromClient = (object: {
|
|
70
|
-
clientX: number;
|
|
71
|
-
clientY: number;
|
|
72
|
-
}): Point => ({
|
|
73
|
-
x: object.clientX,
|
|
74
|
-
y: object.clientY,
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export namespace Rect {
|
|
79
|
-
export const Empty: Readonly<Rect> = Object.freeze({
|
|
80
|
-
x: 0,
|
|
81
|
-
y: 0,
|
|
82
|
-
width: 0,
|
|
83
|
-
height: 0,
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
export const corners = (rectangle: Rect): Array<Point> => {
|
|
87
|
-
const x0 = rectangle.x;
|
|
88
|
-
const y0 = rectangle.y;
|
|
89
|
-
const x1 = x0 + rectangle.width;
|
|
90
|
-
const y1 = y0 + rectangle.height;
|
|
91
|
-
return [
|
|
92
|
-
{ x: x0, y: y0 },
|
|
93
|
-
{ x: x1, y: y0 },
|
|
94
|
-
{ x: x1, y: y1 },
|
|
95
|
-
{ x: x0, y: y1 },
|
|
96
|
-
];
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
export const inflate = (rect: Rect, amount: number): Rect => ({
|
|
100
|
-
x: rect.x - amount,
|
|
101
|
-
y: rect.y - amount,
|
|
102
|
-
width: rect.width + amount * 2.0,
|
|
103
|
-
height: rect.height + amount * 2.0,
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
export const contains = (outer: Rect, inner: Rect): boolean => {
|
|
107
|
-
const topLeftInside = inner.x >= outer.x && inner.y >= outer.y;
|
|
108
|
-
const bottomRightInside =
|
|
109
|
-
inner.x + inner.width <= outer.x + outer.width &&
|
|
110
|
-
inner.y + inner.height <= outer.y + outer.height;
|
|
111
|
-
return topLeftInside && bottomRightInside;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
export const isPointInside = (point: Point, rect: Rect): boolean =>
|
|
115
|
-
point.x >= rect.x &&
|
|
116
|
-
point.x <= rect.x + rect.width &&
|
|
117
|
-
point.y >= rect.y &&
|
|
118
|
-
point.y <= rect.y + rect.height;
|
|
119
|
-
|
|
120
|
-
export const intersect = (a: Rect, b: Rect): boolean => {
|
|
121
|
-
const xMin = Math.max(a.x, b.x);
|
|
122
|
-
const xMax = Math.min(a.x + a.width, b.x + b.width);
|
|
123
|
-
const yMax = Math.min(a.y + a.height, b.y + b.height);
|
|
124
|
-
const yMin = Math.max(a.y, b.y);
|
|
125
|
-
return xMax > xMin && yMax > yMin;
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
export const axis = (rectangle: Rect, axisValue: Axis): number => {
|
|
129
|
-
switch (axisValue) {
|
|
130
|
-
case Axis.T:
|
|
131
|
-
return rectangle.y;
|
|
132
|
-
case Axis.R:
|
|
133
|
-
return rectangle.x + rectangle.width;
|
|
134
|
-
case Axis.B:
|
|
135
|
-
return rectangle.y + rectangle.height;
|
|
136
|
-
case Axis.L:
|
|
137
|
-
return rectangle.x;
|
|
138
|
-
default:
|
|
139
|
-
return Unhandled(axisValue);
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
export const corner = (rectangle: Rect, cornerValue: Corner): Point => {
|
|
144
|
-
switch (cornerValue) {
|
|
145
|
-
case Corner.TL:
|
|
146
|
-
return { x: rectangle.x, y: rectangle.y };
|
|
147
|
-
case Corner.TR:
|
|
148
|
-
return { x: rectangle.x + rectangle.width, y: rectangle.y };
|
|
149
|
-
case Corner.BR:
|
|
150
|
-
return {
|
|
151
|
-
x: rectangle.x + rectangle.width,
|
|
152
|
-
y: rectangle.y + rectangle.height,
|
|
153
|
-
};
|
|
154
|
-
case Corner.BL:
|
|
155
|
-
return { x: rectangle.x, y: rectangle.y + rectangle.height };
|
|
156
|
-
default:
|
|
157
|
-
return Unhandled(cornerValue);
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
export const center = (rectangle: Rect): Point => ({
|
|
162
|
-
x: rectangle.x + rectangle.width * 0.5,
|
|
163
|
-
y: rectangle.y + rectangle.height * 0.5,
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
export const isEmpty = (rectangle: Rect): boolean =>
|
|
167
|
-
rectangle.width === 0 || rectangle.height === 0;
|
|
168
|
-
|
|
169
|
-
export const union = (a: Rect, b: Readonly<Rect>): void => {
|
|
170
|
-
if (Rect.isEmpty(a)) {
|
|
171
|
-
if (!Rect.isEmpty(b)) {
|
|
172
|
-
a.x = b.x;
|
|
173
|
-
a.y = b.y;
|
|
174
|
-
a.width = b.width;
|
|
175
|
-
a.height = b.height;
|
|
176
|
-
}
|
|
177
|
-
} else if (!Rect.isEmpty(b)) {
|
|
178
|
-
const bx = b.x;
|
|
179
|
-
const by = b.y;
|
|
180
|
-
const ux = Math.min(a.x, bx);
|
|
181
|
-
const uy = Math.min(a.y, by);
|
|
182
|
-
a.width = Math.max(a.x + a.width, bx + b.width) - ux;
|
|
183
|
-
a.height = Math.max(a.y + a.height, by + b.height) - uy;
|
|
184
|
-
a.x = ux;
|
|
185
|
-
a.y = uy;
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export interface AABB {
|
|
191
|
-
xMin: number;
|
|
192
|
-
xMax: number;
|
|
193
|
-
yMin: number;
|
|
194
|
-
yMax: number;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export namespace AABB {
|
|
198
|
-
export const width = (aabb: AABB): number => aabb.xMax - aabb.xMin;
|
|
199
|
-
export const height = (aabb: AABB): number => aabb.yMax - aabb.yMin;
|
|
200
|
-
|
|
201
|
-
export const from = (aabb: AABB, that: AABB): void => {
|
|
202
|
-
aabb.xMin = that.xMin;
|
|
203
|
-
aabb.xMax = that.xMax;
|
|
204
|
-
aabb.yMin = that.yMin;
|
|
205
|
-
aabb.yMax = that.yMax;
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
export const extend = (aabb: AABB, offset: number): void => {
|
|
209
|
-
aabb.xMin -= offset;
|
|
210
|
-
aabb.yMin -= offset;
|
|
211
|
-
aabb.xMax += offset;
|
|
212
|
-
aabb.yMax += offset;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
export const padding = (
|
|
216
|
-
aabb: AABB,
|
|
217
|
-
[top, right, bottom, left]: Readonly<Padding>
|
|
218
|
-
): AABB => {
|
|
219
|
-
aabb.xMin += left;
|
|
220
|
-
aabb.yMin += top;
|
|
221
|
-
aabb.xMax -= right;
|
|
222
|
-
aabb.yMax -= bottom;
|
|
223
|
-
return aabb;
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
export const intersectPoint = (aabb: AABB, point: Point): boolean =>
|
|
227
|
-
aabb.xMin <= point.x &&
|
|
228
|
-
point.x < aabb.xMax &&
|
|
229
|
-
aabb.yMin <= point.y &&
|
|
230
|
-
point.y < aabb.yMax;
|
|
231
|
-
|
|
232
|
-
export const intersectThat = (aabb: AABB, that: AABB): boolean =>
|
|
233
|
-
that.xMin < aabb.xMax &&
|
|
234
|
-
that.xMax > aabb.xMin &&
|
|
235
|
-
that.yMin < aabb.yMax &&
|
|
236
|
-
that.yMax > aabb.yMin;
|
|
237
|
-
|
|
238
|
-
export const center = (aabb: AABB): Point => ({
|
|
239
|
-
x: (aabb.xMin + aabb.xMax) * 0.5,
|
|
240
|
-
y: (aabb.yMin + aabb.yMax) * 0.5,
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export namespace Padding {
|
|
245
|
-
export const Identity: Readonly<Padding> = Object.freeze([
|
|
246
|
-
0.0, 0.0, 0.0, 0.0,
|
|
247
|
-
]);
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
export namespace CohenSutherland {
|
|
251
|
-
export const intersects = (
|
|
252
|
-
xMin: number,
|
|
253
|
-
xMax: number,
|
|
254
|
-
yMin: number,
|
|
255
|
-
yMax: number,
|
|
256
|
-
x0: number,
|
|
257
|
-
y0: number,
|
|
258
|
-
x1: number,
|
|
259
|
-
y1: number
|
|
260
|
-
): boolean => {
|
|
261
|
-
const c0 = code(xMin, xMax, yMin, yMax, x0, y0);
|
|
262
|
-
const c1 = code(xMin, xMax, yMin, yMax, x1, y1);
|
|
263
|
-
if ((c0 | c1) === 0) {
|
|
264
|
-
return false;
|
|
265
|
-
}
|
|
266
|
-
if ((c0 & c1) !== 0) {
|
|
267
|
-
return false;
|
|
268
|
-
}
|
|
269
|
-
const s = sign(x0, y0, x1, y1, xMin, yMin);
|
|
270
|
-
return (
|
|
271
|
-
s !== sign(x0, y0, x1, y1, xMax, yMin) ||
|
|
272
|
-
s !== sign(x0, y0, x1, y1, xMax, yMax) ||
|
|
273
|
-
s !== sign(x0, y0, x1, y1, xMin, yMax)
|
|
274
|
-
);
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const code = (
|
|
278
|
-
xMin: number,
|
|
279
|
-
xMax: number,
|
|
280
|
-
yMin: number,
|
|
281
|
-
yMax: number,
|
|
282
|
-
x: number,
|
|
283
|
-
y: number
|
|
284
|
-
): int => {
|
|
285
|
-
let codeValue = 0;
|
|
286
|
-
if (x <= xMin) {
|
|
287
|
-
codeValue |= 1;
|
|
288
|
-
} else if (x >= xMax) {
|
|
289
|
-
codeValue |= 2;
|
|
290
|
-
}
|
|
291
|
-
if (y <= yMin) {
|
|
292
|
-
codeValue |= 8;
|
|
293
|
-
} else if (y >= yMax) {
|
|
294
|
-
codeValue |= 4;
|
|
295
|
-
}
|
|
296
|
-
return codeValue;
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
const sign = (
|
|
300
|
-
x0: number,
|
|
301
|
-
y0: number,
|
|
302
|
-
x1: number,
|
|
303
|
-
y1: number,
|
|
304
|
-
x2: number,
|
|
305
|
-
y2: number
|
|
306
|
-
): boolean => (x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0) >= 0;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export interface ValueAxis {
|
|
310
|
-
valueToAxis(value: number): number;
|
|
311
|
-
axisToValue(axis: number): number;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
export namespace ValueAxis {
|
|
315
|
-
export const Identity: ValueAxis = {
|
|
316
|
-
valueToAxis: (value: number): number => value,
|
|
317
|
-
axisToValue: (axis: number): number => axis,
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
export const toClamped = (
|
|
321
|
-
valueAxis: ValueAxis,
|
|
322
|
-
min: number,
|
|
323
|
-
max: number
|
|
324
|
-
): ValueAxis => ({
|
|
325
|
-
valueToAxis: (value: number): number =>
|
|
326
|
-
valueAxis.valueToAxis(clamp(value, min, max)),
|
|
327
|
-
axisToValue: (axis: number): number =>
|
|
328
|
-
clamp(valueAxis.axisToValue(axis), min, max),
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
export const createClamped = (min: number, max: number): ValueAxis => ({
|
|
332
|
-
valueToAxis: (value: number): number => clamp(value, min, max),
|
|
333
|
-
axisToValue: (axis: number): number => clamp(axis, min, max),
|
|
334
|
-
});
|
|
335
|
-
}
|
package/src/index.ts
DELETED
package/src/math.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import type { int, unitValue } from "./std";
|
|
2
|
-
|
|
3
|
-
export const TAU = Math.PI * 2.0;
|
|
4
|
-
export const PI_HALF = Math.PI / 2.0;
|
|
5
|
-
export const PI_QUART = Math.PI / 4.0;
|
|
6
|
-
export const INVERSE_SQRT_2 = 1.0 / Math.sqrt(2.0);
|
|
7
|
-
|
|
8
|
-
export const clamp = (value: number, min: number, max: number): number =>
|
|
9
|
-
Math.max(min, Math.min(value, max));
|
|
10
|
-
export const clampUnit = (value: number): unitValue =>
|
|
11
|
-
Math.max(0.0, Math.min(value, 1.0));
|
|
12
|
-
export const squashUnit = (value: unitValue, margin: unitValue): unitValue =>
|
|
13
|
-
margin + (1.0 - 2.0 * margin) * Math.max(0.0, Math.min(value, 1.0));
|
|
14
|
-
export const quantizeFloor = (value: number, interval: number): number =>
|
|
15
|
-
Math.floor(value / interval) * interval;
|
|
16
|
-
export const quantizeCeil = (value: number, interval: number): number =>
|
|
17
|
-
Math.ceil(value / interval) * interval;
|
|
18
|
-
export const quantizeRound = (value: number, interval: number): number =>
|
|
19
|
-
Math.round(value / interval) * interval;
|
|
20
|
-
export const linear = (y1: number, y2: number, mu: number): number =>
|
|
21
|
-
y1 + (y2 - y1) * mu;
|
|
22
|
-
export const exponential = (y1: number, y2: number, mu: number): number =>
|
|
23
|
-
y1 * (y2 / y1) ** mu;
|
|
24
|
-
export const cosine = (y1: number, y2: number, mu: number): number => {
|
|
25
|
-
const mu2 = (1.0 - Math.cos(mu * Math.PI)) * 0.5;
|
|
26
|
-
return y1 * (1.0 - mu2) + y2 * mu2;
|
|
27
|
-
};
|
|
28
|
-
export const mod = (value: number, range: number): number =>
|
|
29
|
-
fract(value / range) * range;
|
|
30
|
-
export const fract = (value: number): number => value - Math.floor(value);
|
|
31
|
-
export const nextPowOf2 = (n: int): int =>
|
|
32
|
-
2 ** Math.ceil(Math.log(n) / Math.log(2));
|
|
33
|
-
export const radToDeg = (rad: number): number => (rad * 180.0) / Math.PI;
|
|
34
|
-
export const degToRad = (deg: number): number => (deg / 180.0) * Math.PI;
|
package/src/std.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
export type int = number;
|
|
2
|
-
export type unitValue = number; // 0...1
|
|
3
|
-
export type Optional<T> = T | undefined;
|
|
4
|
-
export type Nullable<T> = T | null;
|
|
5
|
-
export type Maybe<T> = T | undefined | null;
|
|
6
|
-
export type ValueOrProvider<T> = T | (() => T);
|
|
7
|
-
export type Procedure<T> = (value: T) => void;
|
|
8
|
-
export type Func<U, T> = (value: U) => T;
|
|
9
|
-
export type AnyFunc = (...args: any[]) => any;
|
|
10
|
-
export type AssertType<T> = (value: unknown) => value is T;
|
|
11
|
-
export type Exec = () => void;
|
|
12
|
-
|
|
13
|
-
export const isDefined = <T>(value: Maybe<T>): value is T =>
|
|
14
|
-
value !== undefined && value !== null;
|
|
15
|
-
|
|
16
|
-
export const isUndefined = (value: unknown): value is undefined =>
|
|
17
|
-
value === undefined;
|
|
18
|
-
|
|
19
|
-
export const isNotUndefined = <T>(value: Optional<T>): value is T =>
|
|
20
|
-
value !== undefined;
|
|
21
|
-
|
|
22
|
-
export const asDefined = <T>(
|
|
23
|
-
value: Maybe<T>,
|
|
24
|
-
fail: ValueOrProvider<string> = "asDefined failed"
|
|
25
|
-
): T =>
|
|
26
|
-
value === null || value === undefined ? panic(getOrProvide(fail)) : value;
|
|
27
|
-
|
|
28
|
-
export const Unhandled = <R>(empty: never): R => {
|
|
29
|
-
throw new Error(`Unhandled ${empty}`);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const panic = (issue?: string | Error | unknown): never => {
|
|
33
|
-
throw typeof issue === "string" ? new Error(issue) : issue;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export const getOrProvide = <T>(value: ValueOrProvider<T>): T =>
|
|
37
|
-
value instanceof Function ? value() : value;
|
|
38
|
-
|
|
39
|
-
export const EmptyExec: Exec = (): void => {
|
|
40
|
-
// no-op
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
export const EmptyProcedure: Procedure<any> = (_: any): void => {
|
|
44
|
-
// no-op
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export function assertType<T>(_value: unknown): asserts _value is T {
|
|
48
|
-
// no-op
|
|
49
|
-
}
|
package/tsconfig.json
DELETED