Loading...
Searching...
No Matches
geometry_utils_example.dart
Go to the documentation of this file.
1import 'dart:async';
2import 'dart:io';
3import 'dart:math';
4import 'package:navigine_sdk/navigine_sdk.dart';
5
10class GeometryUtilsExample {
11 GeometryUtilsExample() {
12 _demonstrateGeometryUtilsMethods();
13 }
14
18 void _demonstrateGeometryUtilsMethods() {
19 print("=== GeometryUtils Methods ===");
20
21 _demonstratePointOperations();
22 _demonstrateGlobalPointOperations();
23 _demonstrateBoundingBoxOperations();
24 _demonstrateSegmentOperations();
25 _demonstratePolygonOperations();
26 _demonstrateLineOperations();
27 _demonstrateAdvancedGeometryOperations();
28 }
29
33 void _demonstratePointOperations() {
34 print("--- Point Operations ---");
35
36 // [dart_Point_constructor]
37 // Create points with x, y coordinates
38 Point point1 = Point(10.0, 20.0);
39 Point point2 = Point(30.0, 40.0);
40 Point point3 = Point(50.0, 60.0);
41 print("Created points: P1(${point1.x}, ${point1.y}), P2(${point2.x}, ${point2.y}), P3(${point3.x}, ${point3.y})");
42 // [dart_Point_constructor]
43
44 // [dart_Point_getX]
45 // Get X coordinate
46 double x1 = point1.x;
47 print("Point1 X coordinate: $x1");
48 // [dart_Point_getX]
49
50 // [dart_Point_getY]
51 // Get Y coordinate
52 double y1 = point1.y;
53 print("Point1 Y coordinate: $y1");
54 // [dart_Point_getY]
55
56 // [dart_GeometryUtils_distanceBetweenPoints]
57 // Calculate distance between points
58 double distance = GeometryUtils.distanceBetweenPoints(point1, point2);
59 print("Distance between P1 and P2: ${distance.toStringAsFixed(2)} meters");
60 // [dart_GeometryUtils_distanceBetweenPoints]
61
62 // Create more points for testing
63 List<Point> testPoints = [
64 Point(0.0, 0.0),
65 Point(10.0, 0.0),
66 Point(10.0, 10.0),
67 Point(0.0, 10.0),
68 Point(5.0, 5.0),
69 ];
70
71 print("Test points created for further calculations");
72 }
73
77 void _demonstrateBoundingBoxOperations() {
78 print("--- BoundingBox Operations ---");
79
80 Point bottomLeft = Point(0.0, 0.0);
81 Point topRight = Point(10.0, 20.0);
82
83 // [dart_BoundingBox_constructor]
84 BoundingBox boundingBox = BoundingBox(bottomLeft, topRight);
85 print("Created bounding box: bottomLeft(${boundingBox.bottomLeft.x}, ${boundingBox.bottomLeft.y}), topRight(${boundingBox.topRight.x}, ${boundingBox.topRight.y})");
86 // [dart_BoundingBox_constructor]
87
88 // [dart_BoundingBox_getBottomLeft]
89 Point leftCorner = boundingBox.bottomLeft;
90 print("Bottom-left corner: (${leftCorner.x}, ${leftCorner.y})");
91 // [dart_BoundingBox_getBottomLeft]
92
93 // [dart_BoundingBox_getTopRight]
94 Point rightCorner = boundingBox.topRight;
95 print("Top-right corner: (${rightCorner.x}, ${rightCorner.y})");
96 // [dart_BoundingBox_getTopRight]
97 }
98
102 void _demonstrateGlobalPointOperations() {
103 print("--- GlobalPoint Operations ---");
104
105 // [dart_GlobalPoint_constructor]
106 // Create global points with latitude, longitude
107 GlobalPoint globalPoint1 = GlobalPoint(55.7558, 37.6176); // Moscow
108 GlobalPoint globalPoint2 = GlobalPoint(59.9311, 30.3609); // St. Petersburg
109 GlobalPoint globalPoint3 = GlobalPoint(55.7522, 37.6156); // Moscow center
110 print("Created global points: GP1(${globalPoint1.latitude}, ${globalPoint1.longitude}), GP2(${globalPoint2.latitude}, ${globalPoint2.longitude})");
111 // [dart_GlobalPoint_constructor]
112
113 // [dart_GlobalPoint_getLatitude]
114 // Get latitude
115 double lat1 = globalPoint1.latitude;
116 print("GlobalPoint1 latitude: $lat1");
117 // [dart_GlobalPoint_getLatitude]
118
119 // [dart_GlobalPoint_getLongitude]
120 // Get longitude
121 double lon1 = globalPoint1.longitude;
122 print("GlobalPoint1 longitude: $lon1");
123 // [dart_GlobalPoint_getLongitude]
124
125 // [dart_GeometryUtils_distanceBetweenGlobalPoints]
126 // Calculate distance between global points
127 double globalDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint2);
128 print("Distance between Moscow and St. Petersburg: ${globalDistance.toStringAsFixed(2)} meters");
129 // [dart_GeometryUtils_distanceBetweenGlobalPoints]
130
131 // Calculate distance between nearby points
132 double nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3);
133 print("Distance between Moscow points: ${nearbyDistance.toStringAsFixed(2)} meters");
134 }
135
139 void _demonstrateSegmentOperations() {
140 print("--- Segment Operations ---");
141
142 // Create points for segments
143 Point start1 = Point(0.0, 0.0);
144 Point end1 = Point(10.0, 10.0);
145 Point start2 = Point(0.0, 10.0);
146 Point end2 = Point(10.0, 0.0);
147 Point testPoint = Point(5.0, 5.0);
148
149 // [dart_Segment_constructor]
150 // Create segments
151 Segment segment1 = Segment(start1, end1);
152 Segment segment2 = Segment(start2, end2);
153 print("Created segments: S1((${segment1.start.x}, ${segment1.start.y}) -> (${segment1.end.x}, ${segment1.end.y})), S2((${segment2.start.x}, ${segment2.start.y}) -> (${segment2.end.x}, ${segment2.end.y}))");
154 // [dart_Segment_constructor]
155
156 // [dart_Segment_getStart]
157 // Get start point
158 Point segment1Start = segment1.start;
159 print("Segment1 start point: (${segment1Start.x}, ${segment1Start.y})");
160 // [dart_Segment_getStart]
161
162 // [dart_Segment_getEnd]
163 // Get end point
164 Point segment1End = segment1.end;
165 print("Segment1 end point: (${segment1End.x}, ${segment1End.y})");
166 // [dart_Segment_getEnd]
167
168 // [dart_GeometryUtils_segmentLength]
169 // Calculate segment length
170 double length1 = GeometryUtils.segmentLength(segment1);
171 double length2 = GeometryUtils.segmentLength(segment2);
172 print("Segment1 length: ${length1.toStringAsFixed(2)} meters");
173 print("Segment2 length: ${length2.toStringAsFixed(2)} meters");
174 // [dart_GeometryUtils_segmentLength]
175
176 // [dart_GeometryUtils_segmentPointDistance]
177 // Calculate distance from segment to point
178 double distanceToPoint = GeometryUtils.segmentPointDistance(segment1, testPoint);
179 print("Distance from segment1 to test point: ${distanceToPoint.toStringAsFixed(2)} meters");
180 // [dart_GeometryUtils_segmentPointDistance]
181
182 // [dart_GeometryUtils_segmentIntersectsSegment]
183 // Check if segments intersect
184 bool intersects = GeometryUtils.segmentIntersectsSegment(segment1, segment2);
185 print("Segments intersect: $intersects");
186 // [dart_GeometryUtils_segmentIntersectsSegment]
187
188 // [dart_GeometryUtils_segmentIntersectionSegment]
189 // Calculate intersection point
190 Point intersection = GeometryUtils.segmentIntersectionSegment(segment1, segment2);
191 print("Intersection point: (${intersection.x.toStringAsFixed(2)}, ${intersection.y.toStringAsFixed(2)})");
192 // [dart_GeometryUtils_segmentIntersectionSegment]
193
194 // [dart_GeometryUtils_divisionRatioBySegment]
195 // Calculate division ratio
196 double divisionRatio = GeometryUtils.divisionRatioBySegment(segment1, segment2);
197 print("Division ratio: ${divisionRatio.toStringAsFixed(2)}");
198 // [dart_GeometryUtils_divisionRatioBySegment]
199
200 // [dart_GeometryUtils_getRatioPoint]
201 // Get point at specific ratio
202 double ratio = 0.5;
203 Point ratioPoint = GeometryUtils.getRatioPoint(segment1, ratio);
204 print("Point at ratio $ratio: (${ratioPoint.x.toStringAsFixed(2)}, ${ratioPoint.y.toStringAsFixed(2)})");
205 // [dart_GeometryUtils_getRatioPoint]
206
207 // [dart_GeometryUtils_getProjectionRatio]
208 // Calculate projection ratio
209 double projectionRatio = GeometryUtils.getProjectionRatio(segment1, testPoint);
210 print("Projection ratio: ${projectionRatio.toStringAsFixed(2)}");
211 // [dart_GeometryUtils_getProjectionRatio]
212 }
213
217 void _demonstratePolygonOperations() {
218 print("--- Polygon Operations ---");
219
220 // Create points for polygon
221 List<Point> polygonPoints = [
222 Point(0.0, 0.0),
223 Point(10.0, 0.0),
224 Point(10.0, 10.0),
225 Point(0.0, 10.0),
226 ];
227
228 // [dart_Polygon_constructor]
229 // Create polygon
230 Polygon polygon = Polygon(polygonPoints);
231 print("Created polygon with ${polygon.points.length} points");
232 // [dart_Polygon_constructor]
233
234 // [dart_Polygon_getPoints]
235 // Get polygon points
236 List<Point> points = polygon.points;
237 print("Polygon points: ${points.map((p) => "(${p.x}, ${p.y})").join(", ")}");
238 // [dart_Polygon_getPoints]
239
240 // [dart_GeometryUtils_polygonArea]
241 // Calculate polygon area
242 double area = GeometryUtils.polygonArea(polygon);
243 print("Polygon area: ${area.toStringAsFixed(2)} square meters");
244 // [dart_GeometryUtils_polygonArea]
245
246 // [dart_GeometryUtils_polygonCenter]
247 // Calculate polygon center
248 Point center = GeometryUtils.polygonCenter(polygon);
249 print("Polygon center: (${center.x.toStringAsFixed(2)}, ${center.y.toStringAsFixed(2)})");
250 // [dart_GeometryUtils_polygonCenter]
251
252 // [dart_GeometryUtils_polygonContainsPoint]
253 // Check if polygon contains point
254 Point insidePoint = Point(5.0, 5.0);
255 Point outsidePoint = Point(15.0, 15.0);
256
257 bool containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint);
258 bool containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint);
259
260 print("Polygon contains inside point: $containsInside");
261 print("Polygon contains outside point: $containsOutside");
262 // [dart_GeometryUtils_polygonContainsPoint]
263
264 // Create complex polygon
265 List<Point> complexPolygonPoints = [
266 Point(0.0, 0.0),
267 Point(20.0, 0.0),
268 Point(20.0, 20.0),
269 Point(10.0, 10.0),
270 Point(0.0, 20.0),
271 ];
272 Polygon complexPolygon = Polygon(complexPolygonPoints);
273
274 double complexArea = GeometryUtils.polygonArea(complexPolygon);
275 Point complexCenter = GeometryUtils.polygonCenter(complexPolygon);
276
277 print("Complex polygon area: ${complexArea.toStringAsFixed(2)} square meters");
278 print("Complex polygon center: (${complexCenter.x.toStringAsFixed(2)}, ${complexCenter.y.toStringAsFixed(2)})");
279 }
280
284 void _demonstrateLineOperations() {
285 print("--- Line Operations ---");
286
287 // Create segments for line
288 List<Segment> lineSegments = [
289 Segment(Point(0.0, 0.0), Point(10.0, 10.0)),
290 Segment(Point(10.0, 10.0), Point(20.0, 5.0)),
291 Segment(Point(20.0, 5.0), Point(30.0, 15.0)),
292 ];
293
294 // [dart_Line_constructor]
295 // Create line
296 Line line = Line(lineSegments);
297 print("Created line with ${line.segments.length} segments");
298 // [dart_Line_constructor]
299
300 // [dart_Line_getSegments]
301 // Get line segments
302 List<Segment> segments = line.segments;
303 print("Line segments: ${segments.length} segments");
304 // [dart_Line_getSegments]
305
306 // Calculate total length of line
307 double totalLength = 0.0;
308 for (Segment segment in segments) {
309 totalLength += GeometryUtils.segmentLength(segment);
310 }
311 print("Total line length: ${totalLength.toStringAsFixed(2)} meters");
312
313 // Create complex line
314 List<Segment> complexLineSegments = [
315 Segment(Point(0.0, 0.0), Point(5.0, 5.0)),
316 Segment(Point(5.0, 5.0), Point(10.0, 0.0)),
317 Segment(Point(10.0, 0.0), Point(15.0, 10.0)),
318 Segment(Point(15.0, 10.0), Point(20.0, 5.0)),
319 ];
320 Line complexLine = Line(complexLineSegments);
321
322 double complexTotalLength = 0.0;
323 for (Segment segment in complexLine.segments) {
324 complexTotalLength += GeometryUtils.segmentLength(segment);
325 }
326 print("Complex line total length: ${complexTotalLength.toStringAsFixed(2)} meters");
327 }
328
332 void _demonstrateAdvancedGeometryOperations() {
333 print("--- Advanced Geometry Operations ---");
334
335 // Test distance calculations with various scenarios
336 _demonstrateDistanceCalculations();
337
338 // Test intersection scenarios
339 _demonstrateIntersectionScenarios();
340
341 // Test polygon operations with different shapes
342 _demonstratePolygonScenarios();
343
344 // Test projection and ratio calculations
345 _demonstrateProjectionCalculations();
346 }
347
351 void _demonstrateDistanceCalculations() {
352 print("--- Distance Calculations ---");
353
354 // Test point distances
355 Point origin = Point(0.0, 0.0);
356 List<Point> testPoints = [
357 Point(3.0, 4.0), // Should be 5.0 distance
358 Point(6.0, 8.0), // Should be 10.0 distance
359 Point(1.0, 1.0), // Should be sqrt(2) distance
360 ];
361
362 for (Point testPoint in testPoints) {
363 double distance = GeometryUtils.distanceBetweenPoints(origin, testPoint);
364 print("Distance from origin to (${testPoint.x}, ${testPoint.y}): ${distance.toStringAsFixed(2)} meters");
365 }
366
367 // Test global point distances
368 GlobalPoint moscow = GlobalPoint(55.7558, 37.6176);
369 GlobalPoint london = GlobalPoint(51.5074, -0.1278);
370 GlobalPoint tokyo = GlobalPoint(35.6762, 139.6503);
371
372 double moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london);
373 double moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo);
374
375 print("Moscow to London: ${(moscowLondon / 1000).toStringAsFixed(2)} km");
376 print("Moscow to Tokyo: ${(moscowTokyo / 1000).toStringAsFixed(2)} km");
377 }
378
382 void _demonstrateIntersectionScenarios() {
383 print("--- Intersection Scenarios ---");
384
385 // Parallel segments (should not intersect)
386 Segment parallel1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0));
387 Segment parallel2 = Segment(Point(0.0, 5.0), Point(10.0, 5.0));
388
389 bool parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2);
390 print("Parallel segments intersect: $parallelIntersects");
391
392 // Perpendicular segments (should intersect)
393 Segment perpendicular1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0));
394 Segment perpendicular2 = Segment(Point(5.0, -5.0), Point(5.0, 5.0));
395
396 bool perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2);
397 print("Perpendicular segments intersect: $perpendicularIntersects");
398
399 if (perpendicularIntersects) {
400 Point intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2);
401 print("Intersection point: (${intersection.x.toStringAsFixed(2)}, ${intersection.y.toStringAsFixed(2)})");
402 }
403
404 // Overlapping segments
405 Segment overlap1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0));
406 Segment overlap2 = Segment(Point(5.0, 0.0), Point(15.0, 0.0));
407
408 bool overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2);
409 print("Overlapping segments intersect: $overlapIntersects");
410 }
411
415 void _demonstratePolygonScenarios() {
416 print("--- Polygon Scenarios ---");
417
418 // Regular square
419 List<Point> square = [
420 Point(0.0, 0.0),
421 Point(10.0, 0.0),
422 Point(10.0, 10.0),
423 Point(0.0, 10.0),
424 ];
425 Polygon squarePolygon = Polygon(square);
426
427 double squareArea = GeometryUtils.polygonArea(squarePolygon);
428 Point squareCenter = GeometryUtils.polygonCenter(squarePolygon);
429
430 print("Square area: ${squareArea.toStringAsFixed(2)} square meters");
431 print("Square center: (${squareCenter.x.toStringAsFixed(2)}, ${squareCenter.y.toStringAsFixed(2)})");
432
433 // Triangle
434 List<Point> triangle = [
435 Point(0.0, 0.0),
436 Point(10.0, 0.0),
437 Point(5.0, 10.0),
438 ];
439 Polygon trianglePolygon = Polygon(triangle);
440
441 double triangleArea = GeometryUtils.polygonArea(trianglePolygon);
442 Point triangleCenter = GeometryUtils.polygonCenter(trianglePolygon);
443
444 print("Triangle area: ${triangleArea.toStringAsFixed(2)} square meters");
445 print("Triangle center: (${triangleCenter.x.toStringAsFixed(2)}, ${triangleCenter.y.toStringAsFixed(2)})");
446
447 // Test point containment
448 Point insideSquare = Point(5.0, 5.0);
449 Point outsideSquare = Point(15.0, 15.0);
450
451 bool containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare);
452 bool containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare);
453
454 print("Square contains (5,5): $containsInside");
455 print("Square contains (15,15): $containsOutside");
456 }
457
461 void _demonstrateProjectionCalculations() {
462 print("--- Projection Calculations ---");
463
464 Segment segment = Segment(Point(0.0, 0.0), Point(10.0, 10.0));
465
466 // Test different ratio points
467 List<double> ratios = [0.0, 0.25, 0.5, 0.75, 1.0];
468
469 for (double ratio in ratios) {
470 Point ratioPoint = GeometryUtils.getRatioPoint(segment, ratio);
471 print("Ratio $ratio: (${ratioPoint.x.toStringAsFixed(2)}, ${ratioPoint.y.toStringAsFixed(2)})");
472 }
473
474 // Test projection ratio calculation
475 Point testPoint = Point(5.0, 5.0);
476 double projectionRatio = GeometryUtils.getProjectionRatio(segment, testPoint);
477 print("Projection ratio for point (5,5): ${projectionRatio.toStringAsFixed(2)}");
478
479 // Test distance from segment to point
480 Point offSegmentPoint = Point(5.0, 0.0);
481 double distanceToOffPoint = GeometryUtils.segmentPointDistance(segment, offSegmentPoint);
482 print("Distance from segment to point (5,0): ${distanceToOffPoint.toStringAsFixed(2)} meters");
483 }
484
488 Future<void> runExample() async {
489 print("=== GeometryUtils Example ===");
490
491 // Wait a bit for processing
492 await Future.delayed(Duration(seconds: 1));
493
494 print("=== Example completed ===");
495 }
496}
497
501Future<void> main() async {
503 await example.runExample();
504}