rectpackbin 0.0.2 → 0.0.4
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/.vscode/launch.json +18 -0
- package/README.md +16 -0
- package/babel.config.js +5 -0
- package/package.json +21 -3
- package/src/index.js +80 -0
- package/src/rectpack/geometry.js +224 -0
- package/src/rectpack/guillotine copy.js +406 -0
- package/src/rectpack/guillotine.js +621 -0
- package/src/rectpack/index.js +90 -0
- package/src/rectpack/packAlgo.js +154 -0
- package/src/rectpack/packer.js +578 -0
- package/src/rectpack/packer2.js +144 -0
- package/test/geometry/hsegment.test.js +77 -0
- package/test/geometry/point.test.js +120 -0
- package/test/geometry/rectangle.test.js +197 -0
- package/test/geometry/segment.test.js +87 -0
- package/test/geometry/vsegment.test.js +77 -0
- package/test/guillotine/GuillotineBssfSas.test.js +139 -0
- package/test/guillotine/GuillotineBssfSas2.test.js +141 -0
- package/test/guillotine/guillotine.test.js +248 -0
- package/test/packAlgo/packingAlgorithm.test.js +164 -0
- package/test/packer/packer.test.js +466 -0
- package/test/packer/sort.test.js +200 -0
- package/test/index.test.js +0 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Point, Segment } from '../../src/rectpack/geometry.js';
|
|
2
|
+
|
|
3
|
+
describe('Segment', () => {
|
|
4
|
+
let point1;
|
|
5
|
+
let point2;
|
|
6
|
+
let segment;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
point1 = new Point(0, 0);
|
|
10
|
+
point2 = new Point(3, 4);
|
|
11
|
+
segment = new Segment(point1, point2);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should create a segment with valid points', () => {
|
|
16
|
+
expect(segment.start).toBe(point1);
|
|
17
|
+
expect(segment.end).toBe(point2);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should throw error when points are invalid', () => {
|
|
21
|
+
expect(() => new Segment('not a point', point2)).toThrow('Start and end must be Point instances');
|
|
22
|
+
expect(() => new Segment(point1, 'not a point')).toThrow('Start and end must be Point instances');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('equals', () => {
|
|
27
|
+
it('should return true for identical segments', () => {
|
|
28
|
+
const segment2 = new Segment(new Point(0, 0), new Point(3, 4));
|
|
29
|
+
expect(segment.equals(segment2)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return false for different segments', () => {
|
|
33
|
+
const segment2 = new Segment(new Point(0, 0), new Point(5, 5));
|
|
34
|
+
expect(segment.equals(segment2)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return false for non-segment objects', () => {
|
|
38
|
+
expect(segment.equals('not a segment')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('toString', () => {
|
|
43
|
+
it('should return correct string representation', () => {
|
|
44
|
+
expect(segment.toString()).toBe('S(P(0, 0), P(3, 4))');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('lengthSquared', () => {
|
|
49
|
+
it('should return correct squared length', () => {
|
|
50
|
+
// For points (0,0) and (3,4), lengthSquared should be 25 (3² + 4²)
|
|
51
|
+
expect(segment.lengthSquared).toBe(25);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('length', () => {
|
|
56
|
+
it('should return correct length', () => {
|
|
57
|
+
// For points (0,0) and (3,4), length should be 5
|
|
58
|
+
expect(segment.length).toBe(5);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('boundaries', () => {
|
|
63
|
+
it('should calculate correct top value', () => {
|
|
64
|
+
expect(segment.top).toBe(4);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should calculate correct bottom value', () => {
|
|
68
|
+
expect(segment.bottom).toBe(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should calculate correct right value', () => {
|
|
72
|
+
expect(segment.right).toBe(3);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should calculate correct left value', () => {
|
|
76
|
+
expect(segment.left).toBe(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should handle segments with reversed points', () => {
|
|
80
|
+
const reversedSegment = new Segment(new Point(3, 4), new Point(0, 0));
|
|
81
|
+
expect(reversedSegment.top).toBe(4);
|
|
82
|
+
expect(reversedSegment.bottom).toBe(0);
|
|
83
|
+
expect(reversedSegment.right).toBe(3);
|
|
84
|
+
expect(reversedSegment.left).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Point, VSegment } from '../../src/rectpack/geometry.js';
|
|
2
|
+
|
|
3
|
+
describe('VSegment', () => {
|
|
4
|
+
let startPoint;
|
|
5
|
+
let length;
|
|
6
|
+
let vSegment;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
startPoint = new Point(5, 0);
|
|
10
|
+
length = 10;
|
|
11
|
+
vSegment = new VSegment(startPoint, length);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should create a vertical segment with valid start point and length', () => {
|
|
16
|
+
expect(vSegment.start).toBe(startPoint);
|
|
17
|
+
expect(vSegment.end.x).toBe(startPoint.x);
|
|
18
|
+
expect(vSegment.end.y).toBe(startPoint.y + length);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should throw error when start point is invalid', () => {
|
|
22
|
+
expect(() => new VSegment('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 negativeVSegment = new VSegment(startPoint, negativeLength);
|
|
28
|
+
expect(negativeVSegment.start).toBe(startPoint);
|
|
29
|
+
expect(negativeVSegment.end.x).toBe(startPoint.x);
|
|
30
|
+
expect(negativeVSegment.end.y).toBe(startPoint.y + negativeLength);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('length', () => {
|
|
35
|
+
it('should return correct positive length', () => {
|
|
36
|
+
expect(vSegment.length).toBe(10);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should return correct negative length', () => {
|
|
40
|
+
const negativeVSegment = new VSegment(startPoint, -5);
|
|
41
|
+
expect(negativeVSegment.length).toBe(-5);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return zero for zero-length segment', () => {
|
|
45
|
+
const zeroLengthSegment = new VSegment(startPoint, 0);
|
|
46
|
+
expect(zeroLengthSegment.length).toBe(0);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('inherited properties', () => {
|
|
51
|
+
it('should calculate correct left and right values', () => {
|
|
52
|
+
expect(vSegment.left).toBe(5);
|
|
53
|
+
expect(vSegment.right).toBe(5);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should calculate correct top and bottom values', () => {
|
|
57
|
+
expect(vSegment.bottom).toBe(0);
|
|
58
|
+
expect(vSegment.top).toBe(10);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should have correct string representation', () => {
|
|
62
|
+
expect(vSegment.toString()).toBe('S(P(5, 0), P(5, 10))');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('equals', () => {
|
|
67
|
+
it('should return true for identical vertical segments', () => {
|
|
68
|
+
const vSegment2 = new VSegment(new Point(5, 0), 10);
|
|
69
|
+
expect(vSegment.equals(vSegment2)).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should return false for different vertical segments', () => {
|
|
73
|
+
const vSegment2 = new VSegment(new Point(6, 0), 10);
|
|
74
|
+
expect(vSegment.equals(vSegment2)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|