Loading...
Searching...
No Matches
GeometryUtilsExample.kt
Go to the documentation of this file.
1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
3
4/**
5 * GeometryUtils usage example for Kotlin
6 * Demonstrates working with geometric calculations, points, segments, polygons, and lines
7 */
8class GeometryUtilsExample {
9
10 init {
11 demonstrateGeometryUtilsMethods()
12 }
13
14 /**
15 * Demonstrate GeometryUtils methods
16 */
17 fun demonstrateGeometryUtilsMethods() {
18 println("=== GeometryUtils Methods ===")
19
20 demonstratePointOperations()
21 demonstrateGlobalPointOperations()
22 demonstrateBoundingBoxOperations()
23 demonstrateSegmentOperations()
24 demonstratePolygonOperations()
25 demonstrateLineOperations()
26 demonstrateAdvancedGeometryOperations()
27 }
28
29 /**
30 * Demonstrate Point operations
31 */
32 fun demonstratePointOperations() {
33 println("--- Point Operations ---")
34
35 // [kotlin_Point_constructor]
36 // Create points with x, y coordinates
37 val point1 = Point(10.0, 20.0)
38 val point2 = Point(30.0, 40.0)
39 val point3 = Point(50.0, 60.0)
40 println("Created points: P1(${point1.x}, ${point1.y}), P2(${point2.x}, ${point2.y}), P3(${point3.x}, ${point3.y})")
41 // [kotlin_Point_constructor]
42
43 // [kotlin_Point_getX]
44 // Get X coordinate
45 val x1 = point1.x
46 println("Point1 X coordinate: $x1")
47 // [kotlin_Point_getX]
48
49 // [kotlin_Point_getY]
50 // Get Y coordinate
51 val y1 = point1.y
52 println("Point1 Y coordinate: $y1")
53 // [kotlin_Point_getY]
54
55 // [kotlin_GeometryUtils_distanceBetweenPoints]
56 // Calculate distance between points
57 val distance = GeometryUtils.distanceBetweenPoints(point1, point2)
58 println("Distance between P1 and P2: ${"%.2f".format(distance)} meters")
59 // [kotlin_GeometryUtils_distanceBetweenPoints]
60
61 // Create more points for testing
62 val testPoints = listOf(
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 println("Test points created for further calculations")
71 }
72
73 /**
74 * Demonstrate BoundingBox operations
75 */
76 fun demonstrateBoundingBoxOperations() {
77 println("--- BoundingBox Operations ---")
78
79 val bottomLeft = Point(0.0, 0.0)
80 val topRight = Point(10.0, 20.0)
81
82 // [kotlin_BoundingBox_constructor]
83 val boundingBox = BoundingBox(bottomLeft, topRight)
84 println("Created bounding box: bottomLeft(${boundingBox.bottomLeft.x}, ${boundingBox.bottomLeft.y}), topRight(${boundingBox.topRight.x}, ${boundingBox.topRight.y})")
85 // [kotlin_BoundingBox_constructor]
86
87 // [kotlin_BoundingBox_getBottomLeft]
88 val leftCorner = boundingBox.bottomLeft
89 println("Bottom-left corner: (${leftCorner.x}, ${leftCorner.y})")
90 // [kotlin_BoundingBox_getBottomLeft]
91
92 // [kotlin_BoundingBox_getTopRight]
93 val rightCorner = boundingBox.topRight
94 println("Top-right corner: (${rightCorner.x}, ${rightCorner.y})")
95 // [kotlin_BoundingBox_getTopRight]
96 }
97
98 /**
99 * Demonstrate GlobalPoint operations
100 */
101 fun demonstrateGlobalPointOperations() {
102 println("--- GlobalPoint Operations ---")
103
104 // [kotlin_GlobalPoint_constructor]
105 // Create global points with latitude, longitude
106 val globalPoint1 = GlobalPoint(55.7558, 37.6176) // Moscow
107 val globalPoint2 = GlobalPoint(59.9311, 30.3609) // St. Petersburg
108 val globalPoint3 = GlobalPoint(55.7522, 37.6156) // Moscow center
109 println("Created global points: GP1(${globalPoint1.latitude}, ${globalPoint1.longitude}), GP2(${globalPoint2.latitude}, ${globalPoint2.longitude})")
110 // [kotlin_GlobalPoint_constructor]
111
112 // [kotlin_GlobalPoint_getLatitude]
113 // Get latitude
114 val lat1 = globalPoint1.latitude
115 println("GlobalPoint1 latitude: $lat1")
116 // [kotlin_GlobalPoint_getLatitude]
117
118 // [kotlin_GlobalPoint_getLongitude]
119 // Get longitude
120 val lon1 = globalPoint1.longitude
121 println("GlobalPoint1 longitude: $lon1")
122 // [kotlin_GlobalPoint_getLongitude]
123
124 // [kotlin_GeometryUtils_distanceBetweenGlobalPoints]
125 // Calculate distance between global points
126 val globalDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint2)
127 println("Distance between Moscow and St. Petersburg: ${"%.2f".format(globalDistance)} meters")
128 // [kotlin_GeometryUtils_distanceBetweenGlobalPoints]
129
130 // Calculate distance between nearby points
131 val nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3)
132 println("Distance between Moscow points: ${"%.2f".format(nearbyDistance)} meters")
133 }
134
135 /**
136 * Demonstrate Segment operations
137 */
138 fun demonstrateSegmentOperations() {
139 println("--- Segment Operations ---")
140
141 // Create points for segments
142 val start1 = Point(0.0, 0.0)
143 val end1 = Point(10.0, 10.0)
144 val start2 = Point(0.0, 10.0)
145 val end2 = Point(10.0, 0.0)
146 val testPoint = Point(5.0, 5.0)
147
148 // [kotlin_Segment_constructor]
149 // Create segments
150 val segment1 = Segment(start1, end1)
151 val segment2 = Segment(start2, end2)
152 println("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}))")
153 // [kotlin_Segment_constructor]
154
155 // [kotlin_Segment_getStart]
156 // Get start point
157 val segment1Start = segment1.start
158 println("Segment1 start point: (${segment1Start.x}, ${segment1Start.y})")
159 // [kotlin_Segment_getStart]
160
161 // [kotlin_Segment_getEnd]
162 // Get end point
163 val segment1End = segment1.end
164 println("Segment1 end point: (${segment1End.x}, ${segment1End.y})")
165 // [kotlin_Segment_getEnd]
166
167 // [kotlin_GeometryUtils_segmentLength]
168 // Calculate segment length
169 val length1 = GeometryUtils.segmentLength(segment1)
170 val length2 = GeometryUtils.segmentLength(segment2)
171 println("Segment1 length: ${"%.2f".format(length1)} meters")
172 println("Segment2 length: ${"%.2f".format(length2)} meters")
173 // [kotlin_GeometryUtils_segmentLength]
174
175 // [kotlin_GeometryUtils_segmentPointDistance]
176 // Calculate distance from segment to point
177 val distanceToPoint = GeometryUtils.segmentPointDistance(segment1, testPoint)
178 println("Distance from segment1 to test point: ${"%.2f".format(distanceToPoint)} meters")
179 // [kotlin_GeometryUtils_segmentPointDistance]
180
181 // [kotlin_GeometryUtils_segmentIntersectsSegment]
182 // Check if segments intersect
183 val intersects = GeometryUtils.segmentIntersectsSegment(segment1, segment2)
184 println("Segments intersect: $intersects")
185 // [kotlin_GeometryUtils_segmentIntersectsSegment]
186
187 // [kotlin_GeometryUtils_segmentIntersectionSegment]
188 // Calculate intersection point
189 val intersection = GeometryUtils.segmentIntersectionSegment(segment1, segment2)
190 println("Intersection point: (${"%.2f".format(intersection.x)}, ${"%.2f".format(intersection.y)})")
191 // [kotlin_GeometryUtils_segmentIntersectionSegment]
192
193 // [kotlin_GeometryUtils_divisionRatioBySegment]
194 // Calculate division ratio
195 val divisionRatio = GeometryUtils.divisionRatioBySegment(segment1, segment2)
196 println("Division ratio: ${"%.2f".format(divisionRatio)}")
197 // [kotlin_GeometryUtils_divisionRatioBySegment]
198
199 // [kotlin_GeometryUtils_getRatioPoint]
200 // Get point at specific ratio
201 val ratio = 0.5
202 val ratioPoint = GeometryUtils.getRatioPoint(segment1, ratio)
203 println("Point at ratio $ratio: (${"%.2f".format(ratioPoint.x)}, ${"%.2f".format(ratioPoint.y)})")
204 // [kotlin_GeometryUtils_getRatioPoint]
205
206 // [kotlin_GeometryUtils_getProjectionRatio]
207 // Calculate projection ratio
208 val projectionRatio = GeometryUtils.getProjectionRatio(segment1, testPoint)
209 println("Projection ratio: ${"%.2f".format(projectionRatio)}")
210 // [kotlin_GeometryUtils_getProjectionRatio]
211 }
212
213 /**
214 * Demonstrate Polygon operations
215 */
216 fun demonstratePolygonOperations() {
217 println("--- Polygon Operations ---")
218
219 // Create points for polygon
220 val polygonPoints = listOf(
221 Point(0.0, 0.0),
222 Point(10.0, 0.0),
223 Point(10.0, 10.0),
224 Point(0.0, 10.0)
225 )
226
227 // [kotlin_Polygon_constructor]
228 // Create polygon
229 val polygon = Polygon(polygonPoints)
230 println("Created polygon with ${polygon.points.size} points")
231 // [kotlin_Polygon_constructor]
232
233 // [kotlin_Polygon_getPoints]
234 // Get polygon points
235 val points = polygon.points
236 println("Polygon points: ${points.joinToString(", ") { "(${it.x}, ${it.y})" }}")
237 // [kotlin_Polygon_getPoints]
238
239 // [kotlin_GeometryUtils_polygonArea]
240 // Calculate polygon area
241 val area = GeometryUtils.polygonArea(polygon)
242 println("Polygon area: ${"%.2f".format(area)} square meters")
243 // [kotlin_GeometryUtils_polygonArea]
244
245 // [kotlin_GeometryUtils_polygonCenter]
246 // Calculate polygon center
247 val center = GeometryUtils.polygonCenter(polygon)
248 println("Polygon center: (${"%.2f".format(center.x)}, ${"%.2f".format(center.y)})")
249 // [kotlin_GeometryUtils_polygonCenter]
250
251 // [kotlin_GeometryUtils_polygonContainsPoint]
252 // Check if polygon contains point
253 val insidePoint = Point(5.0, 5.0)
254 val outsidePoint = Point(15.0, 15.0)
255
256 val containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint)
257 val containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint)
258
259 println("Polygon contains inside point: $containsInside")
260 println("Polygon contains outside point: $containsOutside")
261 // [kotlin_GeometryUtils_polygonContainsPoint]
262
263 // Create complex polygon
264 val complexPolygonPoints = listOf(
265 Point(0.0, 0.0),
266 Point(20.0, 0.0),
267 Point(20.0, 20.0),
268 Point(10.0, 10.0),
269 Point(0.0, 20.0)
270 )
271 val complexPolygon = Polygon(complexPolygonPoints)
272
273 val complexArea = GeometryUtils.polygonArea(complexPolygon)
274 val complexCenter = GeometryUtils.polygonCenter(complexPolygon)
275
276 println("Complex polygon area: ${"%.2f".format(complexArea)} square meters")
277 println("Complex polygon center: (${"%.2f".format(complexCenter.x)}, ${"%.2f".format(complexCenter.y)})")
278 }
279
280 /**
281 * Demonstrate Line operations
282 */
283 fun demonstrateLineOperations() {
284 println("--- Line Operations ---")
285
286 // Create segments for line
287 val lineSegments = listOf(
288 Segment(Point(0.0, 0.0), Point(10.0, 10.0)),
289 Segment(Point(10.0, 10.0), Point(20.0, 5.0)),
290 Segment(Point(20.0, 5.0), Point(30.0, 15.0))
291 )
292
293 // [kotlin_Line_constructor]
294 // Create line
295 val line = Line(lineSegments)
296 println("Created line with ${line.segments.size} segments")
297 // [kotlin_Line_constructor]
298
299 // [kotlin_Line_getSegments]
300 // Get line segments
301 val segments = line.segments
302 println("Line segments: ${segments.size} segments")
303 // [kotlin_Line_getSegments]
304
305 // Calculate total length of line
306 var totalLength = 0.0
307 for (segment in segments) {
308 totalLength += GeometryUtils.segmentLength(segment)
309 }
310 println("Total line length: ${"%.2f".format(totalLength)} meters")
311
312 // Create complex line
313 val complexLineSegments = listOf(
314 Segment(Point(0.0, 0.0), Point(5.0, 5.0)),
315 Segment(Point(5.0, 5.0), Point(10.0, 0.0)),
316 Segment(Point(10.0, 0.0), Point(15.0, 10.0)),
317 Segment(Point(15.0, 10.0), Point(20.0, 5.0))
318 )
319 val complexLine = Line(complexLineSegments)
320
321 var complexTotalLength = 0.0
322 for (segment in complexLine.segments) {
323 complexTotalLength += GeometryUtils.segmentLength(segment)
324 }
325 println("Complex line total length: ${"%.2f".format(complexTotalLength)} meters")
326 }
327
328 /**
329 * Demonstrate advanced geometry operations
330 */
331 fun demonstrateAdvancedGeometryOperations() {
332 println("--- Advanced Geometry Operations ---")
333
334 // Test distance calculations with various scenarios
335 demonstrateDistanceCalculations()
336
337 // Test intersection scenarios
338 demonstrateIntersectionScenarios()
339
340 // Test polygon operations with different shapes
341 demonstratePolygonScenarios()
342
343 // Test projection and ratio calculations
344 demonstrateProjectionCalculations()
345 }
346
347 /**
348 * Demonstrate distance calculations
349 */
350 fun demonstrateDistanceCalculations() {
351 println("--- Distance Calculations ---")
352
353 // Test point distances
354 val origin = Point(0.0, 0.0)
355 val testPoints = listOf(
356 Point(3.0, 4.0), // Should be 5.0 distance
357 Point(6.0, 8.0), // Should be 10.0 distance
358 Point(1.0, 1.0) // Should be sqrt(2) distance
359 )
360
361 for (testPoint in testPoints) {
362 val distance = GeometryUtils.distanceBetweenPoints(origin, testPoint)
363 println("Distance from origin to (${testPoint.x}, ${testPoint.y}): ${"%.2f".format(distance)} meters")
364 }
365
366 // Test global point distances
367 val moscow = GlobalPoint(55.7558, 37.6176)
368 val london = GlobalPoint(51.5074, -0.1278)
369 val tokyo = GlobalPoint(35.6762, 139.6503)
370
371 val moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london)
372 val moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo)
373
374 println("Moscow to London: ${"%.2f".format(moscowLondon / 1000)} km")
375 println("Moscow to Tokyo: ${"%.2f".format(moscowTokyo / 1000)} km")
376 }
377
378 /**
379 * Demonstrate intersection scenarios
380 */
381 fun demonstrateIntersectionScenarios() {
382 println("--- Intersection Scenarios ---")
383
384 // Parallel segments (should not intersect)
385 val parallel1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
386 val parallel2 = Segment(Point(0.0, 5.0), Point(10.0, 5.0))
387
388 val parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2)
389 println("Parallel segments intersect: $parallelIntersects")
390
391 // Perpendicular segments (should intersect)
392 val perpendicular1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
393 val perpendicular2 = Segment(Point(5.0, -5.0), Point(5.0, 5.0))
394
395 val perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2)
396 println("Perpendicular segments intersect: $perpendicularIntersects")
397
398 if (perpendicularIntersects) {
399 val intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2)
400 println("Intersection point: (${"%.2f".format(intersection.x)}, ${"%.2f".format(intersection.y)})")
401 }
402
403 // Overlapping segments
404 val overlap1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
405 val overlap2 = Segment(Point(5.0, 0.0), Point(15.0, 0.0))
406
407 val overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2)
408 println("Overlapping segments intersect: $overlapIntersects")
409 }
410
411 /**
412 * Demonstrate polygon scenarios
413 */
414 fun demonstratePolygonScenarios() {
415 println("--- Polygon Scenarios ---")
416
417 // Regular square
418 val square = listOf(
419 Point(0.0, 0.0),
420 Point(10.0, 0.0),
421 Point(10.0, 10.0),
422 Point(0.0, 10.0)
423 )
424 val squarePolygon = Polygon(square)
425
426 val squareArea = GeometryUtils.polygonArea(squarePolygon)
427 val squareCenter = GeometryUtils.polygonCenter(squarePolygon)
428
429 println("Square area: ${"%.2f".format(squareArea)} square meters")
430 println("Square center: (${"%.2f".format(squareCenter.x)}, ${"%.2f".format(squareCenter.y)})")
431
432 // Triangle
433 val triangle = listOf(
434 Point(0.0, 0.0),
435 Point(10.0, 0.0),
436 Point(5.0, 10.0)
437 )
438 val trianglePolygon = Polygon(triangle)
439
440 val triangleArea = GeometryUtils.polygonArea(trianglePolygon)
441 val triangleCenter = GeometryUtils.polygonCenter(trianglePolygon)
442
443 println("Triangle area: ${"%.2f".format(triangleArea)} square meters")
444 println("Triangle center: (${"%.2f".format(triangleCenter.x)}, ${"%.2f".format(triangleCenter.y)})")
445
446 // Test point containment
447 val insideSquare = Point(5.0, 5.0)
448 val outsideSquare = Point(15.0, 15.0)
449
450 val containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare)
451 val containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare)
452
453 println("Square contains (5,5): $containsInside")
454 println("Square contains (15,15): $containsOutside")
455 }
456
457 /**
458 * Demonstrate projection calculations
459 */
460 fun demonstrateProjectionCalculations() {
461 println("--- Projection Calculations ---")
462
463 val segment = Segment(Point(0.0, 0.0), Point(10.0, 10.0))
464
465 // Test different ratio points
466 val ratios = listOf(0.0, 0.25, 0.5, 0.75, 1.0)
467
468 for (ratio in ratios) {
469 val ratioPoint = GeometryUtils.getRatioPoint(segment, ratio)
470 println("Ratio $ratio: (${"%.2f".format(ratioPoint.x)}, ${"%.2f".format(ratioPoint.y)})")
471 }
472
473 // Test projection ratio calculation
474 val testPoint = Point(5.0, 5.0)
475 val projectionRatio = GeometryUtils.getProjectionRatio(segment, testPoint)
476 println("Projection ratio for point (5,5): ${"%.2f".format(projectionRatio)}")
477
478 // Test distance from segment to point
479 val offSegmentPoint = Point(5.0, 0.0)
480 val distanceToOffPoint = GeometryUtils.segmentPointDistance(segment, offSegmentPoint)
481 println("Distance from segment to point (5,0): ${"%.2f".format(distanceToOffPoint)} meters")
482 }
483
484 /**
485 * Main demonstration method
486 */
487 suspend fun runExample() {
488 println("=== GeometryUtils Example ===")
489
490 // Wait a bit for processing
491 delay(1000)
492
493 println("=== Example completed ===")
494 }
495}
496
497/**
498 * Function to run the example
499 */
500fun main() = runBlocking {
501 val example = GeometryUtilsExample()
502 example.runExample()
503}