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