Using the C function in Objective-C (for iPhone)

'all. I self-described recognized noob in programming on the iPhone (having a much longer pearl and web background for 30 years) ... but last week I decided to plunge into a couple of good books. After overflowing and reading over 1000 pages - and understanding it pretty well, I'm well on my way to the first first iPhone application. My problem is this: I do not know how to perform a simple geography procedure (lat / long) point-in-polygon in Objective-C. I have two ways to do this. One in C (first code example) and one in JavaScript (second code example):

// this is the poly.h file

int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);


// this is the poly.c file

#include "poly.h"
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy){
 int i, j, c = 0;
 for (i = 0, j = nvert-1; i < nvert; j = i++) {
 if ( ((verty[i]>testy) != (verty[j]>testy)) &&
  (testx < (vertx[j]-vertx[i]) * (testy-verty[i]) / (verty[j]-verty[i]) + vertx[i]) )
    c = !c;
 }
 return c;
}

or this (in Javascript):

function _isPointInPoly(poly, pt){
 for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
  ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
  && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
  && (c = !c);
 return c;
}

(or one will work if I could convert them)

, ... .h .c xcode iPhone. , Objective-C ..:)

: Google, , " C iPhone Objective-C" .., , !:) , google .

, :

  • pnpoly Objective-C?
  • ? (int , float * vertx - , , .., NSArray - )

(: . , , )

.

( objective-c) : ( )

NSMutableArray *latitudeArray = [[NSMutableArray alloc] init];
NSMutableArray *longitudeArray = [[NSMutableArray alloc] init];

// coordinates surrounding 1 inifite loop.

[latitudeArray addObject:@"37.32812557141369"];
[longitudeArray addObject:@"-122.0320253896318"];
[latitudeArray addObject:@"37.32821852349916"];
[longitudeArray addObject:@"-122.0289014325174"];
[latitudeArray addObject:@"37.33021046381746"];
[longitudeArray addObject:@"-122.0289300638158"];
[latitudeArray addObject:@"37.33042111092124"];
[longitudeArray addObject:@"-122.0279574092159"];
[latitudeArray addObject:@"37.33395972491337"];
[longitudeArray addObject:@"-122.0279263955651"];
[latitudeArray addObject:@"37.33363270879559"];
[longitudeArray addObject:@"-122.0320527775551"];
[latitudeArray addObject:@"37.32812557141369"];
[longitudeArray addObject:@"-122.0320253896318"];


int nvert = [[latitudeArray count] intvalue];

// 37.33189399206268 x -122.0296274412866 should return true

float testx =37.33189399206268;
float testy =-122.0296274412866;

int y_or_n = pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);

, Objective-C, , C- - , C, , , .

, ... . - ? .

+3
3

, :

int nCoords = 4;
float vertexXCoords[n] = {0.0, 0.0, 20.0, 20.0};
float vertexYCoords[n] = {0.0, 20.0, 20.0, 0.0};
NSPoint testPoint = NSMakePoint(5, 10);

BOOL testPointIsInPoly = pnpoly(nCoords, xCoords, yCoords, testPoint.x, testPoint.y);

, Objective-C. C ( Cocoa BOOL NSPoint C). Objective-C C, C- Objective-C. , Objective-C . ( C).

+3

. Objective-C - API C ( , ...), C , C.

- (int)doWhatever {
  // ...
  int hitTest = pnPoly(/*blah*/);
  return hitTest;
}

C, int float, Objective-C . float:). Foundation, NSArray, NSNumber.

NSNumber *someFloat = [NSNumber numberWithFloat: f];
float usefulValue = [someFloat floatValue];
+7

The target of C is a superset of C, so you can call C routines. If you call this procedure many times, open it or make it a macro.

+1
source

Source: https://habr.com/ru/post/1731245/


All Articles