在Objective-C中使用C函数(适用于iPhone) [英] Using a C function in Objective-C (for iPhone)

查看:112
本文介绍了在Objective-C中使用C函数(适用于iPhone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'全部.我自称是iPhone编程的入门者(具有更长的Perl和Web背景-30年)...但是上周大跌,买了几本好书.在塞满并阅读了1000多个页面之后-并很好地理解了它,我很顺利地开始着手开发第一个出色的Native iPhone应用程序.我的问题是:我不知道如何在Objective-C中执行简单的地理(经/纬)多边形点例程.我有两种方法可以做到这一点. C语言中的一个(第一个代码示例)和JavaScript语言中的一个(第二个代码示例):

'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;
}

或这个(用Javascript):

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)

所以,要尝试一下...我将.h文件和.c文件放入我的iPhone项目的xcode中.现在唯一的问题是如何从Objective-C调用此方法并获取结果..:)

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.. :)

顺便说一句:我昨晚搜索了大神谷歌以得到答案,但只需尝试搜索在Objective-C iPhone应用程序中包含C",等等.做这个! :)只是让您知道我在这里发布之前尝试过Google.

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.

好的,我的问题:

  1. 我怎么称呼Objective-C的pnpoly?
  2. 我使用什么类型的称呼? (int很好,但是浮点数 * vertx显然是一个浮点数数组..NSArray没有 -我可以找到)
  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)

(这里有更多信息.我正在寻求帮助构造应该通过的阵列)

这个问题没有被完全问到.

The question was not asked fully.

(在objective-c中)例程将是这样的:(假设它被正确编码)

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

我应该清楚地表明我正在学习Objective-c,但是发现C例程了-所以不确定如何构造C变量来调用它或使用例程来调用它.

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.

推荐答案

以下是如何使用它的示例:

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

请注意,这里没有特定于Objective-C的内容.这是C代码(尽管它确实使用Cocoa BOOL和NSPoint C类型).由于Objective-C是C的严格超集,因此任何有效的C代码也是有效的Objective-C.在这种情况下,Objective-C的独特功能也不会特别有用. (通常,数值计算不太复杂,在普通C语言中可读性更高.)

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

这篇关于在Objective-C中使用C函数(适用于iPhone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆