Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 611719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:48:54+00:00 2026-05-13T17:48:54+00:00

‘lo all. I am a self-described admitted noob in iPhone programming (having a much

  • 0

‘lo all. I am a self-described admitted noob in iPhone programming (having a much longer perl & web background — 30 years)…but took the plunge last week and bought a couple of good books. After cramming and reading well over 1000 pages — and understanding it pretty well, I am well on my way to a good first Native iPhone app. My problem is this: I do not know how to do a simple Geographic (lat/long) point-in-polygon routine in Objective-C. I have 2 ways of doing this. One in C (the first code example) and one in JavaScript (the 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;
}

(either one will work if i could get them converted)

So, to try this out…I put the .h file and .c file into xcode with my iPhone project. The only question now is how to call this from Objective-C and get the result.. 🙂

BTW: I searched the Great God Google all last night to get the answer to this but just TRY to search for “including C in an Objective-C iPhone app”, etc.. you get so many entries and none have to do with this! 🙂 Just letting you know I tried google before posting here.

Okay, my issues:

  1. How do I call the pnpoly from Objective-C?
  2. What types do i call it using? (int is fine, but the float
    *vertx is obviously an array of floats..which NSArray does not have
    — that I can find)

(EDIT: HERE IS MORE INFO. I AM ASKING FOR HELP CONTRUCTING THE ARRAYS THAT WOULD BE PASSED AS WELL)

The question was not asked fully.

The routine (in objective-c) would be like this: (assuming this is coded right)

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);

I should’ve made it clear that I am learning Objective-c but FOUND that C routine–so was not sure how to construct either the C variables to call it with or the routine to call it with.

I know this is asking a lot…but it is really puzzling to me. Can anyone help me?
Thanks so much.

Jann

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:48:54+00:00Added an answer on May 13, 2026 at 5:48 pm

    Here’s en example of how it would be used:

    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);
    

    Note that there’s nothing specific to Objective-C in here. This is C code (though it does use the Cocoa BOOL and NSPoint C types). Since Objective-C is a strict superset of C, any valid C code is also valid Objective-C. This is also a case in which Objective-C’s unique features would not be particularly useful. (Numerical calculations in general are less complex and more readable in plain C.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.