-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle_examples_test.go
276 lines (238 loc) · 7.97 KB
/
circle_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
package geom2d_test
import (
"fmt"
"github.com/mikenye/geom2d"
"math"
)
func ExampleNewCircle() {
center := geom2d.NewPoint(3, 4) // Note type inference as int not explicitly specified
radius := 5
circle := geom2d.NewCircle(center, radius) // Creates a Circle with center (3, 4) and radius 5
fmt.Println(circle)
// Output:
// Circle[center=(3, 4), radius=5]
}
func ExampleCircle_Area() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5) // Creates a Circle with center (3, 4) and radius 5
area := circle.Area() // area = π*5*5 = 25π
areaRounded := math.Round(area*100) / 100 // Round to two digits
fmt.Println(areaRounded)
// Output:
// 78.54
}
func ExampleCircle_AsFloat64() {
intCircle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
fltCircle := intCircle.AsFloat64()
fmt.Printf("intCircle is %v of type: %T\n", intCircle, intCircle)
fmt.Printf("fltCircle is %v of type: %T\n", fltCircle, fltCircle)
// Output:
// intCircle is Circle[center=(3, 4), radius=5] of type: geom2d.Circle[int]
// fltCircle is Circle[center=(3, 4), radius=5] of type: geom2d.Circle[float64]
}
func ExampleCircle_AsInt() {
fltCircle := geom2d.NewCircle(geom2d.NewPoint(3.7, 4.9), 5.6)
intCircle := fltCircle.AsInt()
fmt.Printf("fltCircle is %v of type: %T\n", fltCircle, fltCircle)
fmt.Printf("intCircle is %v of type: %T\n", intCircle, intCircle)
// Output:
// fltCircle is Circle[center=(3.7, 4.9), radius=5.6] of type: geom2d.Circle[float64]
// intCircle is Circle[center=(3, 4), radius=5] of type: geom2d.Circle[int]
}
func ExampleCircle_AsIntRounded() {
fltCircle := geom2d.NewCircle(geom2d.NewPoint(3.7, 4.2), 5.6)
intCircle := fltCircle.AsIntRounded()
fmt.Printf("fltCircle is %v of type: %T\n", fltCircle, fltCircle)
fmt.Printf("intCircle is %v of type: %T\n", intCircle, intCircle)
// Output:
// fltCircle is Circle[center=(3.7, 4.2), radius=5.6] of type: geom2d.Circle[float64]
// intCircle is Circle[center=(4, 4), radius=6] of type: geom2d.Circle[int]
}
func ExampleCircle_BoundingBox() {
// Create a circle centered at (10, 10) with a radius of 5
circle := geom2d.NewCircle(geom2d.NewPoint(10, 10), 5)
// Calculate its bounding box
boundingBox := circle.BoundingBox()
// Output the bounding box
fmt.Println("Bounding box:", boundingBox)
// Output:
// Bounding box: Rectangle[(5, 5), (15, 5), (15, 15), (5, 15)]
}
func ExampleCircle_Bresenham() {
// Define a circle with center (5, 5) and radius 3.
c := geom2d.NewCircle(geom2d.NewPoint(5, 5), 3)
// Generate the points on the circle's perimeter.
c.Bresenham(func(p geom2d.Point[int]) bool {
fmt.Println(p)
return true // Continue generating points
})
// Output:
// Point[(5, 8)]
// Point[(5, 8)]
// Point[(5, 2)]
// Point[(5, 2)]
// Point[(8, 5)]
// Point[(2, 5)]
// Point[(8, 5)]
// Point[(2, 5)]
// Point[(6, 8)]
// Point[(4, 8)]
// Point[(6, 2)]
// Point[(4, 2)]
// Point[(8, 6)]
// Point[(2, 6)]
// Point[(8, 4)]
// Point[(2, 4)]
// Point[(7, 7)]
// Point[(3, 7)]
// Point[(7, 3)]
// Point[(3, 3)]
// Point[(7, 7)]
// Point[(3, 7)]
// Point[(7, 3)]
// Point[(3, 3)]
}
func ExampleCircle_Center() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
center := circle.Center()
fmt.Println(center)
// Output:
// Point[(3, 4)]
}
func ExampleCircle_Circumference() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
circumference := circle.Circumference()
circumferenceRounded := math.Round(circumference*100) / 100
fmt.Println(circumferenceRounded)
// Output:
// 31.42
}
func ExampleCircle_Eq() {
c1 := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
c2 := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
isEqual := c1.Eq(c2)
fmt.Println(isEqual)
// Output:
// true
}
func ExampleCircle_Eq_epsilon() {
c1 := geom2d.NewCircle(geom2d.NewPoint[float64](3, 4), 5)
c2 := geom2d.NewCircle(geom2d.NewPoint(3.0001, 4.0001), 5.0001)
isApproximatelyEqual := c1.Eq(c2, geom2d.WithEpsilon(1e-3))
fmt.Println(isApproximatelyEqual)
// Output:
// true
}
func ExampleCircle_Radius() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
radius := circle.Radius()
fmt.Println(radius)
// Output:
// 5
}
func ExampleCircle_RelationshipToCircle() {
circle1 := geom2d.NewCircle(geom2d.NewPoint(0, 0), 10)
circle2 := geom2d.NewCircle(geom2d.NewPoint(0, 0), 10)
circle3 := geom2d.NewCircle(geom2d.NewPoint(3, 0), 5)
circle4 := geom2d.NewCircle(geom2d.NewPoint(20, 0), 5)
fmt.Println(circle1.RelationshipToCircle(circle2))
fmt.Println(circle1.RelationshipToCircle(circle3))
fmt.Println(circle3.RelationshipToCircle(circle1))
fmt.Println(circle1.RelationshipToCircle(circle4))
// Output:
// RelationshipEqual
// RelationshipContains
// RelationshipContainedBy
// RelationshipDisjoint
}
func ExampleCircle_RelationshipToLineSegment() {
circle := geom2d.NewCircle(geom2d.NewPoint(0, 0), 10)
line1 := geom2d.NewLineSegment(geom2d.NewPoint(15, 0), geom2d.NewPoint(20, 0)) // Outside
line2 := geom2d.NewLineSegment(geom2d.NewPoint(0, -15), geom2d.NewPoint(0, 15)) // Intersects
line3 := geom2d.NewLineSegment(geom2d.NewPoint(5, 5), geom2d.NewPoint(-5, -5)) // Fully contained
fmt.Println(circle.RelationshipToLineSegment(line1))
fmt.Println(circle.RelationshipToLineSegment(line2))
fmt.Println(circle.RelationshipToLineSegment(line3))
// Output:
// RelationshipDisjoint
// RelationshipIntersection
// RelationshipContains
}
func ExampleCircle_RelationshipToPoint() {
circle := geom2d.NewCircle(geom2d.NewPoint(0, 0), 10)
point1 := geom2d.NewPoint(15, 0) // Outside the circle
point2 := geom2d.NewPoint(10, 0) // On the circle boundary
point3 := geom2d.NewPoint(5, 0) // Inside the circle
fmt.Println(circle.RelationshipToPoint(point1))
fmt.Println(circle.RelationshipToPoint(point2))
fmt.Println(circle.RelationshipToPoint(point3))
// Output:
// RelationshipDisjoint
// RelationshipIntersection
// RelationshipContains
}
func ExampleCircle_RelationshipToPolyTree() {
// Create a polygon as a PolyTree
root, _ := geom2d.NewPolyTree([]geom2d.Point[int]{
geom2d.NewPoint(0, 0),
geom2d.NewPoint(0, 100),
geom2d.NewPoint(100, 100),
geom2d.NewPoint(100, 0),
}, geom2d.PTSolid)
// 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.
// Create a circle
circle := geom2d.NewCircle(geom2d.NewPoint(50, 50), 10)
// Check relationships
relationships := circle.RelationshipToPolyTree(root)
// Output the relationship
fmt.Printf("Circle's relationship to root polygon is: %v\n", relationships[root])
// Output:
// Circle's relationship to root polygon is: RelationshipContainedBy
}
func ExampleCircle_RelationshipToRectangle() {
// 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 a circle
circle := geom2d.NewCircle(geom2d.NewPoint(50, 50), 30)
// Determine the relationship
rel := circle.RelationshipToRectangle(rect)
// Print the result
fmt.Println("Relationship:", rel)
// Output:
// Relationship: RelationshipContainedBy
}
func ExampleCircle_Rotate() {
pivot := geom2d.NewPoint(1, 1)
circle := geom2d.NewCircle(geom2d.NewPoint(3, 3), 5)
angle := math.Pi / 2 // Rotate 90 degrees
rotated := circle.Rotate(pivot, angle, geom2d.WithEpsilon(1e-10)).AsInt()
fmt.Println(rotated)
// Output:
// Circle[center=(-1, 3), radius=5]
}
func ExampleCircle_Scale() {
circle := geom2d.NewCircle(geom2d.NewPoint(0, 0), 5)
scaled := circle.Scale(2).AsInt()
fmt.Println(scaled)
// Output:
// Circle[center=(0, 0), radius=10]
}
func ExampleCircle_String() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
fmt.Println(circle.String())
// Output:
// Circle[center=(3, 4), radius=5]
}
func ExampleCircle_Translate() {
circle := geom2d.NewCircle(geom2d.NewPoint(3, 4), 5)
translationVector := geom2d.NewPoint(2, 3)
translatedCircle := circle.Translate(translationVector)
fmt.Println(translatedCircle)
// Output:
// Circle[center=(5, 7), radius=5]
}