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
//! Basic 2D geometric operations.
//!
//! This library provides core 2D geometric primitives and operations including:
//! - **Primitives:** Points, Segments, Circles, Arcs, Polylines, Intervals
//! - **Distance Calculations:** Between points, segments, circles, and arcs
//! - **Intersection Detection:** Line-line, circle-circle, arc-arc, and mixed geometric types
//! - **Geometric Algorithms:** Convex hull, area calculation, bounding shapes
//!
//! > **Important:** All arcs in this library are **CCW (counter-clockwise)** oriented.
//!
//! # Examples
//!
//! ## Distance and intersection calculations
//!
//! ```
//! use togo::prelude::*;
//!
//! // Create primitives
//! let p = point(10.0, 0.0);
//! let c = circle(point(0.0, 0.0), 5.0);
//! let seg = segment(point(0.0, 0.0), point(5.0, 0.0));
//! let a1 = arc(point(1.0, 0.0), point(0.0, 1.0), point(0.0, 0.0), 1.0);
//!
//! // Compute distances
//! let (dist_to_circle, _, _) = dist_point_circle(&p, &c);
//! assert_eq!(dist_to_circle, 5.0);
//!
//! let (dist_to_segment, _) = dist_point_segment(&point(5.0, 5.0), &seg);
//! assert_eq!(dist_to_segment, 5.0);
//!
//! // Distance between arcs
//! let a2 = arc(point(4.0, 0.0), point(2.0, 0.0), point(3.0, 0.0), 1.0);
//! let dist = dist_arc_arc(&a1, &a2);
//! assert!(dist > 0.0);
//! ```
//!
//! ## Intersection tests
//!
//! ```
//! use togo::prelude::*;
//!
//! // Segment intersection
//! let seg1 = segment(point(0.0, 0.0), point(2.0, 2.0));
//! let seg2 = segment(point(0.0, 2.0), point(2.0, 0.0));
//! match int_segment_segment(&seg1, &seg2) {
//! SegmentSegmentConfig::OnePoint(pt, ..) => {
//! assert!(point(1.0, 1.0).close_enough(pt, 1e-10));
//! },
//! _ => assert!(false, "Expected segment intersection"),
//! }
//!
//! // Circle intersection
//! let c1 = circle(point(0.0, 0.0), 3.0);
//! let c2 = circle(point(4.0, 0.0), 3.0);
//! match int_circle_circle(c1, c2) {
//! CircleCircleConfig::NoncocircularTwoPoints(_, _) => {
//! assert!(true); // Two points found
//! },
//! _ => assert!(false, "Expected two intersection points"),
//! }
//! ```
//!
//! ## Geometric algorithms
//!
//! ```
//! use togo::prelude::*;
//! use togo::algo::{pointline_area, points_convex_hull, arc_bounding_circle};
//!
//! // Polygon area
//! let triangle = vec![
//! point(0.0, 0.0),
//! point(4.0, 0.0),
//! point(2.0, 3.0),
//! point(0.0, 0.0),
//! ];
//! let area = pointline_area(&triangle);
//! assert_eq!(area, 6.0);
//!
//! // Convex hull computation
//! let points = vec![
//! point(0.0, 0.0), point(2.0, 1.0), point(1.0, 2.0),
//! point(3.0, 0.0), point(2.0, 3.0), point(0.0, 2.0),
//! ];
//! let hull = points_convex_hull(&points);
//! assert_eq!(hull.len(), 4); // 4 points on convex hull
//!
//! // Bounding shape for an arc
//! let quarter_arc = arc(point(1.0, 0.0), point(0.0, 1.0), point(0.0, 0.0), 1.0);
//! let bounding = arc_bounding_circle(&quarter_arc);
//! assert_eq!(bounding.r, 0.7071067811865476); // sqrt(2)/2
//! ```
//!
//! ## Distance computations
//!
//! ```
//! use togo::prelude::*;
//! let l = line(point(0.0, 3.0), point(1.0, 0.0)); // Line with point and direction
//! let c = circle(point(0.0, 0.0), 2.0);
//! let result = dist_line_circle(&l, &c);
//! match result {
//! DistLineCircleConfig::OnePair(dist, _param, _line_pt, _circle_pt) => {
//! assert_eq!(1.0, dist);
//! }
//! _ => assert!(false),
//! }
//!
//! // Distance from point to arc
//! let p = point(2.0, 0.0);
//! let a = arc(point(0.0, 1.0), point(1.0, 0.0), point(0.0, 0.0), 1.0);
//! match dist_point_arc(&p, &a) {
//! DistPointArcConfig::OnePoint(dist, _) => {
//! assert_eq!(1.0, dist);
//! }
//! _ => assert!(false),
//! }
//!
//! // Distance from segment to arc
//! let seg = segment(point(3.0, 0.0), point(4.0, 0.0));
//! let a = arc(point(0.0, 1.0), point(1.0, 0.0), point(0.0, 0.0), 1.0);
//! let dist = dist_segment_arc(&seg, &a);
//! assert_eq!(2.0, dist);
//! ```
//!
//! ```
//! use togo::prelude::*;
//! // Distance from segment to circle
//! let seg = segment(point(3.0, 0.0), point(4.0, 0.0));
//! let c = circle(point(0.0, 0.0), 1.0);
//! let result = dist_segment_circle(&seg, &c);
//! // Function returns DistSegmentCircleConfig enum
//! match result {
//! DistSegmentCircleConfig::OnePoint(dist, closest) => {
//! assert_eq!(2.0, dist); // Distance should be non-negative
//! }
//! _ => assert!(false),
//! }
//!
//! // Distance between two segments
//! let seg1 = segment(point(0.0, 0.0), point(1.0, 0.0));
//! let seg2 = segment(point(0.0, 2.0), point(1.0, 2.0));
//! let dist = dist_segment_segment(&seg1, &seg2);
//! assert_eq!(dist, 2.0); // Parallel segments 2 units apart
//!
//! ```
//!
//! ## Intersection computations
//!
//! ```
//! use togo::prelude::*;
//!
//! // Interval-interval intersection
//! let iv1 = interval(1.0, 5.0);
//! let iv2 = interval(3.0, 7.0);
//! let result = int_interval_interval(iv1, iv2);
//! match result {
//! IntervalConfig::Overlap(start, end) => {
//! // Intervals overlap from 3.0 to 5.0
//! assert_eq!(start, 3.0);
//! assert_eq!(end, 5.0);
//! },
//! _ => assert!(false),
//! }
//!
//! // Line-line intersection
//! let l1 = line(point(0.0, 0.0), point(1.0, 0.0)); // Line with origin and direction
//! let l2 = line(point(0.0, 0.0), point(0.0, 1.0)); // Line with origin and direction
//! let result = int_line_line(&l1, &l2);
//! match result {
//! LineLineConfig::OnePoint(pt, _param1, _param2) => {
//! // Lines intersect at origin
//! assert_eq!(point(0.0, 0.0), pt);
//! },
//! _ => assert!(false),
//! }
//! ```
// Core geometric primitives
// Centralized constants for numeric stability
// Geometric algorithms and utilities
// Distance computation modules
// Intersection computation modules
// Bézier curve support (experimental)
// Visualization and debugging