rectpackbin 0.0.2 → 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.
@@ -0,0 +1,317 @@
1
+ const { GuillotineBssfSas } = require('./guillotine');
2
+
3
+ // Helper function to convert float to decimal with specified precision
4
+ function float2dec(ft, decimalDigits) {
5
+ return Number(Math.ceil(ft * Math.pow(10, decimalDigits)) / Math.pow(10, decimalDigits));
6
+ }
7
+
8
+ // Sorting algorithms for rectangle lists
9
+ const SORT_AREA = (rectList) => [...rectList].sort((a, b) => (b[0] * b[1]) - (a[0] * a[1]));
10
+ const SORT_PERI = (rectList) => [...rectList].sort((a, b) => (b[0] + b[1]) - (a[0] + a[1]));
11
+ const SORT_DIFF = (rectList) => [...rectList].sort((a, b) => Math.abs(b[0] - b[1]) - Math.abs(a[0] - a[1]));
12
+ const SORT_SSIDE = (rectList) => [...rectList].sort((a, b) => {
13
+ const [minA, maxA] = [Math.min(a[0], a[1]), Math.max(a[0], a[1])];
14
+ const [minB, maxB] = [Math.min(b[0], b[1]), Math.max(b[0], b[1])];
15
+ return minB - minA || maxB - maxA;
16
+ });
17
+ const SORT_LSIDE = (rectList) => [...rectList].sort((a, b) => {
18
+ const [maxA, minA] = [Math.max(a[0], a[1]), Math.min(a[0], a[1])];
19
+ const [maxB, minB] = [Math.max(b[0], b[1]), Math.min(b[0], b[1])];
20
+ return maxB - maxA || minB - minA;
21
+ });
22
+ const SORT_RATIO = (rectList) => [...rectList].sort((a, b) => (b[0] / b[1]) - (a[0] / a[1]));
23
+ const SORT_NONE = (rectList) => [...rectList];
24
+
25
+ class BinFactory {
26
+ constructor(width, height, count, packAlgo, ...args) {
27
+ this._width = width;
28
+ this._height = height;
29
+ this._count = count;
30
+ this._packAlgo = packAlgo;
31
+ this._algoArgs = args;
32
+ this._refBin = null; // Reference bin used to calculate fitness
33
+ }
34
+
35
+ _createBin() {
36
+ return new this._packAlgo(this._width, this._height, ...this._algoArgs);
37
+ }
38
+
39
+ isEmpty() {
40
+ return this._count < 1;
41
+ }
42
+
43
+ fitness(width, height) {
44
+ if (!this._refBin) {
45
+ this._refBin = this._createBin();
46
+ }
47
+ return this._refBin.fitness(width, height);
48
+ }
49
+
50
+ fitsInside(width, height) {
51
+ if (!this._refBin) {
52
+ this._refBin = this._createBin();
53
+ }
54
+ return this._refBin._fitsSurface(width, height);
55
+ }
56
+
57
+ newBin() {
58
+ if (this._count > 0) {
59
+ this._count--;
60
+ return this._createBin();
61
+ }
62
+ return null;
63
+ }
64
+ }
65
+
66
+ class PackerBNFMixin {
67
+ async addRect(width, height, rid = null) {
68
+ while (true) {
69
+ if (this._openBins.length === 0) {
70
+ const newBin = await this._newOpenBin(width, height, rid);
71
+ if (!newBin) return null;
72
+ }
73
+
74
+ const rect = await this._openBins[0].addRect(width, height, rid);
75
+ if (rect) return rect;
76
+
77
+ const closedBin = this._openBins.shift();
78
+ this._closedBins.push(closedBin);
79
+ }
80
+ }
81
+ }
82
+
83
+ class PackerBFFMixin {
84
+ async addRect(width, height, rid = null) {
85
+ for (const bin of this._openBins) {
86
+ const rect = await bin.addRect(width, height, rid);
87
+ if (rect) return rect;
88
+ }
89
+
90
+ while (true) {
91
+ const newBin = await this._newOpenBin(width, height, rid);
92
+ if (!newBin) return null;
93
+
94
+ const rect = await newBin.addRect(width, height, rid);
95
+ if (rect) return rect;
96
+ }
97
+ }
98
+ }
99
+
100
+ class PackerBBFMixin {
101
+ async addRect(width, height, rid = null) {
102
+ // Try packing into open bins
103
+ const fitBins = this._openBins
104
+ .map(b => [b.fitness(width, height), b])
105
+ .filter(([fitness]) => fitness !== null);
106
+
107
+ if (fitBins.length > 0) {
108
+ const [_, bestBin] = fitBins.reduce((min, curr) =>
109
+ curr[0] < min[0] ? curr : min
110
+ );
111
+ return await bestBin.addRect(width, height, rid);
112
+ }
113
+
114
+ // Try packing into empty bins
115
+ while (true) {
116
+ const newBin = await this._newOpenBin(width, height, rid);
117
+ if (!newBin) return null;
118
+
119
+ if (await newBin.addRect(width, height, rid)) {
120
+ return true;
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ class PackerOnline {
127
+ constructor(packAlgo = GuillotineBssfSas, rotation = true) {
128
+ this._rotation = rotation;
129
+ this._packAlgo = packAlgo;
130
+ this.reset();
131
+ }
132
+
133
+ *[Symbol.iterator]() {
134
+ yield* [...this._closedBins, ...this._openBins];
135
+ }
136
+
137
+ get length() {
138
+ return this._closedBins.length + this._openBins.length;
139
+ }
140
+
141
+ _newOpenBin(width = null, height = null, rid = null) {
142
+ const factoriesToDelete = new Set();
143
+ let newBin = null;
144
+
145
+ for (const [key, binFac] of Object.entries(this._emptyBins)) {
146
+ if (!binFac.fitsInside(width, height)) continue;
147
+
148
+ newBin = binFac.newBin();
149
+ if (!newBin) continue;
150
+
151
+ this._openBins.push(newBin);
152
+
153
+ if (binFac.isEmpty()) {
154
+ factoriesToDelete.add(key);
155
+ }
156
+ break;
157
+ }
158
+
159
+ factoriesToDelete.forEach(f => delete this._emptyBins[f]);
160
+ return newBin;
161
+ }
162
+
163
+ addBin(width, height, count = 1, options = {}) {
164
+ options.rot = this._rotation;
165
+ const binFactory = new BinFactory(width, height, count, this._packAlgo, options);
166
+ this._emptyBins[this._binCount++] = binFactory;
167
+ }
168
+
169
+ rectList() {
170
+ const rectangles = [];
171
+ let binCount = 0;
172
+
173
+ for (const bin of this) {
174
+ for (const rect of bin) {
175
+ rectangles.push([binCount, rect.x, rect.y, rect.width, rect.height, rect.rid]);
176
+ }
177
+ binCount++;
178
+ }
179
+
180
+ return rectangles;
181
+ }
182
+
183
+ binList() {
184
+ return [...this].map(b => [b.width, b.height]);
185
+ }
186
+
187
+ validatePacking() {
188
+ for (const b of this) {
189
+ b.validatePacking();
190
+ }
191
+ }
192
+
193
+ reset() {
194
+ this._closedBins = [];
195
+ this._openBins = [];
196
+ this._emptyBins = new Map();
197
+ this._binCount = 0;
198
+ }
199
+ }
200
+
201
+ // Enum implementation
202
+ const PackingMode = {
203
+ Online: 0,
204
+ Offline: 1,
205
+ properties: {
206
+ 0: { name: "Online" },
207
+ 1: { name: "Offline" }
208
+ }
209
+ };
210
+
211
+ const PackingBin = {
212
+ BNF: 0, // Bin Next Fit
213
+ BFF: 1, // Bin First Fit
214
+ BBF: 2, // Bin Best Fit
215
+ Global: 3, // Global Fit
216
+ properties: {
217
+ 0: { name: "BNF" },
218
+ 1: { name: "BFF" },
219
+ 2: { name: "BBF" },
220
+ 3: { name: "Global" }
221
+ }
222
+ };
223
+
224
+ /**
225
+ * Packer factory helper function
226
+ * @param {PackingMode} mode - Packing mode (Online or Offline)
227
+ * @param {PackingBin} binAlgo - Bin selection heuristic
228
+ * @param {Function} packAlgo - Algorithm used (default: GuillotineBssfSas)
229
+ * @param {Function} sortAlgo - Sorting algorithm (default: SORT_AREA)
230
+ * @param {boolean} rotation - Enable or disable rectangle rotation
231
+ * @returns {Packer} Initialized packer instance
232
+ */
233
+ function newPacker({
234
+ mode = PackingMode.Offline,
235
+ binAlgo = PackingBin.BBF,
236
+ packAlgo = GuillotineBssfSas,
237
+ sortAlgo = SORT_AREA,
238
+ rotation = true
239
+ } = {}) {
240
+ let packerClass = null;
241
+
242
+ // Online Mode
243
+ if (mode === PackingMode.Online) {
244
+ sortAlgo = null;
245
+ switch (binAlgo) {
246
+ case PackingBin.BNF:
247
+ packerClass = PackerOnlineBNF;
248
+ break;
249
+ case PackingBin.BFF:
250
+ packerClass = PackerOnlineBFF;
251
+ break;
252
+ case PackingBin.BBF:
253
+ packerClass = PackerOnlineBBF;
254
+ break;
255
+ default:
256
+ throw new Error("Unsupported bin selection heuristic");
257
+ }
258
+ }
259
+ // Offline Mode
260
+ else if (mode === PackingMode.Offline) {
261
+ switch (binAlgo) {
262
+ case PackingBin.BNF:
263
+ packerClass = PackerBNF;
264
+ break;
265
+ case PackingBin.BFF:
266
+ packerClass = PackerBFF;
267
+ break;
268
+ case PackingBin.BBF:
269
+ packerClass = PackerBBF;
270
+ break;
271
+ case PackingBin.Global:
272
+ packerClass = PackerGlobal;
273
+ sortAlgo = null;
274
+ break;
275
+ default:
276
+ throw new Error("Unsupported bin selection heuristic");
277
+ }
278
+ }
279
+ else {
280
+ throw new Error("Unknown packing mode");
281
+ }
282
+
283
+ // Create and return the appropriate packer instance
284
+ if (sortAlgo) {
285
+ return new packerClass({
286
+ packAlgo,
287
+ sortAlgo,
288
+ rotation
289
+ });
290
+ } else {
291
+ return new packerClass({
292
+ packAlgo,
293
+ rotation
294
+ });
295
+ }
296
+ }
297
+
298
+ // Export the classes and sorting functions
299
+ module.exports = {
300
+ float2dec,
301
+ SORT_AREA,
302
+ SORT_PERI,
303
+ SORT_DIFF,
304
+ SORT_SSIDE,
305
+ SORT_LSIDE,
306
+ SORT_RATIO,
307
+ SORT_NONE,
308
+ BinFactory,
309
+ PackerBNFMixin,
310
+ PackerBFFMixin,
311
+ PackerBBFMixin,
312
+ PackerOnline,
313
+ PackingMode,
314
+ PackingBin,
315
+ newPacker
316
+ };
317
+
@@ -0,0 +1,77 @@
1
+ import { Point, HSegment } from '../../src/rectpack/geometry.js';
2
+
3
+ describe('HSegment', () => {
4
+ let startPoint;
5
+ let length;
6
+ let hSegment;
7
+
8
+ beforeEach(() => {
9
+ startPoint = new Point(0, 5);
10
+ length = 10;
11
+ hSegment = new HSegment(startPoint, length);
12
+ });
13
+
14
+ describe('constructor', () => {
15
+ it('should create a horizontal segment with valid start point and length', () => {
16
+ expect(hSegment.start).toBe(startPoint);
17
+ expect(hSegment.end.x).toBe(startPoint.x + length);
18
+ expect(hSegment.end.y).toBe(startPoint.y);
19
+ });
20
+
21
+ it('should throw error when start point is invalid', () => {
22
+ expect(() => new HSegment('not a point', 10)).toThrow('Start must be a Point and length must be a number');
23
+ });
24
+
25
+ it('should create a segment with negative length', () => {
26
+ const negativeLength = -5;
27
+ const negativeHSegment = new HSegment(startPoint, negativeLength);
28
+ expect(negativeHSegment.start).toBe(startPoint);
29
+ expect(negativeHSegment.end.x).toBe(startPoint.x + negativeLength);
30
+ expect(negativeHSegment.end.y).toBe(startPoint.y);
31
+ });
32
+ });
33
+
34
+ describe('length', () => {
35
+ it('should return correct positive length', () => {
36
+ expect(hSegment.length).toBe(10);
37
+ });
38
+
39
+ it('should return correct negative length', () => {
40
+ const negativeHSegment = new HSegment(startPoint, -5);
41
+ expect(negativeHSegment.length).toBe(-5);
42
+ });
43
+
44
+ it('should return zero for zero-length segment', () => {
45
+ const zeroLengthSegment = new HSegment(startPoint, 0);
46
+ expect(zeroLengthSegment.length).toBe(0);
47
+ });
48
+ });
49
+
50
+ describe('inherited properties', () => {
51
+ it('should calculate correct top and bottom values', () => {
52
+ expect(hSegment.top).toBe(5);
53
+ expect(hSegment.bottom).toBe(5);
54
+ });
55
+
56
+ it('should calculate correct left and right values', () => {
57
+ expect(hSegment.left).toBe(0);
58
+ expect(hSegment.right).toBe(10);
59
+ });
60
+
61
+ it('should have correct string representation', () => {
62
+ expect(hSegment.toString()).toBe('S(P(0, 5), P(10, 5))');
63
+ });
64
+ });
65
+
66
+ describe('equals', () => {
67
+ it('should return true for identical horizontal segments', () => {
68
+ const hSegment2 = new HSegment(new Point(0, 5), 10);
69
+ expect(hSegment.equals(hSegment2)).toBe(true);
70
+ });
71
+
72
+ it('should return false for different horizontal segments', () => {
73
+ const hSegment2 = new HSegment(new Point(0, 6), 10);
74
+ expect(hSegment.equals(hSegment2)).toBe(false);
75
+ });
76
+ });
77
+ });
@@ -0,0 +1,120 @@
1
+ const { Point } = require('../../src/rectpack/geometry');
2
+
3
+ describe('Point', () => {
4
+ // Test constructor
5
+ describe('constructor', () => {
6
+ test('should create a point with given x and y coordinates', () => {
7
+ const point = new Point(2, 3);
8
+ expect(point.x).toBe(2);
9
+ expect(point.y).toBe(3);
10
+ });
11
+
12
+ test('should handle negative coordinates', () => {
13
+ const point = new Point(-1, -5);
14
+ expect(point.x).toBe(-1);
15
+ expect(point.y).toBe(-5);
16
+ });
17
+
18
+ test('should handle zero coordinates', () => {
19
+ const point = new Point(0, 0);
20
+ expect(point.x).toBe(0);
21
+ expect(point.y).toBe(0);
22
+ });
23
+
24
+ test('should handle floating point coordinates', () => {
25
+ const point = new Point(1.5, 2.7);
26
+ expect(point.x).toBe(1.5);
27
+ expect(point.y).toBe(2.7);
28
+ });
29
+ });
30
+
31
+ // Test equals method
32
+ describe('equals', () => {
33
+ test('should return true for points with same coordinates', () => {
34
+ const point1 = new Point(2, 3);
35
+ const point2 = new Point(2, 3);
36
+ expect(point1.equals(point2)).toBe(true);
37
+ });
38
+
39
+ test('should return false for points with different coordinates', () => {
40
+ const point1 = new Point(2, 3);
41
+ const point2 = new Point(3, 2);
42
+ expect(point1.equals(point2)).toBe(false);
43
+ });
44
+
45
+ test('should handle comparison with same point instance', () => {
46
+ const point = new Point(2, 3);
47
+ expect(point.equals(point)).toBe(true);
48
+ });
49
+ });
50
+
51
+ // Test toString method
52
+ describe('toString', () => {
53
+ test('should return correct string representation', () => {
54
+ const point = new Point(2, 3);
55
+ expect(point.toString()).toBe('P(2, 3)');
56
+ });
57
+
58
+ test('should handle negative coordinates in string representation', () => {
59
+ const point = new Point(-1, -5);
60
+ expect(point.toString()).toBe('P(-1, -5)');
61
+ });
62
+
63
+ test('should handle floating point coordinates in string representation', () => {
64
+ const point = new Point(1.5, 2.7);
65
+ expect(point.toString()).toBe('P(1.5, 2.7)');
66
+ });
67
+ });
68
+
69
+ // Test distance method
70
+ describe('distance', () => {
71
+ test('should calculate correct distance between two points', () => {
72
+ const point1 = new Point(0, 0);
73
+ const point2 = new Point(3, 4);
74
+ expect(point1.distance(point2)).toBe(5);
75
+ });
76
+
77
+ test('should return 0 for distance to same point', () => {
78
+ const point = new Point(2, 3);
79
+ expect(point.distance(point)).toBe(0);
80
+ });
81
+
82
+ test('should handle negative coordinates', () => {
83
+ const point1 = new Point(-1, -1);
84
+ const point2 = new Point(2, 3);
85
+ expect(point1.distance(point2)).toBe(5);
86
+ });
87
+
88
+ test('should be symmetric', () => {
89
+ const point1 = new Point(1, 2);
90
+ const point2 = new Point(4, 6);
91
+ expect(point1.distance(point2)).toBe(point2.distance(point1));
92
+ });
93
+ });
94
+
95
+ // Test distanceSquared method
96
+ describe('distanceSquared', () => {
97
+ test('should calculate correct squared distance between two points', () => {
98
+ const point1 = new Point(0, 0);
99
+ const point2 = new Point(3, 4);
100
+ expect(point1.distanceSquared(point2)).toBe(25);
101
+ });
102
+
103
+ test('should return 0 for squared distance to same point', () => {
104
+ const point = new Point(2, 3);
105
+ expect(point.distanceSquared(point)).toBe(0);
106
+ });
107
+
108
+ test('should handle negative coordinates', () => {
109
+ const point1 = new Point(-1, -1);
110
+ const point2 = new Point(2, 3);
111
+ expect(point1.distanceSquared(point2)).toBe(25);
112
+ });
113
+
114
+ test('should be symmetric', () => {
115
+ const point1 = new Point(1, 2);
116
+ const point2 = new Point(4, 6);
117
+ expect(point1.distanceSquared(point2)).toBe(point2.distanceSquared(point1));
118
+ });
119
+ });
120
+ });
@@ -0,0 +1,197 @@
1
+ import { Point, Rectangle } from '../../src/rectpack/geometry.js';
2
+
3
+ describe('Rectangle', () => {
4
+ let rectangle;
5
+
6
+ beforeEach(() => {
7
+ rectangle = new Rectangle(10, 20, 30, 40, 'rect1');
8
+ });
9
+
10
+ describe('constructor', () => {
11
+ it('should create a rectangle with valid parameters', () => {
12
+ expect(rectangle.x).toBe(10);
13
+ expect(rectangle.y).toBe(20);
14
+ expect(rectangle.width).toBe(30);
15
+ expect(rectangle.height).toBe(40);
16
+ expect(rectangle.rid).toBe('rect1');
17
+ });
18
+
19
+ it('should create a rectangle without rid', () => {
20
+ const rectWithoutId = new Rectangle(10, 20, 30, 40);
21
+ expect(rectWithoutId.rid).toBeNull();
22
+ });
23
+
24
+ it('should throw error for negative dimensions', () => {
25
+ expect(() => new Rectangle(10, 20, -30, 40)).toThrow('Height and width must be non-negative');
26
+ expect(() => new Rectangle(10, 20, 30, -40)).toThrow('Height and width must be non-negative');
27
+ });
28
+ });
29
+
30
+ describe('boundaries', () => {
31
+ it('should calculate correct bottom value', () => {
32
+ expect(rectangle.bottom).toBe(20);
33
+ });
34
+
35
+ it('should calculate correct top value', () => {
36
+ expect(rectangle.top).toBe(60); // y + height = 20 + 40
37
+ });
38
+
39
+ it('should calculate correct left value', () => {
40
+ expect(rectangle.left).toBe(10);
41
+ });
42
+
43
+ it('should calculate correct right value', () => {
44
+ expect(rectangle.right).toBe(40); // x + width = 10 + 30
45
+ });
46
+ });
47
+
48
+ describe('corners', () => {
49
+ it('should calculate correct top-left corner', () => {
50
+ const corner = rectangle.cornerTopL;
51
+ expect(corner instanceof Point).toBe(true);
52
+ expect(corner.x).toBe(10);
53
+ expect(corner.y).toBe(60);
54
+ });
55
+
56
+ it('should calculate correct top-right corner', () => {
57
+ const corner = rectangle.cornerTopR;
58
+ expect(corner.x).toBe(40);
59
+ expect(corner.y).toBe(60);
60
+ });
61
+
62
+ it('should calculate correct bottom-right corner', () => {
63
+ const corner = rectangle.cornerBotR;
64
+ expect(corner.x).toBe(40);
65
+ expect(corner.y).toBe(20);
66
+ });
67
+
68
+ it('should calculate correct bottom-left corner', () => {
69
+ const corner = rectangle.cornerBotL;
70
+ expect(corner.x).toBe(10);
71
+ expect(corner.y).toBe(20);
72
+ });
73
+ });
74
+
75
+ describe('area', () => {
76
+ it('should calculate correct area', () => {
77
+ expect(rectangle.area()).toBe(1200); // 30 * 40
78
+ });
79
+
80
+ it('should return zero for zero-width rectangle', () => {
81
+ const zeroWidth = new Rectangle(10, 20, 0, 40);
82
+ expect(zeroWidth.area()).toBe(0);
83
+ });
84
+
85
+ it('should return zero for zero-height rectangle', () => {
86
+ const zeroHeight = new Rectangle(10, 20, 30, 0);
87
+ expect(zeroHeight.area()).toBe(0);
88
+ });
89
+ });
90
+
91
+ describe('move', () => {
92
+ it('should update position correctly', () => {
93
+ rectangle.move(50, 60);
94
+ expect(rectangle.x).toBe(50);
95
+ expect(rectangle.y).toBe(60);
96
+ });
97
+ });
98
+
99
+ describe('contains', () => {
100
+ it('should return true for contained rectangle', () => {
101
+ const contained = new Rectangle(15, 25, 10, 10);
102
+ expect(rectangle.contains(contained)).toBe(true);
103
+ });
104
+
105
+ it('should return false for overlapping rectangle', () => {
106
+ const overlapping = new Rectangle(5, 15, 20, 20);
107
+ expect(rectangle.contains(overlapping)).toBe(false);
108
+ });
109
+
110
+ it('should return false for non-intersecting rectangle', () => {
111
+ const outside = new Rectangle(100, 100, 10, 10);
112
+ expect(rectangle.contains(outside)).toBe(false);
113
+ });
114
+ });
115
+
116
+ describe('intersects', () => {
117
+ it('should return true for overlapping rectangles', () => {
118
+ const overlapping = new Rectangle(20, 30, 30, 40);
119
+ expect(rectangle.intersects(overlapping)).toBe(true);
120
+ });
121
+
122
+ it('should return false for non-intersecting rectangles', () => {
123
+ const nonOverlapping = new Rectangle(100, 100, 10, 10);
124
+ expect(rectangle.intersects(nonOverlapping)).toBe(false);
125
+ });
126
+
127
+ it('should handle edge cases correctly', () => {
128
+ const touching = new Rectangle(40, 20, 10, 40);
129
+ expect(rectangle.intersects(touching, false)).toBe(false);
130
+ expect(rectangle.intersects(touching, true)).toBe(true);
131
+ });
132
+ });
133
+
134
+ describe('intersection', () => {
135
+ it('should return null for non-intersecting rectangles', () => {
136
+ const nonOverlapping = new Rectangle(100, 100, 10, 10);
137
+ expect(rectangle.intersection(nonOverlapping)).toBeNull();
138
+ });
139
+
140
+ it('should return correct intersection rectangle', () => {
141
+ const overlapping = new Rectangle(20, 30, 30, 40);
142
+ const intersection = rectangle.intersection(overlapping);
143
+ expect(intersection.x).toBe(20);
144
+ expect(intersection.y).toBe(30);
145
+ expect(intersection.width).toBe(20);
146
+ expect(intersection.height).toBe(30);
147
+ });
148
+ });
149
+
150
+ describe('join', () => {
151
+ it('should return true and not modify when containing other rectangle', () => {
152
+ const contained = new Rectangle(15, 25, 10, 10);
153
+ const originalX = rectangle.x;
154
+ const originalY = rectangle.y;
155
+ const originalWidth = rectangle.width;
156
+ const originalHeight = rectangle.height;
157
+
158
+ expect(rectangle.join(contained)).toBe(true);
159
+ expect(rectangle.x).toBe(originalX);
160
+ expect(rectangle.y).toBe(originalY);
161
+ expect(rectangle.width).toBe(originalWidth);
162
+ expect(rectangle.height).toBe(originalHeight);
163
+ });
164
+
165
+ it('should return true and update when other rectangle contains this', () => {
166
+ const larger = new Rectangle(5, 15, 50, 50);
167
+ expect(rectangle.join(larger)).toBe(true);
168
+ expect(rectangle.x).toBe(5);
169
+ expect(rectangle.y).toBe(15);
170
+ expect(rectangle.width).toBe(50);
171
+ expect(rectangle.height).toBe(50);
172
+ });
173
+
174
+ it('should join adjacent rectangles vertically', () => {
175
+ const adjacent = new Rectangle(10, 60, 30, 20);
176
+ expect(rectangle.join(adjacent)).toBe(true);
177
+ expect(rectangle.x).toBe(10);
178
+ expect(rectangle.y).toBe(20);
179
+ expect(rectangle.width).toBe(30);
180
+ expect(rectangle.height).toBe(60);
181
+ });
182
+
183
+ it('should join adjacent rectangles horizontally', () => {
184
+ const adjacent = new Rectangle(40, 20, 20, 40);
185
+ expect(rectangle.join(adjacent)).toBe(true);
186
+ expect(rectangle.x).toBe(10);
187
+ expect(rectangle.y).toBe(20);
188
+ expect(rectangle.width).toBe(50);
189
+ expect(rectangle.height).toBe(40);
190
+ });
191
+
192
+ it('should return false for non-adjacent rectangles', () => {
193
+ const nonAdjacent = new Rectangle(100, 100, 10, 10);
194
+ expect(rectangle.join(nonAdjacent)).toBe(false);
195
+ });
196
+ });
197
+ });