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 demonstrateSegmentOperations()
23 demonstratePolygonOperations()
24 demonstrateLineOperations()
25 demonstrateAdvancedGeometryOperations()
29 * Demonstrate Point operations
31 fun demonstratePointOperations() {
32 println("--- Point Operations ---")
34 // [kotlin_Point_constructor]
35 // Create points with x, y coordinates
36 val point1 = Point(10.0, 20.0)
37 val point2 = Point(30.0, 40.0)
38 val point3 = Point(50.0, 60.0)
39 println("Created points: P1(${point1.x}, ${point1.y}), P2(${point2.x}, ${point2.y}), P3(${point3.x}, ${point3.y})")
40 // [kotlin_Point_constructor]
42 // [kotlin_Point_getX]
45 println("Point1 X coordinate: $x1")
46 // [kotlin_Point_getX]
48 // [kotlin_Point_getY]
51 println("Point1 Y coordinate: $y1")
52 // [kotlin_Point_getY]
54 // [kotlin_GeometryUtils_distanceBetweenPoints]
55 // Calculate distance between points
56 val distance = GeometryUtils.distanceBetweenPoints(point1, point2)
57 println("Distance between P1 and P2: ${"%.2f".format(distance)} meters")
58 // [kotlin_GeometryUtils_distanceBetweenPoints]
60 // Create more points for testing
61 val testPoints = listOf(
69 println("Test points created for further calculations")
73 * Demonstrate GlobalPoint operations
75 fun demonstrateGlobalPointOperations() {
76 println("--- GlobalPoint Operations ---")
78 // [kotlin_GlobalPoint_constructor]
79 // Create global points with latitude, longitude
80 val globalPoint1 = GlobalPoint(55.7558, 37.6176) // Moscow
81 val globalPoint2 = GlobalPoint(59.9311, 30.3609) // St. Petersburg
82 val globalPoint3 = GlobalPoint(55.7522, 37.6156) // Moscow center
83 println("Created global points: GP1(${globalPoint1.latitude}, ${globalPoint1.longitude}), GP2(${globalPoint2.latitude}, ${globalPoint2.longitude})")
84 // [kotlin_GlobalPoint_constructor]
86 // [kotlin_GlobalPoint_getLatitude]
88 val lat1 = globalPoint1.latitude
89 println("GlobalPoint1 latitude: $lat1")
90 // [kotlin_GlobalPoint_getLatitude]
92 // [kotlin_GlobalPoint_getLongitude]
94 val lon1 = globalPoint1.longitude
95 println("GlobalPoint1 longitude: $lon1")
96 // [kotlin_GlobalPoint_getLongitude]
98 // [kotlin_GeometryUtils_distanceBetweenGlobalPoints]
99 // Calculate distance between global points
100 val globalDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint2)
101 println("Distance between Moscow and St. Petersburg: ${"%.2f".format(globalDistance)} meters")
102 // [kotlin_GeometryUtils_distanceBetweenGlobalPoints]
104 // Calculate distance between nearby points
105 val nearbyDistance = GeometryUtils.distanceBetweenGlobalPoints(globalPoint1, globalPoint3)
106 println("Distance between Moscow points: ${"%.2f".format(nearbyDistance)} meters")
110 * Demonstrate Segment operations
112 fun demonstrateSegmentOperations() {
113 println("--- Segment Operations ---")
115 // Create points for segments
116 val start1 = Point(0.0, 0.0)
117 val end1 = Point(10.0, 10.0)
118 val start2 = Point(0.0, 10.0)
119 val end2 = Point(10.0, 0.0)
120 val testPoint = Point(5.0, 5.0)
122 // [kotlin_Segment_constructor]
124 val segment1 = Segment(start1, end1)
125 val segment2 = Segment(start2, end2)
126 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}))")
127 // [kotlin_Segment_constructor]
129 // [kotlin_Segment_getStart]
131 val segment1Start = segment1.start
132 println("Segment1 start point: (${segment1Start.x}, ${segment1Start.y})")
133 // [kotlin_Segment_getStart]
135 // [kotlin_Segment_getEnd]
137 val segment1End = segment1.end
138 println("Segment1 end point: (${segment1End.x}, ${segment1End.y})")
139 // [kotlin_Segment_getEnd]
141 // [kotlin_GeometryUtils_segmentLength]
142 // Calculate segment length
143 val length1 = GeometryUtils.segmentLength(segment1)
144 val length2 = GeometryUtils.segmentLength(segment2)
145 println("Segment1 length: ${"%.2f".format(length1)} meters")
146 println("Segment2 length: ${"%.2f".format(length2)} meters")
147 // [kotlin_GeometryUtils_segmentLength]
149 // [kotlin_GeometryUtils_segmentPointDistance]
150 // Calculate distance from segment to point
151 val distanceToPoint = GeometryUtils.segmentPointDistance(segment1, testPoint)
152 println("Distance from segment1 to test point: ${"%.2f".format(distanceToPoint)} meters")
153 // [kotlin_GeometryUtils_segmentPointDistance]
155 // [kotlin_GeometryUtils_segmentIntersectsSegment]
156 // Check if segments intersect
157 val intersects = GeometryUtils.segmentIntersectsSegment(segment1, segment2)
158 println("Segments intersect: $intersects")
159 // [kotlin_GeometryUtils_segmentIntersectsSegment]
161 // [kotlin_GeometryUtils_segmentIntersectionSegment]
162 // Calculate intersection point
163 val intersection = GeometryUtils.segmentIntersectionSegment(segment1, segment2)
164 println("Intersection point: (${"%.2f".format(intersection.x)}, ${"%.2f".format(intersection.y)})")
165 // [kotlin_GeometryUtils_segmentIntersectionSegment]
167 // [kotlin_GeometryUtils_divisionRatioBySegment]
168 // Calculate division ratio
169 val divisionRatio = GeometryUtils.divisionRatioBySegment(segment1, segment2)
170 println("Division ratio: ${"%.2f".format(divisionRatio)}")
171 // [kotlin_GeometryUtils_divisionRatioBySegment]
173 // [kotlin_GeometryUtils_getRatioPoint]
174 // Get point at specific ratio
176 val ratioPoint = GeometryUtils.getRatioPoint(segment1, ratio)
177 println("Point at ratio $ratio: (${"%.2f".format(ratioPoint.x)}, ${"%.2f".format(ratioPoint.y)})")
178 // [kotlin_GeometryUtils_getRatioPoint]
180 // [kotlin_GeometryUtils_getProjectionRatio]
181 // Calculate projection ratio
182 val projectionRatio = GeometryUtils.getProjectionRatio(segment1, testPoint)
183 println("Projection ratio: ${"%.2f".format(projectionRatio)}")
184 // [kotlin_GeometryUtils_getProjectionRatio]
188 * Demonstrate Polygon operations
190 fun demonstratePolygonOperations() {
191 println("--- Polygon Operations ---")
193 // Create points for polygon
194 val polygonPoints = listOf(
201 // [kotlin_Polygon_constructor]
203 val polygon = Polygon(polygonPoints)
204 println("Created polygon with ${polygon.points.size} points")
205 // [kotlin_Polygon_constructor]
207 // [kotlin_Polygon_getPoints]
208 // Get polygon points
209 val points = polygon.points
210 println("Polygon points: ${points.joinToString(", ") { "(${it.x}, ${it.y})" }}")
211 // [kotlin_Polygon_getPoints]
213 // [kotlin_GeometryUtils_polygonArea]
214 // Calculate polygon area
215 val area = GeometryUtils.polygonArea(polygon)
216 println("Polygon area: ${"%.2f".format(area)} square meters")
217 // [kotlin_GeometryUtils_polygonArea]
219 // [kotlin_GeometryUtils_polygonCenter]
220 // Calculate polygon center
221 val center = GeometryUtils.polygonCenter(polygon)
222 println("Polygon center: (${"%.2f".format(center.x)}, ${"%.2f".format(center.y)})")
223 // [kotlin_GeometryUtils_polygonCenter]
225 // [kotlin_GeometryUtils_polygonContainsPoint]
226 // Check if polygon contains point
227 val insidePoint = Point(5.0, 5.0)
228 val outsidePoint = Point(15.0, 15.0)
230 val containsInside = GeometryUtils.polygonContainsPoint(polygon, insidePoint)
231 val containsOutside = GeometryUtils.polygonContainsPoint(polygon, outsidePoint)
233 println("Polygon contains inside point: $containsInside")
234 println("Polygon contains outside point: $containsOutside")
235 // [kotlin_GeometryUtils_polygonContainsPoint]
237 // Create complex polygon
238 val complexPolygonPoints = listOf(
245 val complexPolygon = Polygon(complexPolygonPoints)
247 val complexArea = GeometryUtils.polygonArea(complexPolygon)
248 val complexCenter = GeometryUtils.polygonCenter(complexPolygon)
250 println("Complex polygon area: ${"%.2f".format(complexArea)} square meters")
251 println("Complex polygon center: (${"%.2f".format(complexCenter.x)}, ${"%.2f".format(complexCenter.y)})")
255 * Demonstrate Line operations
257 fun demonstrateLineOperations() {
258 println("--- Line Operations ---")
260 // Create segments for line
261 val lineSegments = listOf(
262 Segment(Point(0.0, 0.0), Point(10.0, 10.0)),
263 Segment(Point(10.0, 10.0), Point(20.0, 5.0)),
264 Segment(Point(20.0, 5.0), Point(30.0, 15.0))
267 // [kotlin_Line_constructor]
269 val line = Line(lineSegments)
270 println("Created line with ${line.segments.size} segments")
271 // [kotlin_Line_constructor]
273 // [kotlin_Line_getSegments]
275 val segments = line.segments
276 println("Line segments: ${segments.size} segments")
277 // [kotlin_Line_getSegments]
279 // Calculate total length of line
280 var totalLength = 0.0
281 for (segment in segments) {
282 totalLength += GeometryUtils.segmentLength(segment)
284 println("Total line length: ${"%.2f".format(totalLength)} meters")
286 // Create complex line
287 val complexLineSegments = listOf(
288 Segment(Point(0.0, 0.0), Point(5.0, 5.0)),
289 Segment(Point(5.0, 5.0), Point(10.0, 0.0)),
290 Segment(Point(10.0, 0.0), Point(15.0, 10.0)),
291 Segment(Point(15.0, 10.0), Point(20.0, 5.0))
293 val complexLine = Line(complexLineSegments)
295 var complexTotalLength = 0.0
296 for (segment in complexLine.segments) {
297 complexTotalLength += GeometryUtils.segmentLength(segment)
299 println("Complex line total length: ${"%.2f".format(complexTotalLength)} meters")
303 * Demonstrate advanced geometry operations
305 fun demonstrateAdvancedGeometryOperations() {
306 println("--- Advanced Geometry Operations ---")
308 // Test distance calculations with various scenarios
309 demonstrateDistanceCalculations()
311 // Test intersection scenarios
312 demonstrateIntersectionScenarios()
314 // Test polygon operations with different shapes
315 demonstratePolygonScenarios()
317 // Test projection and ratio calculations
318 demonstrateProjectionCalculations()
322 * Demonstrate distance calculations
324 fun demonstrateDistanceCalculations() {
325 println("--- Distance Calculations ---")
327 // Test point distances
328 val origin = Point(0.0, 0.0)
329 val testPoints = listOf(
330 Point(3.0, 4.0), // Should be 5.0 distance
331 Point(6.0, 8.0), // Should be 10.0 distance
332 Point(1.0, 1.0) // Should be sqrt(2) distance
335 for (testPoint in testPoints) {
336 val distance = GeometryUtils.distanceBetweenPoints(origin, testPoint)
337 println("Distance from origin to (${testPoint.x}, ${testPoint.y}): ${"%.2f".format(distance)} meters")
340 // Test global point distances
341 val moscow = GlobalPoint(55.7558, 37.6176)
342 val london = GlobalPoint(51.5074, -0.1278)
343 val tokyo = GlobalPoint(35.6762, 139.6503)
345 val moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(moscow, london)
346 val moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(moscow, tokyo)
348 println("Moscow to London: ${"%.2f".format(moscowLondon / 1000)} km")
349 println("Moscow to Tokyo: ${"%.2f".format(moscowTokyo / 1000)} km")
353 * Demonstrate intersection scenarios
355 fun demonstrateIntersectionScenarios() {
356 println("--- Intersection Scenarios ---")
358 // Parallel segments (should not intersect)
359 val parallel1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
360 val parallel2 = Segment(Point(0.0, 5.0), Point(10.0, 5.0))
362 val parallelIntersects = GeometryUtils.segmentIntersectsSegment(parallel1, parallel2)
363 println("Parallel segments intersect: $parallelIntersects")
365 // Perpendicular segments (should intersect)
366 val perpendicular1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
367 val perpendicular2 = Segment(Point(5.0, -5.0), Point(5.0, 5.0))
369 val perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(perpendicular1, perpendicular2)
370 println("Perpendicular segments intersect: $perpendicularIntersects")
372 if (perpendicularIntersects) {
373 val intersection = GeometryUtils.segmentIntersectionSegment(perpendicular1, perpendicular2)
374 println("Intersection point: (${"%.2f".format(intersection.x)}, ${"%.2f".format(intersection.y)})")
377 // Overlapping segments
378 val overlap1 = Segment(Point(0.0, 0.0), Point(10.0, 0.0))
379 val overlap2 = Segment(Point(5.0, 0.0), Point(15.0, 0.0))
381 val overlapIntersects = GeometryUtils.segmentIntersectsSegment(overlap1, overlap2)
382 println("Overlapping segments intersect: $overlapIntersects")
386 * Demonstrate polygon scenarios
388 fun demonstratePolygonScenarios() {
389 println("--- Polygon Scenarios ---")
398 val squarePolygon = Polygon(square)
400 val squareArea = GeometryUtils.polygonArea(squarePolygon)
401 val squareCenter = GeometryUtils.polygonCenter(squarePolygon)
403 println("Square area: ${"%.2f".format(squareArea)} square meters")
404 println("Square center: (${"%.2f".format(squareCenter.x)}, ${"%.2f".format(squareCenter.y)})")
407 val triangle = listOf(
412 val trianglePolygon = Polygon(triangle)
414 val triangleArea = GeometryUtils.polygonArea(trianglePolygon)
415 val triangleCenter = GeometryUtils.polygonCenter(trianglePolygon)
417 println("Triangle area: ${"%.2f".format(triangleArea)} square meters")
418 println("Triangle center: (${"%.2f".format(triangleCenter.x)}, ${"%.2f".format(triangleCenter.y)})")
420 // Test point containment
421 val insideSquare = Point(5.0, 5.0)
422 val outsideSquare = Point(15.0, 15.0)
424 val containsInside = GeometryUtils.polygonContainsPoint(squarePolygon, insideSquare)
425 val containsOutside = GeometryUtils.polygonContainsPoint(squarePolygon, outsideSquare)
427 println("Square contains (5,5): $containsInside")
428 println("Square contains (15,15): $containsOutside")
432 * Demonstrate projection calculations
434 fun demonstrateProjectionCalculations() {
435 println("--- Projection Calculations ---")
437 val segment = Segment(Point(0.0, 0.0), Point(10.0, 10.0))
439 // Test different ratio points
440 val ratios = listOf(0.0, 0.25, 0.5, 0.75, 1.0)
442 for (ratio in ratios) {
443 val ratioPoint = GeometryUtils.getRatioPoint(segment, ratio)
444 println("Ratio $ratio: (${"%.2f".format(ratioPoint.x)}, ${"%.2f".format(ratioPoint.y)})")
447 // Test projection ratio calculation
448 val testPoint = Point(5.0, 5.0)
449 val projectionRatio = GeometryUtils.getProjectionRatio(segment, testPoint)
450 println("Projection ratio for point (5,5): ${"%.2f".format(projectionRatio)}")
452 // Test distance from segment to point
453 val offSegmentPoint = Point(5.0, 0.0)
454 val distanceToOffPoint = GeometryUtils.segmentPointDistance(segment, offSegmentPoint)
455 println("Distance from segment to point (5,0): ${"%.2f".format(distanceToOffPoint)} meters")
459 * Main demonstration method
461 suspend fun runExample() {
462 println("=== GeometryUtils Example ===")
464 // Wait a bit for processing
467 println("=== Example completed ===")
472 * Function to run the example
474fun main() = runBlocking {
475 val example = GeometryUtilsExample()