-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshapes.ts
More file actions
31 lines (27 loc) · 835 Bytes
/
shapes.ts
File metadata and controls
31 lines (27 loc) · 835 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
type Rect = { x: number, y: number, width: number, height: number };
class Shape {
rect: Rect;
constructor(rect: Rect) {
this.rect = rect;
}
}
class ShapeManager {
shapes: Shape[] = [];
// Check if two rectangles overlap
private isOverlap(a: Rect, b: Rect): boolean {
return !(a.x + a.width <= b.x ||
b.x + b.width <= a.x ||
a.y + a.height <= b.y ||
b.y + b.height <= a.y);
}
// Try to add a shape, only if it doesn't overlap
addShape(newShape: Shape): boolean {
for (const shape of this.shapes) {
if (this.isOverlap(shape.rect, newShape.rect)) {
return false; // Overlap detected
}
}
this.shapes.push(newShape);
return true; // Successfully added
}
}