4 * GeometryUtils usage example for Swift
5 * Demonstrates working with geometric calculations, points, segments, polygons, and lines
7class GeometryUtilsExample: NSObject {
11 demonstrateGeometryUtilsMethods()
15 * Demonstrate GeometryUtils methods
17 func demonstrateGeometryUtilsMethods() {
18 print("=== GeometryUtils Methods ===")
20 demonstratePointOperations()
21 demonstrateGlobalPointOperations()
22 demonstrateBoundingBoxOperations()
23 demonstrateSegmentOperations()
24 demonstratePolygonOperations()
25 demonstrateLineOperations()
26 demonstrateAdvancedGeometryOperations()
30 * Demonstrate Point operations
32 func demonstratePointOperations() {
33 print("--- Point Operations ---")
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]
46 print("Point1 X coordinate: \(x1)")
52 print("Point1 Y coordinate: \(y1)")
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]
61 // Create more points for testing
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),
70 print("Test points created for further calculations")
74 * Demonstrate BoundingBox operations
76 func demonstrateBoundingBoxOperations() {
77 print("--- BoundingBox Operations ---")
79 let bottomLeft = Point(x: 0.0, y: 0.0)
80 let topRight = Point(x: 10.0, y: 20.0)
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]
87 // [swift_BoundingBox_getBottomLeft]
88 let leftCorner = boundingBox.bottomLeft
89 print("Bottom-left corner: (\(leftCorner.x), \(leftCorner.y))")
90 // [swift_BoundingBox_getBottomLeft]
92 // [swift_BoundingBox_getTopRight]
93 let rightCorner = boundingBox.topRight
94 print("Top-right corner: (\(rightCorner.x), \(rightCorner.y))")
95 // [swift_BoundingBox_getTopRight]
99 * Demonstrate GlobalPoint operations
101 func demonstrateGlobalPointOperations() {
102 print("--- GlobalPoint Operations ---")
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]
112 // [swift_GlobalPoint_getLatitude]
114 let lat1 = globalPoint1.latitude
115 print("GlobalPoint1 latitude: \(lat1)")
116 // [swift_GlobalPoint_getLatitude]
118 // [swift_GlobalPoint_getLongitude]
120 let lon1 = globalPoint1.longitude
121 print("GlobalPoint1 longitude: \(lon1)")
122 // [swift_GlobalPoint_getLongitude]
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]
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")
136 * Demonstrate Segment operations
138 func demonstrateSegmentOperations() {
139 print("--- Segment Operations ---")
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)
148 // [swift_Segment_constructor]
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]
155 // [swift_Segment_getStart]
157 let segment1Start = segment1.start
158 print("Segment1 start point: (\(segment1Start.x), \(segment1Start.y))")
159 // [swift_Segment_getStart]
161 // [swift_Segment_getEnd]
163 let segment1End = segment1.end
164 print("Segment1 end point: (\(segment1End.x), \(segment1End.y))")
165 // [swift_Segment_getEnd]
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]
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]
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]
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]
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]
199 // [swift_GeometryUtils_getRatioPoint]
200 // Get point at specific ratio
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]
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]
214 * Demonstrate Polygon operations
216 func demonstratePolygonOperations() {
217 print("--- Polygon Operations ---")
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)
227 // [swift_Polygon_constructor]
229 let polygon = Polygon(points: polygonPoints)
230 print("Created polygon with \(polygon.points.count) points")
231 // [swift_Polygon_constructor]
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]
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]
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]
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)
257 let containsInside = GeometryUtils.polygonContainsPoint(polygon: polygon, point: insidePoint)
258 let containsOutside = GeometryUtils.polygonContainsPoint(polygon: polygon, point: outsidePoint)
260 print("Polygon contains inside point: \(containsInside)")
261 print("Polygon contains outside point: \(containsOutside)")
262 // [swift_GeometryUtils_polygonContainsPoint]
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)
272 let complexPolygon = Polygon(points: complexPolygonPoints)
274 let complexArea = GeometryUtils.polygonArea(polygon: complexPolygon)
275 let complexCenter = GeometryUtils.polygonCenter(polygon: complexPolygon)
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)))")
282 * Demonstrate Line operations
284 func demonstrateLineOperations() {
285 print("--- Line Operations ---")
287 // Create segments for line
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))
294 // [swift_Line_constructor]
296 let line = Line(segments: lineSegments)
297 print("Created line with \(line.segments.count) segments")
298 // [swift_Line_constructor]
300 // [swift_Line_getSegments]
302 let segments = line.segments
303 print("Line segments: \(segments.count) segments")
304 // [swift_Line_getSegments]
306 // Calculate total length of line
307 var totalLength = 0.0
308 for segment in segments {
309 totalLength += GeometryUtils.segmentLength(segment: segment)
311 print("Total line length: \(String(format: "%.2f", totalLength)) meters")
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))
320 let complexLine = Line(segments: complexLineSegments)
322 var complexTotalLength = 0.0
323 for segment in complexLine.segments {
324 complexTotalLength += GeometryUtils.segmentLength(segment: segment)
326 print("Complex line total length: \(String(format: "%.2f", complexTotalLength)) meters")
330 * Demonstrate advanced geometry operations
332 func demonstrateAdvancedGeometryOperations() {
333 print("--- Advanced Geometry Operations ---")
335 // Test distance calculations with various scenarios
336 demonstrateDistanceCalculations()
338 // Test intersection scenarios
339 demonstrateIntersectionScenarios()
341 // Test polygon operations with different shapes
342 demonstratePolygonScenarios()
344 // Test projection and ratio calculations
345 demonstrateProjectionCalculations()
349 * Demonstrate distance calculations
351 func demonstrateDistanceCalculations() {
352 print("--- Distance Calculations ---")
354 // Test point distances
355 let origin = Point(x: 0.0, y: 0.0)
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
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")
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)
372 let moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(from: moscow, to: london)
373 let moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(from: moscow, to: tokyo)
375 print("Moscow to London: \(String(format: "%.2f", moscowLondon / 1000)) km")
376 print("Moscow to Tokyo: \(String(format: "%.2f", moscowTokyo / 1000)) km")
380 * Demonstrate intersection scenarios
382 func demonstrateIntersectionScenarios() {
383 print("--- Intersection Scenarios ---")
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))
389 let parallelIntersects = GeometryUtils.segmentIntersectsSegment(segment1: parallel1, segment2: parallel2)
390 print("Parallel segments intersect: \(parallelIntersects)")
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))
396 let perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(segment1: perpendicular1, segment2: perpendicular2)
397 print("Perpendicular segments intersect: \(perpendicularIntersects)")
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)))")
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))
408 let overlapIntersects = GeometryUtils.segmentIntersectsSegment(segment1: overlap1, segment2: overlap2)
409 print("Overlapping segments intersect: \(overlapIntersects)")
413 * Demonstrate polygon scenarios
415 func demonstratePolygonScenarios() {
416 print("--- Polygon Scenarios ---")
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)
425 let squarePolygon = Polygon(points: square)
427 let squareArea = GeometryUtils.polygonArea(polygon: squarePolygon)
428 let squareCenter = GeometryUtils.polygonCenter(polygon: squarePolygon)
430 print("Square area: \(String(format: "%.2f", squareArea)) square meters")
431 print("Square center: (\(String(format: "%.2f", squareCenter.x)), \(String(format: "%.2f", squareCenter.y)))")
435 Point(x: 0.0, y: 0.0),
436 Point(x: 10.0, y: 0.0),
437 Point(x: 5.0, y: 10.0)
439 let trianglePolygon = Polygon(points: triangle)
441 let triangleArea = GeometryUtils.polygonArea(polygon: trianglePolygon)
442 let triangleCenter = GeometryUtils.polygonCenter(polygon: trianglePolygon)
444 print("Triangle area: \(String(format: "%.2f", triangleArea)) square meters")
445 print("Triangle center: (\(String(format: "%.2f", triangleCenter.x)), \(String(format: "%.2f", triangleCenter.y)))")
447 // Test point containment
448 let insideSquare = Point(x: 5.0, y: 5.0)
449 let outsideSquare = Point(x: 15.0, y: 15.0)
451 let containsInside = GeometryUtils.polygonContainsPoint(polygon: squarePolygon, point: insideSquare)
452 let containsOutside = GeometryUtils.polygonContainsPoint(polygon: squarePolygon, point: outsideSquare)
454 print("Square contains (5,5): \(containsInside)")
455 print("Square contains (15,15): \(containsOutside)")
459 * Demonstrate projection calculations
461 func demonstrateProjectionCalculations() {
462 print("--- Projection Calculations ---")
464 let segment = Segment(start: Point(x: 0.0, y: 0.0), end: Point(x: 10.0, y: 10.0))
466 // Test different ratio points
467 let ratios = [0.0, 0.25, 0.5, 0.75, 1.0]
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)))")
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))")
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")
486 * Main demonstration method
489 print("=== GeometryUtils Example ===")
491 // Wait a bit for processing
492 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
493 print("=== Example completed ===")
499 * Function to run the example
502 let example = GeometryUtilsExample()
505 // Keep the app running for a while to see the output
506 RunLoop.main.run(until: Date().addingTimeInterval(10.0))