'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):
int pnpoly(int nvert, float *vertx, float *verty, float testx, float testy);
#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];
[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];
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, , , .
, ... . - ?
.