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