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