-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
150 lines (117 loc) · 5.43 KB
/
example.ts
File metadata and controls
150 lines (117 loc) · 5.43 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
Rectangle,
Point,
ContentAlignment,
RectOps,
RectCompare,
RectTransform,
RectGeometry,
RectBorder,
RectSplit
} from './src/index';
// Example 1: Creating rectangles
console.log('=== Creating Rectangles ===');
const rect1: Rectangle = { x: 10, y: 20, width: 100, height: 50 };
console.log('rect1:', rect1);
const rect2 = RectOps.create(0, 0, 100, 100);
console.log('rect2 (from coordinates):', rect2);
const pt1: Point = { x: 50, y: 50 };
const pt2: Point = { x: 150, y: 100 };
const rect3 = RectOps.fromPoints(pt1, pt2);
console.log('rect3 (from points):', rect3);
// Example 2: Center and corners
console.log('\n=== Rectangle Center ===');
const center = RectGeometry.center(rect1);
console.log('Center of rect1:', center);
console.log('\n=== Rectangle Corners ===');
console.log('Top-left:', RectGeometry.topLeft(rect1));
console.log('Top-right:', RectGeometry.topRight(rect1));
console.log('Bottom-left:', RectGeometry.bottomLeft(rect1));
console.log('Bottom-right:', RectGeometry.bottomRight(rect1));
// Example 3: Intersection and containment
console.log('\n=== Intersection and Containment ===');
const rect4 = { x: 50, y: 30, width: 80, height: 60 };
console.log('rect1 and rect4 intersect?', RectCompare.intersects(rect1, rect4));
const smallRect = { x: 20, y: 25, width: 30, height: 20 };
console.log('rect1 contains smallRect?', RectCompare.contains(rect1, smallRect));
const point: Point = { x: 60, y: 45 };
console.log('rect1 contains point (60, 45)?', RectCompare.containsPoint(rect1, point));
// Example 4: Alignment
console.log('\n=== Alignment ===');
const baseRect = { x: 0, y: 0, width: 200, height: 200 };
const toAlign = { x: 0, y: 0, width: 50, height: 50 };
const alignedCenter = RectTransform.align(toAlign, baseRect, ContentAlignment.MiddleCenter);
console.log('Center aligned:', alignedCenter);
const alignedTopRight = RectTransform.align(toAlign, baseRect, ContentAlignment.TopRight);
console.log('Top-right aligned:', alignedTopRight);
const alignedBottomLeft = RectTransform.align(toAlign, baseRect, ContentAlignment.BottomLeft);
console.log('Bottom-left aligned:', alignedBottomLeft);
// Example 5: Splitting into cells
console.log('\n=== Splitting into Cells ===');
const container = { x: 0, y: 0, width: 300, height: 200 };
const horizontalCells = RectSplit.horizontal(container, 3);
console.log('3 horizontal cells:', horizontalCells);
const verticalCells = RectSplit.vertical(container, 4);
console.log('4 vertical cells:', verticalCells);
// Example 6: Inflate and Deflate
console.log('\n=== Inflate and Deflate ===');
const original = { x: 50, y: 50, width: 100, height: 100 };
console.log('Original:', original);
const inflated = RectTransform.inflate(original, 10, 10, 10, 10);
console.log('After inflate (10px):', inflated);
const deflated = RectTransform.deflate(original, 5, 5, 5, 5);
console.log('After deflate (5px):', deflated);
// Example 7: Offset (movement)
console.log('\n=== Offset ===');
const moved = RectTransform.offset(original, 20, 30);
console.log('Moved by (20, 30):', moved);
// Example 8: Border operations
console.log('\n=== Border Operations ===');
const mainRect = { x: 0, y: 0, width: 100, height: 100 };
const topPart = RectBorder.getTopSide(mainRect, 20);
console.log('Top part (20px):', topPart);
const withoutTop = RectBorder.cutFromTop(mainRect, 20);
console.log('Without top 20px:', withoutTop);
const expanded = RectBorder.expandToBottom(mainRect, 30);
console.log('Expanded down by 30px:', expanded);
// Example 9: Practical scenario - creating a 3x3 grid
console.log('\n=== Practical Example: 3x3 Grid ===');
const gridContainer = { x: 0, y: 0, width: 300, height: 300 };
const rows = RectSplit.vertical(gridContainer, 3);
const grid: Rectangle[][] = rows.map(row => RectSplit.horizontal(row, 3));
console.log('3x3 Grid:');
grid.forEach((row, i) => {
console.log(`Row ${i}:`, row);
});
// Example 10: Panel with header and content
console.log('\n=== Panel with Header ===');
const panel = { x: 10, y: 10, width: 400, height: 300 };
const headerHeight = 40;
const padding = 10;
const header = RectBorder.getTopSide(panel, headerHeight);
const contentArea = RectBorder.cutFromTop(panel, headerHeight);
const contentWithPadding = RectTransform.deflate(contentArea, padding, padding, padding, padding);
console.log('Header:', header);
console.log('Content area:', contentArea);
console.log('Content with padding:', contentWithPadding);
// Example 11: Validation and comparison
console.log('\n=== Validation and Comparison ===');
const invalidRect = { x: 100, y: 100, width: -50, height: -30 };
const validRect = RectOps.validate(invalidRect);
console.log('Invalid rect:', invalidRect);
console.log('Validated rect:', validRect);
const rect5 = { x: 10, y: 20, width: 100, height: 50 };
const rect6 = { x: 10, y: 20, width: 100, height: 50 };
console.log('rect5 equals rect6?', RectCompare.equals(rect5, rect6));
// Example 12: Intersection calculation
console.log('\n=== Intersection Calculation ===');
const rectA = { x: 0, y: 0, width: 100, height: 100 };
const rectB = { x: 50, y: 50, width: 100, height: 100 };
const intersection = RectCompare.intersect(rectA, rectB);
console.log('Intersection of rectA and rectB:', intersection);
// Example 13: Union of rectangles
console.log('\n=== Union of Rectangles ===');
const rectC = { x: 0, y: 0, width: 50, height: 50 };
const rectD = { x: 100, y: 100, width: 50, height: 50 };
const union = RectCompare.unionNonEmpty(rectC, rectD);
console.log('Union of rectC and rectD:', union);