21 NSLog(
@"=== GeometryUtils Methods ===");
23 [
self demonstratePointOperations];
24 [
self demonstrateGlobalPointOperations];
25 [
self demonstrateBoundingBoxOperations];
26 [
self demonstrateSegmentOperations];
27 [
self demonstratePolygonOperations];
28 [
self demonstrateLineOperations];
29 [
self demonstrateAdvancedGeometryOperations];
35- (void)demonstratePointOperations {
36 NSLog(
@"--- Point Operations ---");
43 NSLog(
@"Created points: P1(%.1f, %.1f), P2(%.1f, %.1f), P3(%.1f, %.1f)",
44 point1.
x, point1.
y, point2.
x, point2.
y, point3.
x, point3.
y);
50 NSLog(
@"Point1 X coordinate: %.1f", x1);
56 NSLog(
@"Point1 Y coordinate: %.1f", y1);
61 double distance = [
NCGeometryUtils distanceBetweenPointsWithFrom:point1 to:point2];
62 NSLog(
@"Distance between P1 and P2: %.2f meters", distance);
66 NSArray<NCPoint *> *testPoints = @[
67 [[
NCPoint alloc] initWithX:0.0 y:0.0],
68 [[
NCPoint alloc] initWithX:10.0 y:0.0],
69 [[
NCPoint alloc] initWithX:10.0 y:10.0],
70 [[
NCPoint alloc] initWithX:0.0 y:10.0],
71 [[
NCPoint alloc] initWithX:5.0 y:5.0]
74 NSLog(
@"Test points created for further calculations");
80- (void)demonstrateBoundingBoxOperations {
81 NSLog(
@"--- BoundingBox Operations ---");
83 NCPoint *bottomLeft = [[
NCPoint alloc] initWithX:0.0 y:0.0];
84 NCPoint *topRight = [[
NCPoint alloc] initWithX:10.0 y:20.0];
87 NCBoundingBox *boundingBox = [[
NCBoundingBox alloc] initWithBottomLeft:bottomLeft topRight:topRight];
88 NSLog(
@"Created bounding box: bottomLeft(%.1f, %.1f), topRight(%.1f, %.1f)",
95 NSLog(
@"Bottom-left corner: (%.1f, %.1f)", leftCorner.
x, leftCorner.
y);
99 NCPoint *rightCorner = boundingBox.
topRight;
100 NSLog(
@"Top-right corner: (%.1f, %.1f)", rightCorner.
x, rightCorner.
y);
107- (void)demonstrateGlobalPointOperations {
108 NSLog(
@"--- GlobalPoint Operations ---");
112 NCGlobalPoint *globalPoint1 = [[
NCGlobalPoint alloc] initWithLatitude:55.7558 longitude:37.6176];
113 NCGlobalPoint *globalPoint2 = [[
NCGlobalPoint alloc] initWithLatitude:59.9311 longitude:30.3609];
114 NCGlobalPoint *globalPoint3 = [[
NCGlobalPoint alloc] initWithLatitude:55.7522 longitude:37.6156];
115 NSLog(
@"Created global points: GP1(%.4f, %.4f), GP2(%.4f, %.4f)",
121 double lat1 = globalPoint1.
latitude;
122 NSLog(
@"GlobalPoint1 latitude: %.4f", lat1);
128 NSLog(
@"GlobalPoint1 longitude: %.4f", lon1);
133 double globalDistance = [
NCGeometryUtils distanceBetweenGlobalPointsWithFrom:globalPoint1 to:globalPoint2];
134 NSLog(
@"Distance between Moscow and St. Petersburg: %.2f meters", globalDistance);
138 double nearbyDistance = [
NCGeometryUtils distanceBetweenGlobalPointsWithFrom:globalPoint1 to:globalPoint3];
139 NSLog(
@"Distance between Moscow points: %.2f meters", nearbyDistance);
145- (void)demonstrateSegmentOperations {
146 NSLog(
@"--- Segment Operations ---");
149 NCPoint *start1 = [[
NCPoint alloc] initWithX:0.0 y:0.0];
150 NCPoint *end1 = [[
NCPoint alloc] initWithX:10.0 y:10.0];
151 NCPoint *start2 = [[
NCPoint alloc] initWithX:0.0 y:10.0];
152 NCPoint *end2 = [[
NCPoint alloc] initWithX:10.0 y:0.0];
153 NCPoint *testPoint = [[
NCPoint alloc] initWithX:5.0 y:5.0];
157 NCSegment *segment1 = [[
NCSegment alloc] initWithStart:start1 end:end1];
158 NCSegment *segment2 = [[
NCSegment alloc] initWithStart:start2 end:end2];
159 NSLog(
@"Created segments: S1((%.1f, %.1f) -> (%.1f, %.1f)), S2((%.1f, %.1f) -> (%.1f, %.1f))",
166 NCPoint *segment1Start = segment1.
start;
167 NSLog(
@"Segment1 start point: (%.1f, %.1f)", segment1Start.
x, segment1Start.
y);
172 NCPoint *segment1End = segment1.
end;
173 NSLog(
@"Segment1 end point: (%.1f, %.1f)", segment1End.
x, segment1End.
y);
180 NSLog(
@"Segment1 length: %.2f meters", length1);
181 NSLog(
@"Segment2 length: %.2f meters", length2);
186 double distanceToPoint = [
NCGeometryUtils segmentPointDistanceWithSegment:segment1 point:testPoint];
187 NSLog(
@"Distance from segment1 to test point: %.2f meters", distanceToPoint);
192 BOOL intersects = [
NCGeometryUtils segmentIntersectsSegmentWithSegment1:segment1 segment2:segment2];
193 NSLog(
@"Segments intersect: %@", intersects ?
@"YES" :
@"NO");
198 NCPoint *intersection = [
NCGeometryUtils segmentIntersectionSegmentWithSegment1:segment1 segment2:segment2];
199 NSLog(
@"Intersection point: (%.2f, %.2f)", intersection.
x, intersection.
y);
204 double divisionRatio = [
NCGeometryUtils divisionRatioBySegmentWithSegment1:segment1 segment2:segment2];
205 NSLog(
@"Division ratio: %.2f", divisionRatio);
211 NCPoint *ratioPoint = [
NCGeometryUtils getRatioPointWithSegment:segment1 r:ratio];
212 NSLog(
@"Point at ratio %.1f: (%.2f, %.2f)", ratio, ratioPoint.
x, ratioPoint.
y);
217 double projectionRatio = [
NCGeometryUtils getProjectionRatioWithSegment:segment1 point:testPoint];
218 NSLog(
@"Projection ratio: %.2f", projectionRatio);
225- (void)demonstratePolygonOperations {
226 NSLog(
@"--- Polygon Operations ---");
229 NSArray<NCPoint *> *polygonPoints = @[
230 [[
NCPoint alloc] initWithX:0.0 y:0.0],
231 [[
NCPoint alloc] initWithX:10.0 y:0.0],
232 [[
NCPoint alloc] initWithX:10.0 y:10.0],
233 [[
NCPoint alloc] initWithX:0.0 y:10.0]
238 NCPolygon *polygon = [[
NCPolygon alloc] initWithPoints:polygonPoints];
239 NSLog(
@"Created polygon with %lu points", (
unsigned long)polygon.
points.count);
244 NSArray<NCPoint *> *points = polygon.
points;
245 NSMutableString *pointsString = [NSMutableString string];
246 for (NCPoint *point in points) {
247 if (pointsString.length > 0) {
248 [pointsString appendString:@", "];
250 [pointsString appendFormat:@"(%.1f, %.1f)", point.x, point.y];
252 NSLog(
@"Polygon points: %@", pointsString);
258 NSLog(
@"Polygon area: %.2f square meters", area);
264 NSLog(
@"Polygon center: (%.2f, %.2f)", center.
x, center.
y);
269 NCPoint *insidePoint = [[
NCPoint alloc] initWithX:5.0 y:5.0];
270 NCPoint *outsidePoint = [[
NCPoint alloc] initWithX:15.0 y:15.0];
272 BOOL containsInside = [
NCGeometryUtils polygonContainsPointWithPolygon:polygon point:insidePoint];
273 BOOL containsOutside = [
NCGeometryUtils polygonContainsPointWithPolygon:polygon point:outsidePoint];
275 NSLog(
@"Polygon contains inside point: %@", containsInside ?
@"YES" :
@"NO");
276 NSLog(
@"Polygon contains outside point: %@", containsOutside ?
@"YES" :
@"NO");
280 NSArray<NCPoint *> *complexPolygonPoints = @[
281 [[
NCPoint alloc] initWithX:0.0 y:0.0],
282 [[
NCPoint alloc] initWithX:20.0 y:0.0],
283 [[
NCPoint alloc] initWithX:20.0 y:20.0],
284 [[
NCPoint alloc] initWithX:10.0 y:10.0],
285 [[
NCPoint alloc] initWithX:0.0 y:20.0]
287 NCPolygon *complexPolygon = [[
NCPolygon alloc] initWithPoints:complexPolygonPoints];
289 double complexArea = [
NCGeometryUtils polygonAreaWithPolygon:complexPolygon];
290 NCPoint *complexCenter = [
NCGeometryUtils polygonCenterWithPolygon:complexPolygon];
292 NSLog(
@"Complex polygon area: %.2f square meters", complexArea);
293 NSLog(
@"Complex polygon center: (%.2f, %.2f)", complexCenter.
x, complexCenter.
y);
299- (void)demonstrateLineOperations {
300 NSLog(
@"--- Line Operations ---");
303 NSArray<NCSegment *> *lineSegments = @[
305 end:[[
NCPoint alloc] initWithX:10.0 y:10.0]],
307 end:[[
NCPoint alloc] initWithX:20.0 y:5.0]],
309 end:[[
NCPoint alloc] initWithX:30.0 y:15.0]]
314 NCLine *line = [[
NCLine alloc] initWithSegments:lineSegments];
315 NSLog(
@"Created line with %lu segments", (
unsigned long)line.
segments.count);
320 NSArray<NCSegment *> *segments = line.
segments;
321 NSLog(
@"Line segments: %lu segments", (
unsigned long)segments.count);
325 double totalLength = 0.0;
326 for (NCSegment *segment in segments) {
327 totalLength += [NCGeometryUtils segmentLengthWithSegment:segment];
329 NSLog(
@"Total line length: %.2f meters", totalLength);
332 NSArray<NCSegment *> *complexLineSegments = @[
334 end:[[
NCPoint alloc] initWithX:5.0 y:5.0]],
336 end:[[
NCPoint alloc] initWithX:10.0 y:0.0]],
338 end:[[
NCPoint alloc] initWithX:15.0 y:10.0]],
340 end:[[
NCPoint alloc] initWithX:20.0 y:5.0]]
342 NCLine *complexLine = [[
NCLine alloc] initWithSegments:complexLineSegments];
344 double complexTotalLength = 0.0;
345 for (NCSegment *segment in complexLine.segments) {
346 complexTotalLength += [NCGeometryUtils segmentLengthWithSegment:segment];
348 NSLog(
@"Complex line total length: %.2f meters", complexTotalLength);
354- (void)demonstrateAdvancedGeometryOperations {
355 NSLog(
@"--- Advanced Geometry Operations ---");
358 [
self demonstrateDistanceCalculations];
361 [
self demonstrateIntersectionScenarios];
364 [
self demonstratePolygonScenarios];
367 [
self demonstrateProjectionCalculations];
373- (void)demonstrateDistanceCalculations {
374 NSLog(
@"--- Distance Calculations ---");
377 NCPoint *origin = [[
NCPoint alloc] initWithX:0.0 y:0.0];
378 NSArray<NCPoint *> *testPoints = @[
379 [[
NCPoint alloc] initWithX:3.0 y:4.0],
380 [[
NCPoint alloc] initWithX:6.0 y:8.0],
381 [[
NCPoint alloc] initWithX:1.0 y:1.0]
384 for (NCPoint *testPoint in testPoints) {
385 double distance = [
NCGeometryUtils distanceBetweenPointsWithFrom:origin to:testPoint];
386 NSLog(
@"Distance from origin to (%.1f, %.1f): %.2f meters", testPoint.
x, testPoint.
y, distance);
390 NCGlobalPoint *moscow = [[
NCGlobalPoint alloc] initWithLatitude:55.7558 longitude:37.6176];
391 NCGlobalPoint *london = [[
NCGlobalPoint alloc] initWithLatitude:51.5074 longitude:-0.1278];
392 NCGlobalPoint *tokyo = [[
NCGlobalPoint alloc] initWithLatitude:35.6762 longitude:139.6503];
394 double moscowLondon = [
NCGeometryUtils distanceBetweenGlobalPointsWithFrom:moscow to:london];
395 double moscowTokyo = [
NCGeometryUtils distanceBetweenGlobalPointsWithFrom:moscow to:tokyo];
397 NSLog(
@"Moscow to London: %.2f km", moscowLondon / 1000);
398 NSLog(
@"Moscow to Tokyo: %.2f km", moscowTokyo / 1000);
404- (void)demonstrateIntersectionScenarios {
405 NSLog(
@"--- Intersection Scenarios ---");
408 NCSegment *parallel1 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:0.0 y:0.0]
409 end:[[
NCPoint alloc] initWithX:10.0 y:0.0]];
410 NCSegment *parallel2 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:0.0 y:5.0]
411 end:[[
NCPoint alloc] initWithX:10.0 y:5.0]];
413 BOOL parallelIntersects = [
NCGeometryUtils segmentIntersectsSegmentWithSegment1:parallel1 segment2:parallel2];
414 NSLog(
@"Parallel segments intersect: %@", parallelIntersects ?
@"YES" :
@"NO");
417 NCSegment *perpendicular1 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:0.0 y:0.0]
418 end:[[
NCPoint alloc] initWithX:10.0 y:0.0]];
419 NCSegment *perpendicular2 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:5.0 y:-5.0]
420 end:[[
NCPoint alloc] initWithX:5.0 y:5.0]];
422 BOOL perpendicularIntersects = [
NCGeometryUtils segmentIntersectsSegmentWithSegment1:perpendicular1 segment2:perpendicular2];
423 NSLog(
@"Perpendicular segments intersect: %@", perpendicularIntersects ?
@"YES" :
@"NO");
425 if (perpendicularIntersects) {
426 NCPoint *intersection = [
NCGeometryUtils segmentIntersectionSegmentWithSegment1:perpendicular1 segment2:perpendicular2];
427 NSLog(
@"Intersection point: (%.2f, %.2f)", intersection.
x, intersection.
y);
431 NCSegment *overlap1 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:0.0 y:0.0]
432 end:[[
NCPoint alloc] initWithX:10.0 y:0.0]];
433 NCSegment *overlap2 = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:5.0 y:0.0]
434 end:[[
NCPoint alloc] initWithX:15.0 y:0.0]];
436 BOOL overlapIntersects = [
NCGeometryUtils segmentIntersectsSegmentWithSegment1:overlap1 segment2:overlap2];
437 NSLog(
@"Overlapping segments intersect: %@", overlapIntersects ?
@"YES" :
@"NO");
443- (void)demonstratePolygonScenarios {
444 NSLog(
@"--- Polygon Scenarios ---");
447 NSArray<NCPoint *> *square = @[
448 [[
NCPoint alloc] initWithX:0.0 y:0.0],
449 [[
NCPoint alloc] initWithX:10.0 y:0.0],
450 [[
NCPoint alloc] initWithX:10.0 y:10.0],
451 [[
NCPoint alloc] initWithX:0.0 y:10.0]
453 NCPolygon *squarePolygon = [[
NCPolygon alloc] initWithPoints:square];
455 double squareArea = [
NCGeometryUtils polygonAreaWithPolygon:squarePolygon];
456 NCPoint *squareCenter = [
NCGeometryUtils polygonCenterWithPolygon:squarePolygon];
458 NSLog(
@"Square area: %.2f square meters", squareArea);
459 NSLog(
@"Square center: (%.2f, %.2f)", squareCenter.
x, squareCenter.
y);
462 NSArray<NCPoint *> *triangle = @[
463 [[
NCPoint alloc] initWithX:0.0 y:0.0],
464 [[
NCPoint alloc] initWithX:10.0 y:0.0],
465 [[
NCPoint alloc] initWithX:5.0 y:10.0]
467 NCPolygon *trianglePolygon = [[
NCPolygon alloc] initWithPoints:triangle];
469 double triangleArea = [
NCGeometryUtils polygonAreaWithPolygon:trianglePolygon];
470 NCPoint *triangleCenter = [
NCGeometryUtils polygonCenterWithPolygon:trianglePolygon];
472 NSLog(
@"Triangle area: %.2f square meters", triangleArea);
473 NSLog(
@"Triangle center: (%.2f, %.2f)", triangleCenter.
x, triangleCenter.
y);
476 NCPoint *insideSquare = [[
NCPoint alloc] initWithX:5.0 y:5.0];
477 NCPoint *outsideSquare = [[
NCPoint alloc] initWithX:15.0 y:15.0];
479 BOOL containsInside = [
NCGeometryUtils polygonContainsPointWithPolygon:squarePolygon point:insideSquare];
480 BOOL containsOutside = [
NCGeometryUtils polygonContainsPointWithPolygon:squarePolygon point:outsideSquare];
482 NSLog(
@"Square contains (5,5): %@", containsInside ?
@"YES" :
@"NO");
483 NSLog(
@"Square contains (15,15): %@", containsOutside ?
@"YES" :
@"NO");
489- (void)demonstrateProjectionCalculations {
490 NSLog(
@"--- Projection Calculations ---");
492 NCSegment *segment = [[
NCSegment alloc] initWithStart:[[
NCPoint alloc] initWithX:0.0 y:0.0]
493 end:[[
NCPoint alloc] initWithX:10.0 y:10.0]];
496 NSArray<NSNumber *> *ratios = @[@0.0, @0.25, @0.5, @0.75, @1.0];
498 for (NSNumber *ratioNumber in ratios) {
499 double ratio = ratioNumber.doubleValue;
500 NCPoint *ratioPoint = [
NCGeometryUtils getRatioPointWithSegment:segment r:ratio];
501 NSLog(
@"Ratio %.2f: (%.2f, %.2f)", ratio, ratioPoint.
x, ratioPoint.
y);
505 NCPoint *testPoint = [[
NCPoint alloc] initWithX:5.0 y:5.0];
506 double projectionRatio = [
NCGeometryUtils getProjectionRatioWithSegment:segment point:testPoint];
507 NSLog(
@"Projection ratio for point (5,5): %.2f", projectionRatio);
510 NCPoint *offSegmentPoint = [[
NCPoint alloc] initWithX:5.0 y:0.0];
511 double distanceToOffPoint = [
NCGeometryUtils segmentPointDistanceWithSegment:segment point:offSegmentPoint];
512 NSLog(
@"Distance from segment to point (5,0): %.2f meters", distanceToOffPoint);
519 NSLog(
@"=== GeometryUtils Example ===");
522 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
523 NSLog(
@"=== Example completed ===");
532int main(
int argc,
const char * argv[]) {
535 [example runExample];
538 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:10.0]];