-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrectangle_examples_test.go
572 lines (483 loc) · 15.1 KB
/
rectangle_examples_test.go
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package geom2d_test
import (
"fmt"
"github.com/mikenye/geom2d"
"image"
)
func ExampleNewRectangle() {
// Define four points that form an axis-aligned rectangle
points := []geom2d.Point[int]{
geom2d.NewPoint(1, 1), // bottom-left
geom2d.NewPoint(4, 1), // bottom-right
geom2d.NewPoint(1, 3), // top-left
geom2d.NewPoint(4, 3), // top-right
}
// Create the rectangle
rect := geom2d.NewRectangle(points)
// Print the corners of the rectangle
for _, point := range rect.Contour() {
fmt.Println(point)
}
// Output:
// Point[(1, 3)]
// Point[(4, 3)]
// Point[(4, 1)]
// Point[(1, 1)]
}
func ExampleNewRectangleFromImageRect() {
// Define an image.Rectangle
imgRect := image.Rect(10, 20, 50, 80)
// Create a Rectangle[int] from the image.Rectangle
rect := geom2d.NewRectangleFromImageRect(imgRect)
// Print the corners of the rectangle
for _, point := range rect.Contour() {
fmt.Println(point)
}
// Output:
// Point[(10, 80)]
// Point[(50, 80)]
// Point[(50, 20)]
// Point[(10, 20)]
}
func ExampleRectangle_Area() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 5),
geom2d.NewPoint(0, 5),
})
// Calculate the area
area := rect.Area()
// Print the area
fmt.Printf("The area of the rectangle is: %d\n", area)
// Output:
// The area of the rectangle is: 50
}
func ExampleRectangle_AsFloat32() {
// Create an integer-based rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 5),
geom2d.NewPoint(0, 5),
})
// Convert the rectangle to a float32-based rectangle
rectFloat32 := rect.AsFloat32()
// Print the original and converted rectangle
fmt.Println("Original rectangle (int):")
for _, point := range rect.Contour() {
fmt.Println(point)
}
fmt.Println("Converted rectangle (float32):")
for _, point := range rectFloat32.Contour() {
fmt.Println(point)
}
// Output:
// Original rectangle (int):
// Point[(0, 5)]
// Point[(10, 5)]
// Point[(10, 0)]
// Point[(0, 0)]
// Converted rectangle (float32):
// Point[(0, 5)]
// Point[(10, 5)]
// Point[(10, 0)]
// Point[(0, 0)]
}
func ExampleRectangle_AsFloat64() {
// Create an integer-based rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 5),
geom2d.NewPoint(0, 5),
})
// Convert the rectangle to a float64-based rectangle
rectFloat64 := rect.AsFloat64()
// Print the original and converted rectangle
fmt.Println("Original rectangle (int):")
for _, point := range rect.Contour() {
fmt.Println(point)
}
fmt.Println("Converted rectangle (float64):")
for _, point := range rectFloat64.Contour() {
fmt.Println(point)
}
// Output:
// Original rectangle (int):
// Point[(0, 5)]
// Point[(10, 5)]
// Point[(10, 0)]
// Point[(0, 0)]
// Converted rectangle (float64):
// Point[(0, 5)]
// Point[(10, 5)]
// Point[(10, 0)]
// Point[(0, 0)]
}
func ExampleRectangle_AsInt() {
// Create a float64-based rectangle
rect := geom2d.NewRectangle([]geom2d.Point[float64]{
geom2d.NewPoint(0.5, 0.5),
geom2d.NewPoint(10.5, 0.5),
geom2d.NewPoint(10.5, 5.5),
geom2d.NewPoint(0.5, 5.5),
})
// Convert the rectangle to an int-based rectangle
rectInt := rect.AsInt()
// Print the original and converted rectangle
fmt.Println("Original rectangle (float64):")
for _, point := range rect.Contour() {
fmt.Println(point)
}
fmt.Println("Converted rectangle (int):")
for _, point := range rectInt.Contour() {
fmt.Println(point)
}
// Output:
// Original rectangle (float64):
// Point[(0.5, 5.5)]
// Point[(10.5, 5.5)]
// Point[(10.5, 0.5)]
// Point[(0.5, 0.5)]
// Converted rectangle (int):
// Point[(0, 5)]
// Point[(10, 5)]
// Point[(10, 0)]
// Point[(0, 0)]
}
func ExampleRectangle_AsIntRounded() {
// Create a float64-based rectangle
rect := geom2d.NewRectangle([]geom2d.Point[float64]{
geom2d.NewPoint(0.5, 0.5),
geom2d.NewPoint(10.7, 0.5),
geom2d.NewPoint(10.7, 5.3),
geom2d.NewPoint(0.5, 5.3),
})
// Convert the rectangle to an int-based rectangle with rounding
rectIntRounded := rect.AsIntRounded()
// Print the original and converted rectangle
fmt.Println("Original rectangle (float64):")
for _, point := range rect.Contour() {
fmt.Println(point)
}
fmt.Println("Converted rectangle (int):")
for _, point := range rectIntRounded.Contour() {
fmt.Println(point)
}
// Output:
// Original rectangle (float64):
// Point[(0.5, 5.3)]
// Point[(10.7, 5.3)]
// Point[(10.7, 0.5)]
// Point[(0.5, 0.5)]
// Converted rectangle (int):
// Point[(1, 5)]
// Point[(11, 5)]
// Point[(11, 1)]
// Point[(1, 1)]
}
func ExampleRectangle_ContainsPoint() {
// Create an integer-based rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(0, 0),
})
// Check if the rectangle contains various points
points := []geom2d.Point[int]{
geom2d.NewPoint(5, 5), // Inside
geom2d.NewPoint(0, 10), // On the top-left corner
geom2d.NewPoint(10, 0), // On the bottom-right corner
geom2d.NewPoint(-1, 5), // Outside (left)
geom2d.NewPoint(5, 11), // Outside (above)
}
for _, p := range points {
fmt.Printf("Point %v contained: %v\n", p, rect.ContainsPoint(p))
}
// Output:
// Point Point[(5, 5)] contained: true
// Point Point[(0, 10)] contained: true
// Point Point[(10, 0)] contained: true
// Point Point[(-1, 5)] contained: false
// Point Point[(5, 11)] contained: false
}
func ExampleRectangle_Edges() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 5),
geom2d.NewPoint(0, 5),
})
// Get the edges of the rectangle
edges := rect.Edges()
// Print the edges
for i, edge := range edges {
fmt.Printf("Edge %d: Start %v, End %v\n", i+1, edge.Start(), edge.End())
}
// Output:
// Edge 1: Start Point[(0, 0)], End Point[(10, 0)]
// Edge 2: Start Point[(10, 0)], End Point[(10, 5)]
// Edge 3: Start Point[(10, 5)], End Point[(0, 5)]
// Edge 4: Start Point[(0, 5)], End Point[(0, 0)]
}
func ExampleRectangle_Eq() {
// Create two identical rectangles
rect1 := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(0, 0),
})
rect2 := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(0, 0),
})
// Create a different rectangle
rect3 := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(1, 11),
geom2d.NewPoint(11, 11),
geom2d.NewPoint(11, 1),
geom2d.NewPoint(1, 1),
})
// Compare the rectangles
fmt.Println("rect1 equals rect2:", rect1.Eq(rect2))
fmt.Println("rect1 equals rect3:", rect1.Eq(rect3))
// Output:
// rect1 equals rect2: true
// rect1 equals rect3: false
}
func ExampleRectangle_Height() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(10, 0), // bottom-right
geom2d.NewPoint(0, 0), // bottom-left
})
// Calculate and print the height of the rectangle
fmt.Println("Height of the rectangle:", rect.Height())
// Output:
// Height of the rectangle: 10
}
func ExampleRectangle_Perimeter() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(0, 0),
})
// Calculate and print the perimeter of the rectangle
fmt.Println("Perimeter of the rectangle:", rect.Perimeter())
// Output:
// Perimeter of the rectangle: 40
}
func ExampleRectangle_Contour() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(10, 0), // bottom-right
geom2d.NewPoint(0, 0), // bottom-left
})
// Get the points of the rectangle
points := rect.Contour()
// Print the points
fmt.Println("Contour of the rectangle:")
for _, point := range points {
fmt.Println(point)
}
// Output:
// Contour of the rectangle:
// Point[(0, 10)]
// Point[(10, 10)]
// Point[(10, 0)]
// Point[(0, 0)]
}
func ExampleRectangle_RelationshipToCircle() {
// Define a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint[int](0, 0),
geom2d.NewPoint[int](100, 0),
geom2d.NewPoint[int](100, 100),
geom2d.NewPoint[int](0, 100),
})
// Define circles to test
circleInside := geom2d.NewCircle(geom2d.NewPoint[int](50, 50), 10)
circleIntersecting := geom2d.NewCircle(geom2d.NewPoint[int](50, 50), 60)
circleOutside := geom2d.NewCircle(geom2d.NewPoint[int](200, 200), 20)
// Check relationships
fmt.Println("Circle inside:", rect.RelationshipToCircle(circleInside))
fmt.Println("Circle intersecting:", rect.RelationshipToCircle(circleIntersecting))
fmt.Println("Circle outside:", rect.RelationshipToCircle(circleOutside))
// Output:
// Circle inside: RelationshipContains
// Circle intersecting: RelationshipIntersection
// Circle outside: RelationshipDisjoint
}
func ExampleRectangle_RelationshipToLineSegment() {
// Define a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint[int](0, 0),
geom2d.NewPoint[int](100, 0),
geom2d.NewPoint[int](100, 100),
geom2d.NewPoint[int](0, 100),
})
// Define line segments to check against the rectangle
segmentInside := geom2d.NewLineSegment(geom2d.NewPoint(10, 10), geom2d.NewPoint(90, 90))
segmentIntersecting := geom2d.NewLineSegment(geom2d.NewPoint(-10, 50), geom2d.NewPoint(110, 50))
segmentOutside := geom2d.NewLineSegment(geom2d.NewPoint(200, 200), geom2d.NewPoint(300, 300))
// Check relationships
fmt.Println("Segment inside:", rect.RelationshipToLineSegment(segmentInside))
fmt.Println("Segment intersecting:", rect.RelationshipToLineSegment(segmentIntersecting))
fmt.Println("Segment outside:", rect.RelationshipToLineSegment(segmentOutside))
// Output:
// Segment inside: RelationshipContains
// Segment intersecting: RelationshipIntersection
// Segment outside: RelationshipDisjoint
}
func ExampleRectangle_RelationshipToPoint() {
// Define a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(100, 0),
geom2d.NewPoint(100, 100),
geom2d.NewPoint(0, 100),
})
// Define points to check against the rectangle
pointInside := geom2d.NewPoint(50, 50)
pointOnEdge := geom2d.NewPoint(0, 50)
pointOnVertex := geom2d.NewPoint(0, 0)
pointOutside := geom2d.NewPoint(200, 200)
// Check relationships
fmt.Println("Point inside:", rect.RelationshipToPoint(pointInside))
fmt.Println("Point on edge:", rect.RelationshipToPoint(pointOnEdge))
fmt.Println("Point on vertex:", rect.RelationshipToPoint(pointOnVertex))
fmt.Println("Point outside:", rect.RelationshipToPoint(pointOutside))
// Output:
// Point inside: RelationshipContains
// Point on edge: RelationshipIntersection
// Point on vertex: RelationshipIntersection
// Point outside: RelationshipDisjoint
}
func ExampleRectangle_RelationshipToRectangle() {
// Define two rectangles
r1 := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(0, 10),
})
r2 := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(5, 5),
geom2d.NewPoint(15, 5),
geom2d.NewPoint(15, 15),
geom2d.NewPoint(5, 15),
})
// Determine the relationship
relationship := r1.RelationshipToRectangle(r2, geom2d.WithEpsilon(1e-10))
// Output the result
fmt.Println("Relationship:", relationship)
// Output:
// Relationship: RelationshipIntersection
}
func ExampleRectangle_Scale() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(0, 0), // bottom-left
geom2d.NewPoint(10, 0), // bottom-right
})
// Scale the rectangle by a factor of 2 relative to the origin
scaled := rect.Scale(geom2d.NewPoint(0, 0), 2)
// Print the original and scaled rectangles
fmt.Println("Original Rectangle Contour:", rect.Contour())
fmt.Println("Scaled Rectangle Contour:", scaled.Contour())
// Output:
// Original Rectangle Contour: [Point[(0, 10)] Point[(10, 10)] Point[(10, 0)] Point[(0, 0)]]
// Scaled Rectangle Contour: [Point[(0, 20)] Point[(20, 20)] Point[(20, 0)] Point[(0, 0)]]
}
func ExampleRectangle_ScaleHeight() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(0, 0), // bottom-left
geom2d.NewPoint(10, 0), // bottom-right
})
// Scale the height of the rectangle by a factor of 1.5
scaled := rect.ScaleHeight(1.5)
// Print the original and scaled rectangles
fmt.Println("Original Rectangle Contour:", rect.Contour())
fmt.Println("Scaled Rectangle Contour:", scaled.Contour())
// Output:
// Original Rectangle Contour: [Point[(0, 10)] Point[(10, 10)] Point[(10, 0)] Point[(0, 0)]]
// Scaled Rectangle Contour: [Point[(0, 25)] Point[(10, 25)] Point[(10, 10)] Point[(0, 10)]]
}
func ExampleRectangle_ScaleWidth() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(0, 0), // bottom-left
geom2d.NewPoint(10, 0), // bottom-right
})
// Scale the width of the rectangle by a factor of 2.0
scaled := rect.ScaleWidth(2.0)
// Print the original and scaled rectangles
fmt.Println("Original Rectangle Contour:", rect.Contour())
fmt.Println("Scaled Rectangle Contour:", scaled.Contour())
// Output:
// Original Rectangle Contour: [Point[(0, 10)] Point[(10, 10)] Point[(10, 0)] Point[(0, 0)]]
// Scaled Rectangle Contour: [Point[(0, 10)] Point[(20, 10)] Point[(20, 0)] Point[(0, 0)]]
}
func ExampleRectangle_String() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
})
// Get the string representation
fmt.Println(rect.String())
// Output:
// Rectangle[(0, 0), (10, 0), (10, 10), (0, 10)]
}
func ExampleRectangle_Translate() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10), // top-left
geom2d.NewPoint(10, 10), // top-right
geom2d.NewPoint(0, 0), // bottom-left
geom2d.NewPoint(10, 0), // bottom-right
})
// Translate the rectangle by (5, -5)
translatedRect := rect.Translate(geom2d.NewPoint(5, -5))
// Print the translated rectangle
fmt.Println(translatedRect.String())
// Output:
// Rectangle[(5, -5), (15, -5), (15, 5), (5, 5)]
}
func ExampleRectangle_Width() {
// Create a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 10),
geom2d.NewPoint(20, 10),
geom2d.NewPoint(0, 0),
geom2d.NewPoint(20, 0),
})
// Calculate the width of the rectangle
width := rect.Width()
// Print the width
fmt.Printf("The width of the rectangle is: %d\n", width)
// Output:
// The width of the rectangle is: 20
}