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 demonstrateSegmentOperations()
23 demonstratePolygonOperations()
24 demonstrateLineOperations()
25 demonstrateAdvancedGeometryOperations()
29 * Demonstrate Point operations
31 func demonstratePointOperations() {
32 print("--- Point Operations ---")
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]
45 print("Point1 X coordinate: \(x1)")
51 print("Point1 Y coordinate: \(y1)")
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]
60 // Create more points for testing
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),
69 print("Test points created for further calculations")
73 * Demonstrate GlobalPoint operations
75 func demonstrateGlobalPointOperations() {
76 print("--- GlobalPoint Operations ---")
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]
86 // [swift_GlobalPoint_getLatitude]
88 let lat1 = globalPoint1.latitude
89 print("GlobalPoint1 latitude: \(lat1)")
90 // [swift_GlobalPoint_getLatitude]
92 // [swift_GlobalPoint_getLongitude]
94 let lon1 = globalPoint1.longitude
95 print("GlobalPoint1 longitude: \(lon1)")
96 // [swift_GlobalPoint_getLongitude]
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]
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")
110 * Demonstrate Segment operations
112 func demonstrateSegmentOperations() {
113 print("--- Segment Operations ---")
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)
122 // [swift_Segment_constructor]
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]
129 // [swift_Segment_getStart]
131 let segment1Start = segment1.start
132 print("Segment1 start point: (\(segment1Start.x), \(segment1Start.y))")
133 // [swift_Segment_getStart]
135 // [swift_Segment_getEnd]
137 let segment1End = segment1.end
138 print("Segment1 end point: (\(segment1End.x), \(segment1End.y))")
139 // [swift_Segment_getEnd]
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]
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]
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]
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]
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]
173 // [swift_GeometryUtils_getRatioPoint]
174 // Get point at specific ratio
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]
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]
188 * Demonstrate Polygon operations
190 func demonstratePolygonOperations() {
191 print("--- Polygon Operations ---")
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)
201 // [swift_Polygon_constructor]
203 let polygon = Polygon(points: polygonPoints)
204 print("Created polygon with \(polygon.points.count) points")
205 // [swift_Polygon_constructor]
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]
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]
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]
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)
231 let containsInside = GeometryUtils.polygonContainsPoint(polygon: polygon, point: insidePoint)
232 let containsOutside = GeometryUtils.polygonContainsPoint(polygon: polygon, point: outsidePoint)
234 print("Polygon contains inside point: \(containsInside)")
235 print("Polygon contains outside point: \(containsOutside)")
236 // [swift_GeometryUtils_polygonContainsPoint]
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)
246 let complexPolygon = Polygon(points: complexPolygonPoints)
248 let complexArea = GeometryUtils.polygonArea(polygon: complexPolygon)
249 let complexCenter = GeometryUtils.polygonCenter(polygon: complexPolygon)
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)))")
256 * Demonstrate Line operations
258 func demonstrateLineOperations() {
259 print("--- Line Operations ---")
261 // Create segments for line
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))
268 // [swift_Line_constructor]
270 let line = Line(segments: lineSegments)
271 print("Created line with \(line.segments.count) segments")
272 // [swift_Line_constructor]
274 // [swift_Line_getSegments]
276 let segments = line.segments
277 print("Line segments: \(segments.count) segments")
278 // [swift_Line_getSegments]
280 // Calculate total length of line
281 var totalLength = 0.0
282 for segment in segments {
283 totalLength += GeometryUtils.segmentLength(segment: segment)
285 print("Total line length: \(String(format: "%.2f", totalLength)) meters")
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))
294 let complexLine = Line(segments: complexLineSegments)
296 var complexTotalLength = 0.0
297 for segment in complexLine.segments {
298 complexTotalLength += GeometryUtils.segmentLength(segment: segment)
300 print("Complex line total length: \(String(format: "%.2f", complexTotalLength)) meters")
304 * Demonstrate advanced geometry operations
306 func demonstrateAdvancedGeometryOperations() {
307 print("--- Advanced Geometry Operations ---")
309 // Test distance calculations with various scenarios
310 demonstrateDistanceCalculations()
312 // Test intersection scenarios
313 demonstrateIntersectionScenarios()
315 // Test polygon operations with different shapes
316 demonstratePolygonScenarios()
318 // Test projection and ratio calculations
319 demonstrateProjectionCalculations()
323 * Demonstrate distance calculations
325 func demonstrateDistanceCalculations() {
326 print("--- Distance Calculations ---")
328 // Test point distances
329 let origin = Point(x: 0.0, y: 0.0)
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
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")
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)
346 let moscowLondon = GeometryUtils.distanceBetweenGlobalPoints(from: moscow, to: london)
347 let moscowTokyo = GeometryUtils.distanceBetweenGlobalPoints(from: moscow, to: tokyo)
349 print("Moscow to London: \(String(format: "%.2f", moscowLondon / 1000)) km")
350 print("Moscow to Tokyo: \(String(format: "%.2f", moscowTokyo / 1000)) km")
354 * Demonstrate intersection scenarios
356 func demonstrateIntersectionScenarios() {
357 print("--- Intersection Scenarios ---")
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))
363 let parallelIntersects = GeometryUtils.segmentIntersectsSegment(segment1: parallel1, segment2: parallel2)
364 print("Parallel segments intersect: \(parallelIntersects)")
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))
370 let perpendicularIntersects = GeometryUtils.segmentIntersectsSegment(segment1: perpendicular1, segment2: perpendicular2)
371 print("Perpendicular segments intersect: \(perpendicularIntersects)")
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)))")
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))
382 let overlapIntersects = GeometryUtils.segmentIntersectsSegment(segment1: overlap1, segment2: overlap2)
383 print("Overlapping segments intersect: \(overlapIntersects)")
387 * Demonstrate polygon scenarios
389 func demonstratePolygonScenarios() {
390 print("--- Polygon Scenarios ---")
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)
399 let squarePolygon = Polygon(points: square)
401 let squareArea = GeometryUtils.polygonArea(polygon: squarePolygon)
402 let squareCenter = GeometryUtils.polygonCenter(polygon: squarePolygon)
404 print("Square area: \(String(format: "%.2f", squareArea)) square meters")
405 print("Square center: (\(String(format: "%.2f", squareCenter.x)), \(String(format: "%.2f", squareCenter.y)))")
409 Point(x: 0.0, y: 0.0),
410 Point(x: 10.0, y: 0.0),
411 Point(x: 5.0, y: 10.0)
413 let trianglePolygon = Polygon(points: triangle)
415 let triangleArea = GeometryUtils.polygonArea(polygon: trianglePolygon)
416 let triangleCenter = GeometryUtils.polygonCenter(polygon: trianglePolygon)
418 print("Triangle area: \(String(format: "%.2f", triangleArea)) square meters")
419 print("Triangle center: (\(String(format: "%.2f", triangleCenter.x)), \(String(format: "%.2f", triangleCenter.y)))")
421 // Test point containment
422 let insideSquare = Point(x: 5.0, y: 5.0)
423 let outsideSquare = Point(x: 15.0, y: 15.0)
425 let containsInside = GeometryUtils.polygonContainsPoint(polygon: squarePolygon, point: insideSquare)
426 let containsOutside = GeometryUtils.polygonContainsPoint(polygon: squarePolygon, point: outsideSquare)
428 print("Square contains (5,5): \(containsInside)")
429 print("Square contains (15,15): \(containsOutside)")
433 * Demonstrate projection calculations
435 func demonstrateProjectionCalculations() {
436 print("--- Projection Calculations ---")
438 let segment = Segment(start: Point(x: 0.0, y: 0.0), end: Point(x: 10.0, y: 10.0))
440 // Test different ratio points
441 let ratios = [0.0, 0.25, 0.5, 0.75, 1.0]
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)))")
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))")
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")
460 * Main demonstration method
463 print("=== GeometryUtils Example ===")
465 // Wait a bit for processing
466 DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
467 print("=== Example completed ===")
473 * Function to run the example
476 let example = GeometryUtilsExample()
479 // Keep the app running for a while to see the output
480 RunLoop.main.run(until: Date().addingTimeInterval(10.0))