1import kotlinx.coroutines.delay
2import kotlinx.coroutines.runBlocking
5 * GeometryUtils usage example for Kotlin
6 * Demonstrates working with geometric calculations, points, segments, polygons, and lines
8class GeometryUtilsExample {
11 demonstrateGeometryUtilsMethods()
15 * Demonstrate GeometryUtils methods
17 fun demonstrateGeometryUtilsMethods() {
18 println("=== GeometryUtils Methods ===")
20 demonstratePointOperations()
21 demonstrateGlobalPointOperations()
22 demonstrateBoundingBoxOperations()
23 demonstrateSegmentOperations()
24 demonstratePolygonOperations()
25 demonstrateLineOperations()
26 demonstrateAdvancedGeometryOperations()
30 * Demonstrate Point operations
32 fun demonstratePointOperations() {
33 println("--- Point Operations ---")
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]
43 // [kotlin_Point_getX]
46 println("Point1 X coordinate: $x1")
47 // [kotlin_Point_getX]
49 // [kotlin_Point_getY]
52 println("Point1 Y coordinate: $y1")
53 // [kotlin_Point_getY]
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]
61 // Create more points for testing
62 val testPoints = listOf(
70 println("Test points created for further calculations")
74 * Demonstrate BoundingBox operations
76 fun demonstrateBoundingBoxOperations() {
77 println("--- BoundingBox Operations ---")
79 val bottomLeft = Point(0.0, 0.0)
80 val topRight = Point(10.0, 20.0)
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]
87 // [kotlin_BoundingBox_getBottomLeft]
88 val leftCorner = boundingBox.bottomLeft
89 println("Bottom-left corner: (${leftCorner.x}, ${leftCorner.y})")
90 // [kotlin_BoundingBox_getBottomLeft]
92 // [kotlin_BoundingBox_getTopRight]
93 val rightCorner = boundingBox.topRight
94 println("Top-right corner: (${rightCorner.x}, ${rightCorner.y})")
95 // [kotlin_BoundingBox_getTopRight]
99 * Demonstrate GlobalPoint operations
101 fun demonstrateGlobalPointOperations() {
102 println("--- GlobalPoint Operations ---")
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]
112 // [kotlin_GlobalPoint_getLatitude]
114 val lat1 = globalPoint1.latitude
115 println("GlobalPoint1 latitude: $lat1")
116 // [kotlin_GlobalPoint_getLatitude]
118 // [kotlin_GlobalPoint_getLongitude]
120 val lon1 = globalPoint1.longitude
121 println("GlobalPoint1 longitude: $lon1")
122 // [kotlin_GlobalPoint_getLongitude]
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]
130 // Calculate distance between nearby points
131 val nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3)
132 println("Distance between Moscow points: ${"%.2f".format(nearbyDistance)} meters")
136 * Demonstrate Segment operations
138 fun demonstrateSegmentOperations() {
139 println("--- Segment Operations ---")
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)
148 // [kotlin_Segment_constructor]
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]
155 // [kotlin_Segment_getStart]
157 val segment1Start = segment1.start
158 println("Segment1 start point: (${segment1Start.x}, ${segment1Start.y})")
159 // [kotlin_Segment_getStart]
161 // [kotlin_Segment_getEnd]
163 val segment1End = segment1.end
164 println("Segment1 end point: (${segment1End.x}, ${segment1End.y})")
165 // [kotlin_Segment_getEnd]
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]
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]
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]
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]
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]
199 // [kotlin_GeometryUtils_getRatioPoint]
200 // Get point at specific ratio
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]
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]
214 * Demonstrate Polygon operations
216 fun demonstratePolygonOperations() {
217 println("--- Polygon Operations ---")
219 // Create points for polygon
220 val polygonPoints = listOf(
227 // [kotlin_Polygon_constructor]
229 val polygon = Polygon(polygonPoints)
230 println("Created polygon with ${polygon.points.size} points")
231 // [kotlin_Polygon_constructor]
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]
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]
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]
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)
256 val containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint)
257 val containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint)
259 println("Polygon contains inside point: $containsInside")
260 println("Polygon contains outside point: $containsOutside")
261 // [kotlin_GeometryUtils_polygonContainsPoint]
263 // Create complex polygon
264 val complexPolygonPoints = listOf(
271 val complexPolygon = Polygon(complexPolygonPoints)
273 val complexArea = GeometryUtils.polygonArea(complexPolygon)
274 val complexCenter = GeometryUtils.polygonCenter(complexPolygon)
276 println("Complex polygon area: ${"%.2f".format(complexArea)} square meters")
277 println("Complex polygon center: (${"%.2f".format(complexCenter.x)}, ${"%.2f".format(complexCenter.y)})")
281 * Demonstrate Line operations
283 fun demonstrateLineOperations() {
284 println("--- Line Operations ---")
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))
293 // [kotlin_Line_constructor]
295 val line = Line(lineSegments)
296 println("Created line with ${line.segments.size} segments")
297 // [kotlin_Line_constructor]
299 // [kotlin_Line_getSegments]
301 val segments = line.segments
302 println("Line segments: ${segments.size} segments")
303 // [kotlin_Line_getSegments]
305 // Calculate total length of line
306 var totalLength = 0.0
307 for (segment in segments) {
308 totalLength += GeometryUtils.segmentLength(segment)
310 println("Total line length: ${"%.2f".format(totalLength)} meters")
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))
319 val complexLine = Line(complexLineSegments)
321 var complexTotalLength = 0.0
322 for (segment in complexLine.segments) {
323 complexTotalLength += GeometryUtils.segmentLength(segment)
325 println("Complex line total length: ${"%.2f".format(complexTotalLength)} meters")
329 * Demonstrate advanced geometry operations
331 fun demonstrateAdvancedGeometryOperations() {
332 println("--- Advanced Geometry Operations ---")
334 // Test distance calculations with various scenarios
335 demonstrateDistanceCalculations()
337 // Test intersection scenarios
338 demonstrateIntersectionScenarios()
340 // Test polygon operations with different shapes
341 demonstratePolygonScenarios()
343 // Test projection and ratio calculations
344 demonstrateProjectionCalculations()
348 * Demonstrate distance calculations
350 fun demonstrateDistanceCalculations() {
351 println("--- Distance Calculations ---")
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
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")
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)
371 val moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london)
372 val moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo)
374 println("Moscow to London: ${"%.2f".format(moscowLondon / 1000)} km")
375 println("Moscow to Tokyo: ${"%.2f".format(moscowTokyo / 1000)} km")
379 * Demonstrate intersection scenarios
381 fun demonstrateIntersectionScenarios() {
382 println("--- Intersection Scenarios ---")
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))
388 val parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2)
389 println("Parallel segments intersect: $parallelIntersects")
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))
395 val perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2)
396 println("Perpendicular segments intersect: $perpendicularIntersects")
398 if (perpendicularIntersects) {
399 val intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2)
400 println("Intersection point: (${"%.2f".format(intersection.x)}, ${"%.2f".format(intersection.y)})")
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))
407 val overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2)
408 println("Overlapping segments intersect: $overlapIntersects")
412 * Demonstrate polygon scenarios
414 fun demonstratePolygonScenarios() {
415 println("--- Polygon Scenarios ---")
424 val squarePolygon = Polygon(square)
426 val squareArea = GeometryUtils.polygonArea(squarePolygon)
427 val squareCenter = GeometryUtils.polygonCenter(squarePolygon)
429 println("Square area: ${"%.2f".format(squareArea)} square meters")
430 println("Square center: (${"%.2f".format(squareCenter.x)}, ${"%.2f".format(squareCenter.y)})")
433 val triangle = listOf(
438 val trianglePolygon = Polygon(triangle)
440 val triangleArea = GeometryUtils.polygonArea(trianglePolygon)
441 val triangleCenter = GeometryUtils.polygonCenter(trianglePolygon)
443 println("Triangle area: ${"%.2f".format(triangleArea)} square meters")
444 println("Triangle center: (${"%.2f".format(triangleCenter.x)}, ${"%.2f".format(triangleCenter.y)})")
446 // Test point containment
447 val insideSquare = Point(5.0, 5.0)
448 val outsideSquare = Point(15.0, 15.0)
450 val containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare)
451 val containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare)
453 println("Square contains (5,5): $containsInside")
454 println("Square contains (15,15): $containsOutside")
458 * Demonstrate projection calculations
460 fun demonstrateProjectionCalculations() {
461 println("--- Projection Calculations ---")
463 val segment = Segment(Point(0.0, 0.0), Point(10.0, 10.0))
465 // Test different ratio points
466 val ratios = listOf(0.0, 0.25, 0.5, 0.75, 1.0)
468 for (ratio in ratios) {
469 val ratioPoint = GeometryUtils.getRatioPoint(segment, ratio)
470 println("Ratio $ratio: (${"%.2f".format(ratioPoint.x)}, ${"%.2f".format(ratioPoint.y)})")
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)}")
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")
485 * Main demonstration method
487 suspend fun runExample() {
488 println("=== GeometryUtils Example ===")
490 // Wait a bit for processing
493 println("=== Example completed ===")
498 * Function to run the example
500fun main() = runBlocking {
501 val example = GeometryUtilsExample()