@mark1russell7/packing 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +67 -2
- package/build/index.d.ts +209 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +560 -0
- package/build/index.js.map +1 -0
- package/package.json +61 -3
- package/src/index.ts +8 -0
- package/src/metrics.ts +72 -0
- package/src/order.ts +26 -0
- package/src/pack.ts +137 -0
- package/src/push.ts +83 -0
- package/src/rect.ts +73 -0
- package/src/shelf.ts +121 -0
- package/src/skyline.ts +83 -0
- package/src/vector.ts +80 -0
package/src/vector.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** A mutable two-dimensional vector. */
|
|
2
|
+
export class Vector {
|
|
3
|
+
#x: number;
|
|
4
|
+
#y: number;
|
|
5
|
+
|
|
6
|
+
constructor(x = 0, y = 0) {
|
|
7
|
+
this.#x = x;
|
|
8
|
+
this.#y = y;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The first component: a width or a left position. */
|
|
12
|
+
get x(): number { return this.#x; }
|
|
13
|
+
set x(v: number) { this.#x = v; }
|
|
14
|
+
/** The second component: a height or a top position. */
|
|
15
|
+
get y(): number { return this.#y; }
|
|
16
|
+
set y(v: number) { this.#y = v; }
|
|
17
|
+
|
|
18
|
+
/** This method sets both components. */
|
|
19
|
+
set(x: number, y: number): void {
|
|
20
|
+
this.#x = x;
|
|
21
|
+
this.#y = y;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** This method copies the components of another vector. */
|
|
25
|
+
setTo(v: Vector): void {
|
|
26
|
+
this.#x = v.x;
|
|
27
|
+
this.#y = v.y;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** This method gives a new vector: the sum of this vector and another. */
|
|
31
|
+
add(other: Vector): Vector {
|
|
32
|
+
return new Vector(this.#x + other.x, this.#y + other.y);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** This method gives the product of the components: the area of a size. */
|
|
36
|
+
area(): number {
|
|
37
|
+
return this.#x * this.#y;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** This method gives a new vector with the same components. */
|
|
41
|
+
clone(): Vector {
|
|
42
|
+
return new Vector(this.#x, this.#y);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** This method gives a new vector with each component between the components of `min` and `max`. */
|
|
46
|
+
clamp(min: Vector, max: Vector): Vector {
|
|
47
|
+
return new Vector(
|
|
48
|
+
Math.max(min.x, Math.min(max.x, this.#x)),
|
|
49
|
+
Math.max(min.y, Math.min(max.y, this.#y)),
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** This function gives a vector with the same value in both components. */
|
|
54
|
+
static of(scalar: number): Vector {
|
|
55
|
+
return new Vector(scalar, scalar);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** This function gives the smallest value of each component, or a zero vector for an empty list. */
|
|
59
|
+
static min(vectors: Vector[]): Vector {
|
|
60
|
+
if (!vectors.length) return new Vector();
|
|
61
|
+
return new Vector(
|
|
62
|
+
Math.min(...vectors.map((v) => v.x)),
|
|
63
|
+
Math.min(...vectors.map((v) => v.y)),
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** This function gives the largest value of each component, or a zero vector for an empty list. */
|
|
68
|
+
static max(vectors: Vector[]): Vector {
|
|
69
|
+
if (!vectors.length) return new Vector();
|
|
70
|
+
return new Vector(
|
|
71
|
+
Math.max(...vectors.map((v) => v.x)),
|
|
72
|
+
Math.max(...vectors.map((v) => v.y)),
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** This getter gives a new infinite vector each time. A shared value is not safe, because a vector is mutable. */
|
|
77
|
+
static get Infinity(): Vector {
|
|
78
|
+
return new Vector(Infinity, Infinity);
|
|
79
|
+
}
|
|
80
|
+
}
|