21 NSLog(
@"=== GeometryUtils Methods ===");
23 [
self demonstratePointOperations];
24 [
self demonstrateGlobalPointOperations];
25 [
self demonstrateSegmentOperations];
26 [
self demonstratePolygonOperations];
27 [
self demonstrateLineOperations];
28 [
self demonstrateAdvancedGeometryOperations];
34- (void)demonstratePointOperations {
35 NSLog(
@"--- Point Operations ---");
39 NCPoint *point1 = [[NCPoint alloc] initWithX:10.0 y:20.0];
40 NCPoint *point2 = [[NCPoint alloc] initWithX:30.0 y:40.0];
41 NCPoint *point3 = [[NCPoint alloc] initWithX:50.0 y:60.0];
42 NSLog(
@"Created points: P1(%.1f, %.1f), P2(%.1f, %.1f), P3(%.1f, %.1f)",
43 point1.x, point1.y, point2.x, point2.y, point3.x, point3.y);
49 NSLog(
@"Point1 X coordinate: %.1f", x1);
55 NSLog(
@"Point1 Y coordinate: %.1f", y1);
60 double distance = [NCGeometryUtils distanceBetweenPointsWithFrom:point1 to:point2];
61 NSLog(
@"Distance between P1 and P2: %.2f meters", distance);
65 NSArray<NCPoint *> *testPoints = @[
66 [[NCPoint alloc] initWithX:0.0 y:0.0],
67 [[NCPoint alloc] initWithX:10.0 y:0.0],
68 [[NCPoint alloc] initWithX:10.0 y:10.0],
69 [[NCPoint alloc] initWithX:0.0 y:10.0],
70 [[NCPoint alloc] initWithX:5.0 y:5.0]
73 NSLog(
@"Test points created for further calculations");
79- (void)demonstrateGlobalPointOperations {
80 NSLog(
@"--- GlobalPoint Operations ---");
84 NCGlobalPoint *globalPoint1 = [[NCGlobalPoint alloc] initWithLatitude:55.7558 longitude:37.6176];
85 NCGlobalPoint *globalPoint2 = [[NCGlobalPoint alloc] initWithLatitude:59.9311 longitude:30.3609];
86 NCGlobalPoint *globalPoint3 = [[NCGlobalPoint alloc] initWithLatitude:55.7522 longitude:37.6156];
87 NSLog(
@"Created global points: GP1(%.4f, %.4f), GP2(%.4f, %.4f)",
88 globalPoint1.latitude, globalPoint1.longitude, globalPoint2.latitude, globalPoint2.longitude);
93 double lat1 = globalPoint1.latitude;
94 NSLog(
@"GlobalPoint1 latitude: %.4f", lat1);
99 double lon1 = globalPoint1.longitude;
100 NSLog(
@"GlobalPoint1 longitude: %.4f", lon1);
105 double globalDistance = [NCGeometryUtils distanceBetweenGlobalPointsWithFrom:globalPoint1 to:globalPoint2];
106 NSLog(
@"Distance between Moscow and St. Petersburg: %.2f meters", globalDistance);
110 double nearbyDistance = [NCGeometryUtils distanceBetweenGlobalPointsWithFrom:globalPoint1 to:globalPoint3];
111 NSLog(
@"Distance between Moscow points: %.2f meters", nearbyDistance);
117- (void)demonstrateSegmentOperations {
118 NSLog(
@"--- Segment Operations ---");
121 NCPoint *start1 = [[NCPoint alloc] initWithX:0.0 y:0.0];
122 NCPoint *end1 = [[NCPoint alloc] initWithX:10.0 y:10.0];
123 NCPoint *start2 = [[NCPoint alloc] initWithX:0.0 y:10.0];
124 NCPoint *end2 = [[NCPoint alloc] initWithX:10.0 y:0.0];
125 NCPoint *testPoint = [[NCPoint alloc] initWithX:5.0 y:5.0];
129 NCSegment *segment1 = [[NCSegment alloc] initWithStart:start1 end:end1];
130 NCSegment *segment2 = [[NCSegment alloc] initWithStart:start2 end:end2];
131 NSLog(
@"Created segments: S1((%.1f, %.1f) -> (%.1f, %.1f)), S2((%.1f, %.1f) -> (%.1f, %.1f))",
132 segment1.start.x, segment1.start.y, segment1.end.x, segment1.end.y,
133 segment2.start.x, segment2.start.y, segment2.end.x, segment2.end.y);
138 NCPoint *segment1Start = segment1.start;
139 NSLog(
@"Segment1 start point: (%.1f, %.1f)", segment1Start.x, segment1Start.y);
144 NCPoint *segment1End = segment1.end;
145 NSLog(
@"Segment1 end point: (%.1f, %.1f)", segment1End.x, segment1End.y);
150 double length1 = [NCGeometryUtils segmentLengthWithSegment:segment1];
151 double length2 = [NCGeometryUtils segmentLengthWithSegment:segment2];
152 NSLog(
@"Segment1 length: %.2f meters", length1);
153 NSLog(
@"Segment2 length: %.2f meters", length2);
158 double distanceToPoint = [NCGeometryUtils segmentPointDistanceWithSegment:segment1 point:testPoint];
159 NSLog(
@"Distance from segment1 to test point: %.2f meters", distanceToPoint);
164 BOOL intersects = [NCGeometryUtils segmentIntersectsSegmentWithSegment1:segment1 segment2:segment2];
165 NSLog(
@"Segments intersect: %@", intersects ?
@"YES" :
@"NO");
170 NCPoint *intersection = [NCGeometryUtils segmentIntersectionSegmentWithSegment1:segment1 segment2:segment2];
171 NSLog(
@"Intersection point: (%.2f, %.2f)", intersection.x, intersection.y);
176 double divisionRatio = [NCGeometryUtils divisionRatioBySegmentWithSegment1:segment1 segment2:segment2];
177 NSLog(
@"Division ratio: %.2f", divisionRatio);
183 NCPoint *ratioPoint = [NCGeometryUtils getRatioPointWithSegment:segment1 r:ratio];
184 NSLog(
@"Point at ratio %.1f: (%.2f, %.2f)", ratio, ratioPoint.x, ratioPoint.y);
189 double projectionRatio = [NCGeometryUtils getProjectionRatioWithSegment:segment1 point:testPoint];
190 NSLog(
@"Projection ratio: %.2f", projectionRatio);
197- (void)demonstratePolygonOperations {
198 NSLog(
@"--- Polygon Operations ---");
201 NSArray<NCPoint *> *polygonPoints = @[
202 [[NCPoint alloc] initWithX:0.0 y:0.0],
203 [[NCPoint alloc] initWithX:10.0 y:0.0],
204 [[NCPoint alloc] initWithX:10.0 y:10.0],
205 [[NCPoint alloc] initWithX:0.0 y:10.0]
210 NCPolygon *polygon = [[NCPolygon alloc] initWithPoints:polygonPoints];
211 NSLog(
@"Created polygon with %lu points", (
unsigned long)polygon.points.count);
216 NSArray<NCPoint *> *points = polygon.points;
217 NSMutableString *pointsString = [NSMutableString string];
218 for (NCPoint *point in points) {
219 if (pointsString.length > 0) {
220 [pointsString appendString:@", "];
222 [pointsString appendFormat:@"(%.1f, %.1f)", point.x, point.y];
224 NSLog(
@"Polygon points: %@", pointsString);
229 double area = [NCGeometryUtils polygonAreaWithPolygon:polygon];
230 NSLog(
@"Polygon area: %.2f square meters", area);
235 NCPoint *center = [NCGeometryUtils polygonCenterWithPolygon:polygon];
236 NSLog(
@"Polygon center: (%.2f, %.2f)", center.x, center.y);
241 NCPoint *insidePoint = [[NCPoint alloc] initWithX:5.0 y:5.0];
242 NCPoint *outsidePoint = [[NCPoint alloc] initWithX:15.0 y:15.0];
244 BOOL containsInside = [NCGeometryUtils polygonContainsPointWithPolygon:polygon point:insidePoint];
245 BOOL containsOutside = [NCGeometryUtils polygonContainsPointWithPolygon:polygon point:outsidePoint];
247 NSLog(
@"Polygon contains inside point: %@", containsInside ?
@"YES" :
@"NO");
248 NSLog(
@"Polygon contains outside point: %@", containsOutside ?
@"YES" :
@"NO");
252 NSArray<NCPoint *> *complexPolygonPoints = @[
253 [[NCPoint alloc] initWithX:0.0 y:0.0],
254 [[NCPoint alloc] initWithX:20.0 y:0.0],
255 [[NCPoint alloc] initWithX:20.0 y:20.0],
256 [[NCPoint alloc] initWithX:10.0 y:10.0],
257 [[NCPoint alloc] initWithX:0.0 y:20.0]
259 NCPolygon *complexPolygon = [[NCPolygon alloc] initWithPoints:complexPolygonPoints];
261 double complexArea = [NCGeometryUtils polygonAreaWithPolygon:complexPolygon];
262 NCPoint *complexCenter = [NCGeometryUtils polygonCenterWithPolygon:complexPolygon];
264 NSLog(
@"Complex polygon area: %.2f square meters", complexArea);
265 NSLog(
@"Complex polygon center: (%.2f, %.2f)", complexCenter.x, complexCenter.y);
271- (void)demonstrateLineOperations {
272 NSLog(
@"--- Line Operations ---");
275 NSArray<NCSegment *> *lineSegments = @[
276 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
277 end:[[NCPoint alloc] initWithX:10.0 y:10.0]],
278 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:10.0 y:10.0]
279 end:[[NCPoint alloc] initWithX:20.0 y:5.0]],
280 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:20.0 y:5.0]
281 end:[[NCPoint alloc] initWithX:30.0 y:15.0]]
286 NCLine *line = [[NCLine alloc] initWithSegments:lineSegments];
287 NSLog(
@"Created line with %lu segments", (
unsigned long)line.segments.count);
292 NSArray<NCSegment *> *segments = line.segments;
293 NSLog(
@"Line segments: %lu segments", (
unsigned long)segments.count);
297 double totalLength = 0.0;
298 for (NCSegment *segment in segments) {
299 totalLength += [NCGeometryUtils segmentLengthWithSegment:segment];
301 NSLog(
@"Total line length: %.2f meters", totalLength);
304 NSArray<NCSegment *> *complexLineSegments = @[
305 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
306 end:[[NCPoint alloc] initWithX:5.0 y:5.0]],
307 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:5.0 y:5.0]
308 end:[[NCPoint alloc] initWithX:10.0 y:0.0]],
309 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:10.0 y:0.0]
310 end:[[NCPoint alloc] initWithX:15.0 y:10.0]],
311 [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:15.0 y:10.0]
312 end:[[NCPoint alloc] initWithX:20.0 y:5.0]]
314 NCLine *complexLine = [[NCLine alloc] initWithSegments:complexLineSegments];
316 double complexTotalLength = 0.0;
317 for (NCSegment *segment in complexLine.segments) {
318 complexTotalLength += [NCGeometryUtils segmentLengthWithSegment:segment];
320 NSLog(
@"Complex line total length: %.2f meters", complexTotalLength);
326- (void)demonstrateAdvancedGeometryOperations {
327 NSLog(
@"--- Advanced Geometry Operations ---");
330 [
self demonstrateDistanceCalculations];
333 [
self demonstrateIntersectionScenarios];
336 [
self demonstratePolygonScenarios];
339 [
self demonstrateProjectionCalculations];
345- (void)demonstrateDistanceCalculations {
346 NSLog(
@"--- Distance Calculations ---");
349 NCPoint *origin = [[NCPoint alloc] initWithX:0.0 y:0.0];
350 NSArray<NCPoint *> *testPoints = @[
351 [[NCPoint alloc] initWithX:3.0 y:4.0],
352 [[NCPoint alloc] initWithX:6.0 y:8.0],
353 [[NCPoint alloc] initWithX:1.0 y:1.0]
356 for (NCPoint *testPoint in testPoints) {
357 double distance = [NCGeometryUtils distanceBetweenPointsWithFrom:origin to:testPoint];
358 NSLog(
@"Distance from origin to (%.1f, %.1f): %.2f meters", testPoint.x, testPoint.y, distance);
362 NCGlobalPoint *moscow = [[NCGlobalPoint alloc] initWithLatitude:55.7558 longitude:37.6176];
363 NCGlobalPoint *london = [[NCGlobalPoint alloc] initWithLatitude:51.5074 longitude:-0.1278];
364 NCGlobalPoint *tokyo = [[NCGlobalPoint alloc] initWithLatitude:35.6762 longitude:139.6503];
366 double moscowLondon = [NCGeometryUtils distanceBetweenGlobalPointsWithFrom:moscow to:london];
367 double moscowTokyo = [NCGeometryUtils distanceBetweenGlobalPointsWithFrom:moscow to:tokyo];
369 NSLog(
@"Moscow to London: %.2f km", moscowLondon / 1000);
370 NSLog(
@"Moscow to Tokyo: %.2f km", moscowTokyo / 1000);
376- (void)demonstrateIntersectionScenarios {
377 NSLog(
@"--- Intersection Scenarios ---");
380 NCSegment *parallel1 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
381 end:[[NCPoint alloc] initWithX:10.0 y:0.0]];
382 NCSegment *parallel2 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:5.0]
383 end:[[NCPoint alloc] initWithX:10.0 y:5.0]];
385 BOOL parallelIntersects = [NCGeometryUtils segmentIntersectsSegmentWithSegment1:parallel1 segment2:parallel2];
386 NSLog(
@"Parallel segments intersect: %@", parallelIntersects ?
@"YES" :
@"NO");
389 NCSegment *perpendicular1 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
390 end:[[NCPoint alloc] initWithX:10.0 y:0.0]];
391 NCSegment *perpendicular2 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:5.0 y:-5.0]
392 end:[[NCPoint alloc] initWithX:5.0 y:5.0]];
394 BOOL perpendicularIntersects = [NCGeometryUtils segmentIntersectsSegmentWithSegment1:perpendicular1 segment2:perpendicular2];
395 NSLog(
@"Perpendicular segments intersect: %@", perpendicularIntersects ?
@"YES" :
@"NO");
397 if (perpendicularIntersects) {
398 NCPoint *intersection = [NCGeometryUtils segmentIntersectionSegmentWithSegment1:perpendicular1 segment2:perpendicular2];
399 NSLog(
@"Intersection point: (%.2f, %.2f)", intersection.x, intersection.y);
403 NCSegment *overlap1 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
404 end:[[NCPoint alloc] initWithX:10.0 y:0.0]];
405 NCSegment *overlap2 = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:5.0 y:0.0]
406 end:[[NCPoint alloc] initWithX:15.0 y:0.0]];
408 BOOL overlapIntersects = [NCGeometryUtils segmentIntersectsSegmentWithSegment1:overlap1 segment2:overlap2];
409 NSLog(
@"Overlapping segments intersect: %@", overlapIntersects ?
@"YES" :
@"NO");
415- (void)demonstratePolygonScenarios {
416 NSLog(
@"--- Polygon Scenarios ---");
419 NSArray<NCPoint *> *square = @[
420 [[NCPoint alloc] initWithX:0.0 y:0.0],
421 [[NCPoint alloc] initWithX:10.0 y:0.0],
422 [[NCPoint alloc] initWithX:10.0 y:10.0],
423 [[NCPoint alloc] initWithX:0.0 y:10.0]
425 NCPolygon *squarePolygon = [[NCPolygon alloc] initWithPoints:square];
427 double squareArea = [NCGeometryUtils polygonAreaWithPolygon:squarePolygon];
428 NCPoint *squareCenter = [NCGeometryUtils polygonCenterWithPolygon:squarePolygon];
430 NSLog(
@"Square area: %.2f square meters", squareArea);
431 NSLog(
@"Square center: (%.2f, %.2f)", squareCenter.x, squareCenter.y);
434 NSArray<NCPoint *> *triangle = @[
435 [[NCPoint alloc] initWithX:0.0 y:0.0],
436 [[NCPoint alloc] initWithX:10.0 y:0.0],
437 [[NCPoint alloc] initWithX:5.0 y:10.0]
439 NCPolygon *trianglePolygon = [[NCPolygon alloc] initWithPoints:triangle];
441 double triangleArea = [NCGeometryUtils polygonAreaWithPolygon:trianglePolygon];
442 NCPoint *triangleCenter = [NCGeometryUtils polygonCenterWithPolygon:trianglePolygon];
444 NSLog(
@"Triangle area: %.2f square meters", triangleArea);
445 NSLog(
@"Triangle center: (%.2f, %.2f)", triangleCenter.x, triangleCenter.y);
448 NCPoint *insideSquare = [[NCPoint alloc] initWithX:5.0 y:5.0];
449 NCPoint *outsideSquare = [[NCPoint alloc] initWithX:15.0 y:15.0];
451 BOOL containsInside = [NCGeometryUtils polygonContainsPointWithPolygon:squarePolygon point:insideSquare];
452 BOOL containsOutside = [NCGeometryUtils polygonContainsPointWithPolygon:squarePolygon point:outsideSquare];
454 NSLog(
@"Square contains (5,5): %@", containsInside ?
@"YES" :
@"NO");
455 NSLog(
@"Square contains (15,15): %@", containsOutside ?
@"YES" :
@"NO");
461- (void)demonstrateProjectionCalculations {
462 NSLog(
@"--- Projection Calculations ---");
464 NCSegment *segment = [[NCSegment alloc] initWithStart:[[NCPoint alloc] initWithX:0.0 y:0.0]
465 end:[[NCPoint alloc] initWithX:10.0 y:10.0]];
468 NSArray<NSNumber *> *ratios = @[@0.0, @0.25, @0.5, @0.75, @1.0];
470 for (NSNumber *ratioNumber in ratios) {
471 double ratio = ratioNumber.doubleValue;
472 NCPoint *ratioPoint = [NCGeometryUtils getRatioPointWithSegment:segment r:ratio];
473 NSLog(
@"Ratio %.2f: (%.2f, %.2f)", ratio, ratioPoint.x, ratioPoint.y);
477 NCPoint *testPoint = [[NCPoint alloc] initWithX:5.0 y:5.0];
478 double projectionRatio = [NCGeometryUtils getProjectionRatioWithSegment:segment point:testPoint];
479 NSLog(
@"Projection ratio for point (5,5): %.2f", projectionRatio);
482 NCPoint *offSegmentPoint = [[NCPoint alloc] initWithX:5.0 y:0.0];
483 double distanceToOffPoint = [NCGeometryUtils segmentPointDistanceWithSegment:segment point:offSegmentPoint];
484 NSLog(
@"Distance from segment to point (5,0): %.2f meters", distanceToOffPoint);
491 NSLog(
@"=== GeometryUtils Example ===");
494 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
495 NSLog(
@"=== Example completed ===");
504int main(
int argc,
const char * argv[]) {
507 [example runExample];
510 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];