-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinesegment_examples_test.go
602 lines (502 loc) · 18.7 KB
/
linesegment_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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package geom2d_test
import (
"fmt"
"github.com/mikenye/geom2d"
"math"
)
func ExampleNewLineSegment() {
segment := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(3, 4))
fmt.Println(segment.String())
// Output:
// LineSegment[(0, 0) -> (3, 4)]
}
func ExampleLineSegment_AddLineSegment() {
segment1 := geom2d.NewLineSegment(geom2d.NewPoint(1, 1), geom2d.NewPoint(4, 5))
segment2 := geom2d.NewLineSegment(geom2d.NewPoint(2, 3), geom2d.NewPoint(1, 2))
result := segment1.AddLineSegment(segment2)
fmt.Println(result)
// Output:
// LineSegment[(3, 4) -> (5, 7)]
}
func ExampleLineSegment_AsFloat64() {
// Create a LineSegment with integer coordinates
intSegment := geom2d.NewLineSegment(
geom2d.NewPoint(1, 2), // Start point
geom2d.NewPoint(3, 4), // End point
)
// Convert the LineSegment to float64
floatSegment := intSegment.AsFloat64()
// Print the converted LineSegment
fmt.Println("Integer LineSegment:", intSegment)
fmt.Println("Float64 LineSegment:", floatSegment)
// Output:
// Integer LineSegment: LineSegment[(1, 2) -> (3, 4)]
// Float64 LineSegment: LineSegment[(1, 2) -> (3, 4)]
}
func ExampleLineSegment_AsInt() {
// Create a LineSegment with floating-point coordinates
line := geom2d.NewLineSegment(
geom2d.NewPoint(1.5, 2.7),
geom2d.NewPoint(3.9, 4.2),
)
// Convert the LineSegment to integer coordinates
intLine := line.AsInt()
// Output the integer LineSegment
fmt.Println(intLine)
// Output:
// LineSegment[(1, 2) -> (3, 4)]
}
func ExampleLineSegment_AsIntRounded() {
// Create a LineSegment with floating-point coordinates
line := geom2d.NewLineSegment(
geom2d.NewPoint(1.5, 2.7),
geom2d.NewPoint(3.9, 4.2),
)
// Convert the LineSegment to integer coordinates with rounding
roundedIntLine := line.AsIntRounded()
// Output the rounded integer LineSegment
fmt.Println(roundedIntLine)
// Output:
// LineSegment[(2, 3) -> (4, 4)]
}
func ExampleLineSegment_BoundingBox() {
// Create a LineSegment
line := geom2d.NewLineSegment(
geom2d.NewPoint(3, 1),
geom2d.NewPoint(6, 4),
)
// Compute the bounding box of the LineSegment
boundingBox := line.BoundingBox()
// Output the bounding box
fmt.Println(boundingBox)
// Output:
// Rectangle[(3, 1), (6, 1), (6, 4), (3, 4)]
}
func ExampleLineSegment_Bresenham() {
// Define a line segment
line := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(5, 3))
// Print the points generated by Bresenham's algorithm
line.Bresenham(func(p geom2d.Point[int]) bool {
fmt.Println(p)
return true
})
// Output:
// Point[(0, 0)]
// Point[(1, 1)]
// Point[(2, 1)]
// Point[(3, 2)]
// Point[(4, 2)]
// Point[(5, 3)]
}
func ExampleLineSegment_Center() {
lineSegment := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 10))
center := lineSegment.Center()
fmt.Printf("Center: %v\n", center)
// Output:
// Center: Point[(5, 5)]
}
func ExampleLineSegment_ContainsPoint() {
lineSegment := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 10))
pointOnSegment := geom2d.NewPoint(5, 5)
pointOffSegment := geom2d.NewPoint(7, 8)
fmt.Printf("Point on segment: %v\n", lineSegment.ContainsPoint(pointOnSegment))
fmt.Printf("Point off segment: %v\n", lineSegment.ContainsPoint(pointOffSegment))
// Output:
// Point on segment: true
// Point off segment: false
}
func ExampleLineSegment_DistanceToLineSegment() {
segmentAB := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(2, 2))
segmentCD := geom2d.NewLineSegment(geom2d.NewPoint(3, 3), geom2d.NewPoint(5, 5))
// Default behavior (no epsilon adjustment)
distance := segmentAB.DistanceToLineSegment(segmentCD)
distanceRounded := math.Round(distance*1000) / 1000
fmt.Println(distanceRounded)
// Output:
// 1.414
}
func ExampleLineSegment_DistanceToPoint() {
lineSegment := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 0))
point := geom2d.NewPoint(5, 5)
distance := lineSegment.DistanceToPoint(point)
fmt.Printf("Distance from point to line segment: %.2f\n", distance)
// Output:
// Distance from point to line segment: 5.00
}
func ExampleLineSegment_End() {
lineSegment := geom2d.NewLineSegment(geom2d.NewPoint(1, 2), geom2d.NewPoint(3, 4))
fmt.Println(lineSegment.End())
// Output:
// Point[(3, 4)]
}
func ExampleLineSegment_Eq() {
segment1 := geom2d.NewLineSegment(geom2d.NewPoint(1.0, 1.0), geom2d.NewPoint(4.0, 5.0))
segment2 := geom2d.NewLineSegment(geom2d.NewPoint(1.0, 1.0), geom2d.NewPoint(4.0, 5.0))
fmt.Println(segment1.Eq(segment2))
// Output:
// true
}
func ExampleLineSegment_Eq_epsilon() {
// Approximate equality with epsilon
segment1 := geom2d.NewLineSegment(geom2d.NewPoint(1.0, 1.0), geom2d.NewPoint(4.0, 5.0))
segment3 := geom2d.NewLineSegment(geom2d.NewPoint(1.00001, 1.00001), geom2d.NewPoint(4.00001, 5.00001))
fmt.Println(segment1.Eq(segment3, geom2d.WithEpsilon(1e-4)))
// Output:
// true
}
func ExampleLineSegment_IntersectsLineSegment() {
// Define two line segments
line1 := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 10)) // Line segment from (0, 0) to (10, 10)
line2 := geom2d.NewLineSegment(geom2d.NewPoint(5, 5), geom2d.NewPoint(15, 15)) // Line segment from (5, 5) to (15, 15)
line3 := geom2d.NewLineSegment(geom2d.NewPoint(20, 20), geom2d.NewPoint(30, 30)) // Line segment far from line1 and line2
// Check for intersection
fmt.Printf("Line1 intersects Line2: %v\n", line1.IntersectsLineSegment(line2)) // Expected: true
fmt.Printf("Line1 intersects Line3: %v\n", line1.IntersectsLineSegment(line3)) // Expected: false
// Overlapping case
line4 := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(5, 5)) // Line segment overlaps partially with Line1
fmt.Printf("Line1 intersects Line4: %v\n", line1.IntersectsLineSegment(line4)) // Expected: true
// Endpoint touching case
line5 := geom2d.NewLineSegment(geom2d.NewPoint(10, 10), geom2d.NewPoint(20, 20)) // Shares an endpoint with Line1
fmt.Printf("Line1 intersects Line5: %v\n", line1.IntersectsLineSegment(line5)) // Expected: true
// Output:
// Line1 intersects Line2: true
// Line1 intersects Line3: false
// Line1 intersects Line4: true
// Line1 intersects Line5: true
}
// todo: update example below
//func ExampleLineSegment_IntersectionPoint() {
// // Define two line segments
// AB := geom2d.NewLineSegment(geom2d.NewPoint[float64](0, 0), geom2d.NewPoint[float64](4, 4))
// CD := geom2d.NewLineSegment(geom2d.NewPoint[float64](0, 4), geom2d.NewPoint[float64](4, 0))
//
// // Find the intersection point
// intersection, exists := AB.IntersectionPoint(CD)
//
// // Print the result
// if exists {
// fmt.Printf("Intersection point: (%.2f, %.2f)\n", intersection.X(), intersection.Y())
// } else {
// fmt.Println("No intersection point exists.")
// }
//
// // Output:
// // Intersection point: (2.00, 2.00)
//}
func ExampleLineSegment_Length() {
segment := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(3, 4))
fmt.Println(segment.Length())
// Output:
// 5
}
func ExampleLineSegment_Normalize() {
// Create a line segment where the start point is not leftmost-lowest
original := geom2d.NewLineSegment(geom2d.NewPoint(3, 5), geom2d.NewPoint(1, 2))
// Normalize the segment
normalized := original.Normalize()
// Print the result
fmt.Printf("Original Line Segment: %s\n", original.String())
fmt.Printf("Normalized Line Segment: %s\n", normalized.String())
// Output:
// Original Line Segment: LineSegment[(3, 5) -> (1, 2)]
// Normalized Line Segment: LineSegment[(1, 2) -> (3, 5)]
}
func ExampleLineSegment_Points() {
// Create a line segment with two endpoints
line := geom2d.NewLineSegment(geom2d.NewPoint(1, 2), geom2d.NewPoint(3, 4))
// Get the points as a slice
points := line.Points()
// Output the points
fmt.Printf("Start Point: %v\n", points[0])
fmt.Printf("End Point: %v\n", points[1])
// Output:
// Start Point: Point[(1, 2)]
// End Point: Point[(3, 4)]
}
func ExampleLineSegment_Reflect() {
// Create a line segment
line := geom2d.NewLineSegment(
geom2d.NewPoint[float64](1, 2),
geom2d.NewPoint[float64](3, 4),
)
// Reflect across the X-axis
reflectedX := line.Reflect(geom2d.ReflectAcrossXAxis)
// Reflect across the Y-axis
reflectedY := line.Reflect(geom2d.ReflectAcrossYAxis)
// Reflect across a custom line (e.g., y = x, represented as LineSegment[(0, 0), (1, 1)])
customLine := geom2d.NewLineSegment(
geom2d.NewPoint[float64](0, 0),
geom2d.NewPoint[float64](1, 1),
)
reflectedCustom := line.Reflect(geom2d.ReflectAcrossCustomLine, customLine)
// Output the results
fmt.Printf("Original Line: %v\n", line)
fmt.Printf("Reflected across X-axis: %v\n", reflectedX)
fmt.Printf("Reflected across Y-axis: %v\n", reflectedY)
fmt.Printf("Reflected across custom line (y = x): %v\n", reflectedCustom)
// Output:
// Original Line: LineSegment[(1, 2) -> (3, 4)]
// Reflected across X-axis: LineSegment[(1, -2) -> (3, -4)]
// Reflected across Y-axis: LineSegment[(-1, 2) -> (-3, 4)]
// Reflected across custom line (y = x): LineSegment[(2, 1) -> (4, 3)]
}
func ExampleLineSegment_RelationshipToCircle() {
// Define a circle with center (5, 5) and radius 5
circle := geom2d.NewCircle(geom2d.NewPoint(5, 5), 5.0)
// Define various line segments
lineDisjoint := geom2d.NewLineSegment(
geom2d.NewPoint(0, 0),
geom2d.NewPoint(-2, -2),
)
lineIntersecting := geom2d.NewLineSegment(
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 10),
)
lineContained := geom2d.NewLineSegment(
geom2d.NewPoint(5, 6),
geom2d.NewPoint(5, 4),
)
// Evaluate relationships
fmt.Println("Disjoint:", lineDisjoint.RelationshipToCircle(circle))
fmt.Println("Intersecting:", lineIntersecting.RelationshipToCircle(circle))
fmt.Println("Contained:", lineContained.RelationshipToCircle(circle))
// Output:
// Disjoint: RelationshipDisjoint
// Intersecting: RelationshipIntersection
// Contained: RelationshipContainedBy
}
func ExampleLineSegment_RelationshipToLineSegment() {
// Define two line segments
line1 := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 10))
line2 := geom2d.NewLineSegment(geom2d.NewPoint(5, 5), geom2d.NewPoint(15, 15))
line3 := geom2d.NewLineSegment(geom2d.NewPoint(20, 20), geom2d.NewPoint(30, 30))
line4 := geom2d.NewLineSegment(geom2d.NewPoint(0, 0), geom2d.NewPoint(10, 10))
// Evaluate relationships
fmt.Println("Line1 vs Line2:", line1.RelationshipToLineSegment(line2))
fmt.Println("Line1 vs Line3:", line1.RelationshipToLineSegment(line3))
fmt.Println("Line1 vs Line4:", line1.RelationshipToLineSegment(line4))
// Output:
// Line1 vs Line2: RelationshipIntersection
// Line1 vs Line3: RelationshipDisjoint
// Line1 vs Line4: RelationshipEqual
}
func ExampleLineSegment_RelationshipToPolyTree() {
// Define a PolyTree with a root polygon and a hole
root, _ := geom2d.NewPolyTree([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(0, 10),
}, geom2d.PTSolid)
hole, _ := geom2d.NewPolyTree([]geom2d.Point[int]{
geom2d.NewPoint(4, 4),
geom2d.NewPoint(6, 4),
geom2d.NewPoint(6, 6),
geom2d.NewPoint(4, 6),
}, geom2d.PTHole)
_ = root.AddChild(hole)
// Note: While errors are ignored in this example for simplicity, it is important to handle errors properly in
// production code to ensure robustness and reliability.
// Define a line segment
segment := geom2d.NewLineSegment(
geom2d.NewPoint(2, 2),
geom2d.NewPoint(8, 8),
)
// Evaluate relationships
relationships := segment.RelationshipToPolyTree(root)
// Print results
fmt.Printf("Root polygon relationship: %v\n", relationships[root])
fmt.Printf("Hole polygon relationship: %v\n", relationships[hole])
// Output:
// Root polygon relationship: RelationshipContainedBy
// Hole polygon relationship: RelationshipIntersection
}
func ExampleLineSegment_RelationshipToPoint() {
// Define a line segment
segment := geom2d.NewLineSegment(
geom2d.NewPoint[float64](0, 0),
geom2d.NewPoint[float64](10, 10),
)
// Define some points
point1 := geom2d.NewPoint[float64](5, 5) // On the segment
point2 := geom2d.NewPoint[float64](10, 0) // Disjoint
point3 := geom2d.NewPoint[float64](0, 0) // Coincides with an endpoint
// Evaluate relationships
fmt.Println("Point1 vs Line Segment:", segment.RelationshipToPoint(point1))
fmt.Println("Point2 vs Line Segment:", segment.RelationshipToPoint(point2))
fmt.Println("Point3 vs Line Segment:", segment.RelationshipToPoint(point3))
// Output:
// Point1 vs Line Segment: RelationshipIntersection
// Point2 vs Line Segment: RelationshipDisjoint
// Point3 vs Line Segment: RelationshipIntersection
}
func ExampleLineSegment_RelationshipToRectangle() {
// Define a rectangle
rect := geom2d.NewRectangle([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(10, 0),
geom2d.NewPoint(10, 10),
geom2d.NewPoint(0, 10),
})
// Define some line segments
line1 := geom2d.NewLineSegment(geom2d.NewPoint(5, 5), geom2d.NewPoint(15, 15)) // Intersects
line2 := geom2d.NewLineSegment(geom2d.NewPoint(1, 1), geom2d.NewPoint(9, 9)) // Contained
line3 := geom2d.NewLineSegment(geom2d.NewPoint(20, 20), geom2d.NewPoint(30, 30)) // Disjoint
// Evaluate relationships
fmt.Println("Line1 vs Rectangle:", line1.RelationshipToRectangle(rect))
fmt.Println("Line2 vs Rectangle:", line2.RelationshipToRectangle(rect))
fmt.Println("Line3 vs Rectangle:", line3.RelationshipToRectangle(rect))
// Output:
// Line1 vs Rectangle: RelationshipIntersection
// Line2 vs Rectangle: RelationshipContainedBy
// Line3 vs Rectangle: RelationshipDisjoint
}
func ExampleLineSegment_Rotate() {
// Define a line segment from (2, 3) to (4, 6)
line := geom2d.NewLineSegment(
geom2d.NewPoint[int](2, 3),
geom2d.NewPoint[int](4, 6),
)
// Define a pivot point at the origin (0, 0)
pivot := geom2d.NewPoint[int](0, 0)
// Rotate the line segment by 90 degrees (π/2 radians) counterclockwise
rotatedLine := line.Rotate(pivot, math.Pi/2)
// Print the rotated line segment's start and end points
fmt.Printf("Rotated Line Start: %v\n", rotatedLine.Points()[0])
fmt.Printf("Rotated Line End: %v\n", rotatedLine.Points()[1])
// Output:
// Rotated Line Start: Point[(-3, 2)]
// Rotated Line End: Point[(-6, 4)]
}
// ExampleLineSegment_RoundToEpsilon demonstrates the use of the RoundToEpsilon method
// to round the coordinates of a LineSegment to the nearest multiple of the given epsilon.
func ExampleLineSegment_RoundToEpsilon() {
// Create a line segment
ls := geom2d.NewLineSegment(geom2d.NewPoint[float64](1.2345, 4.5678), geom2d.NewPoint[float64](7.8912, 3.2109))
// Round the coordinates to the nearest 0.1
rounded := ls.RoundToEpsilon(0.1)
// Print the rounded line segment
fmt.Printf("LineSegment[(%.4f, %.4f) -> (%.4f, %.4f)]",
rounded.Start().X(),
rounded.Start().Y(),
rounded.End().X(),
rounded.End().Y(),
)
// Output: LineSegment[(1.2000, 4.6000) -> (7.9000, 3.2000)]
}
func ExampleLineSegment_Scale() {
// Define a line segment from (2, 3) to (4, 6)
line := geom2d.NewLineSegment(
geom2d.NewPoint[int](2, 3),
geom2d.NewPoint[int](4, 6),
)
// Define a reference point for scaling
ref := geom2d.NewPoint[int](0, 0)
// Scale the line segment by a factor of 2 relative to the origin (0, 0)
scaledLine := line.Scale(ref, 2)
// Print the scaled line segment's start and end points
fmt.Printf("Scaled Line Start: %v\n", scaledLine.Points()[0])
fmt.Printf("Scaled Line End: %v\n", scaledLine.Points()[1])
// Scale the line segment by a shrinking factor of 0.5, converting to floating-point type
lineFloat := line.AsFloat64()
shrunkLine := lineFloat.Scale(ref.AsFloat64(), 0.5)
// Print the shrunk line segment's start and end points
fmt.Printf("Shrunk Line Start: %v\n", shrunkLine.Points()[0])
fmt.Printf("Shrunk Line End: %v\n", shrunkLine.Points()[1])
// Scale the line segment relative to a custom point (3, 3)
customRef := geom2d.NewPoint[int](3, 3)
customScaledLine := line.Scale(customRef, 2)
// Print the line segment scaled relative to the custom reference point
fmt.Printf("Custom Scaled Line Start: %v\n", customScaledLine.Points()[0])
fmt.Printf("Custom Scaled Line End: %v\n", customScaledLine.Points()[1])
// Output:
// Scaled Line Start: Point[(4, 6)]
// Scaled Line End: Point[(8, 12)]
// Shrunk Line Start: Point[(1, 1.5)]
// Shrunk Line End: Point[(2, 3)]
// Custom Scaled Line Start: Point[(1, 3)]
// Custom Scaled Line End: Point[(5, 9)]
}
// ExampleLineSegment_Slope demonstrates the use of the Slope method
// to calculate the slope of a line segment.
func ExampleLineSegment_Slope() {
// Create a line segment
ls := geom2d.NewLineSegment(geom2d.NewPoint[float64](1, 1), geom2d.NewPoint[float64](3, 5))
// Calculate the slope
slope, ok := ls.Slope()
// Print the slope and whether it is valid
fmt.Printf("Slope: %.6f, Valid: %t\n", slope, ok)
// Output: Slope: 2.000000, Valid: true
}
func ExampleLineSegment_Start() {
lineSegment := geom2d.NewLineSegment(geom2d.NewPoint(1, 2), geom2d.NewPoint(3, 4))
fmt.Println(lineSegment.Start())
// Output:
// Point[(1, 2)]
}
func ExampleLineSegment_String() {
segment := geom2d.NewLineSegment(geom2d.NewPoint(1, 1), geom2d.NewPoint(4, 5))
fmt.Println(segment.String())
// Output:
// LineSegment[(1, 1) -> (4, 5)]
}
func ExampleLineSegment_SubLineSegment() {
// Define two line segments
AB := geom2d.NewLineSegment(
geom2d.NewPoint[int](10, 10),
geom2d.NewPoint[int](20, 20),
)
CD := geom2d.NewLineSegment(
geom2d.NewPoint[int](5, 5),
geom2d.NewPoint[int](15, 15),
)
// Subtract CD from AB
result := AB.SubLineSegment(CD)
// Print the resulting line segment's start and end points
fmt.Printf("Resulting Line Start: %v\n", result.Points()[0])
fmt.Printf("Resulting Line End: %v\n", result.Points()[1])
// Output:
// Resulting Line Start: Point[(5, 5)]
// Resulting Line End: Point[(5, 5)]
}
func ExampleLineSegment_Translate() {
// Define a line segment AB
AB := geom2d.NewLineSegment(
geom2d.NewPoint(1, 1),
geom2d.NewPoint(4, 4),
)
// Define the translation vector
delta := geom2d.NewPoint(2, 3)
// Translate the line segment by the vector
translatedAB := AB.Translate(delta)
// Output the translated line segment
fmt.Println("Translated Line Segment:")
fmt.Println("Start Point:", translatedAB.Start())
fmt.Println("End Point:", translatedAB.End())
// Output:
// Translated Line Segment:
// Start Point: Point[(3, 4)]
// End Point: Point[(6, 7)]
}
func ExampleLineSegment_XAtY() {
segment := geom2d.NewLineSegment(geom2d.NewPoint(2, 2), geom2d.NewPoint(8, 5))
x, ok := segment.XAtY(3) // Y = 3
if ok {
fmt.Printf("X at Y=3 is %.2f\n", x)
} else {
fmt.Println("Y=3 is out of bounds for the segment")
}
// Output:
// X at Y=3 is 4.00
}
func ExampleLineSegment_YAtX() {
segment := geom2d.NewLineSegment(geom2d.NewPoint(2, 2), geom2d.NewPoint(8, 5))
y, ok := segment.YAtX(4) // X = 4
if ok {
fmt.Printf("Y at X=4 is %.2f\n", y)
} else {
fmt.Println("X=4 is out of bounds for the segment")
}
// Output:
// Y at X=4 is 3.00
}