如何在目标C中形成CGPoint数组 [英] How to form CGPoint array in Objective C

查看:67
本文介绍了如何在目标C中形成CGPoint数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要这种结构

CGPoint addLines1[] =
{
    CGPointMake(30.0, 150.0),
    CGPointMake(41.67, 145.19),
    CGPointMake(53.33, 103.25),
    CGPointMake(65.0, 131.67),
    CGPointMake(76.67, 106.11),
    CGPointMake(88.33, 110.20),
    CGPointMake(100.0, 111.54),
    CGPointMake(111.67, 112.13),
    CGPointMake(123.33, 115.66),
    CGPointMake(135.0, 123.7),
    CGPointMake(146.67, 125.53),
    CGPointMake(158.33, 115.1),
    CGPointMake(170.0, 69.38),
    CGPointMake(181.67, 112.47),
    CGPointMake(193.33, 65.1),
    CGPointMake(205.0, 103.33),
    CGPointMake(216.67, 92.6),
    CGPointMake(228.33, 54.76),
    CGPointMake(240.0, 79.66),
    CGPointMake(251.67, 53.81),
    CGPointMake(263.33, 56.81),
    CGPointMake(275.0, 88.19),
    CGPointMake(286.67, 74.81),
    CGPointMake(298.33, 28.1),
    CGPointMake(310, 20.0),
};

为了进行一些计算并绘制数据.

In order to make some calculations and draw data.

我有CGPoint *lines = appDelegate.averageResponseTimePoints;

如何从*lines制作数组addLines[]?

推荐答案

C数组在运行时并不是真正的数组,它们只是指向相同类型的相邻对象块的指针.当您看到items[n]时,这只是*(items+n)的语法糖.

C arrays are not really arrays at run time, where they are just pointers to a contiguous block of objects of the same type. When you see items[n], that's just syntactic sugar for *(items+n).

在您的示例中,addLines[1]将是*(lines+1),而addLines[0]将是*(lines+0),即*lines.因此,addLines只是lines而没有指针取消引用. *lines是数组中的第一项,而lines是整个数组.

In your example addLines[1] would be *(lines+1) and addLines[0] would be *(lines+0), which is *lines. So, addLines is just lines without the pointer dereference. *lines is the first item in the array and lines is the whole array.

数组在编译时对指针有一些不同.例如,sizeof(addLines)将为您提供整个数组的大小.

Arrays have some differences to pointers at compile time. For example, sizeof(addLines) would give you the size of the whole array.

一旦将数组传递到大小可能会可变的某个位置,就会失去Arrayness,但是您仍然可以使用下标运算符.例如:

Array-ness is lost as soon as you pass the array somewhere where it's size might be variable, but you can still use the subscript operator. For example:

#include <Foundation/Foundation.h>

#define showsize( expr ) ( printf(#expr " = %zd\n", ( expr ) ) )

CGPoint *
pass_back(CGPoint points[4])
{
    showsize(sizeof(points));
    return points;
}

int
main(void)
{
    CGPoint square[] = {CGPointMake(-1.0,  1.0),
                        CGPointMake( 1.0,  1.0),
                        CGPointMake( 1.0, -1.0),
                        CGPointMake(-1.0, -1.0)};
    CGPoint* returned;
    int i;

    showsize(sizeof(CGPoint));
    showsize(sizeof(CGPoint*));
    showsize(sizeof(square));
    returned = pass_back(square);
    showsize(sizeof(returned));

    for (i = 0; i < 4; ++i) {
        printf("returned[%d] = {%0.1f, %0.1f}\n", i, (float) returned[i].x,
                                                 (float) returned[i].y);
    }

    return 0;
}

这会在Mac上输出以下内容:

This outputs the following on my Mac:


sizeof(CGPoint) = 8
sizeof(CGPoint*) = 4
sizeof(square) = 32
sizeof(points) = 4
sizeof(returned) = 4
returned[0] = {-1.0, 1.0}
returned[1] = {1.0, 1.0}
returned[2] = {1.0, -1.0}
returned[3] = {-1.0, -1.0}

在这里,square是四个CGPoint的大小,但是一旦发送到pass_back函数,它就只是指针的大小,因为这就是指针的大小.当指针返回(并命名为returned)时,它仍可以像数组一样使用.

Here, square is the size of four CGPoints, but once sent to the pass_back function, it's only the size of a pointer, because that's what it is. When the pointer comes back (and named returned) it can still be used like an array.

请注意循环中的幻数4.指针不知道它指向的数组的长度.

Note the magic number 4 in the loop. The pointer doesn't know the length of the array it's pointing to.

不能使用=运算符重新分配阵列.如果您确实必须用lines中的点填充addLines,则可以使用以下方法进行操作:

Arrays cannot be reassigned with the = operator. If you really must populate addLines with the points from lines, you can do that with something like the following:

memcpy(addLines, lines, sizeof(CGPoint) * numberOfPoints);

您必须从某处获取numberOfPoints,并且addLines必须足够大才能处理这些点.如果点数是常数,那没关系,但是如果点数在运行时可以变化,这将是 bad ,特别是如果这些点来自外部世界(请考虑执行任意代码).

You'll have to get numberOfPoints from somewhere, and addLines will have to be large enough to handle those points. That's okay if the number of points is a constant, but it would be bad if the number of points can vary at run time, especially if the points come from the outside world (think arbitrary code execution).

我将更改averageResponseTimePoints以返回NSArray而不是C样式的数组.您需要将CGPoint封装在对象中-您自己的对象或NSValue.

I'd change averageResponseTimePoints to return an NSArray rather than a C-style array. You'll need to encapsulate the CGPoints in objects - either your own object or NSValues.

这是您如何编写averageResponseTimePoints的示例:

Here's an example of how you could write averageResponseTimePoints:

- (NSArray*) averageResponseTimePoints
{
    NSMutableArray* result = [[[NSMutableArray alloc] init] autorelease];

    for (int i = 0; i < numberOfPoints; ++i) {
        NSValue* point = [NSValue value:points+i
                           withObjCType:@encode(CGPoint)];
        [result addObject:point];
    }

    return result;
}

如果您的代码与CocoaTouch一起运行,则可以使用它来创建点值:

If your code runs with CocoaTouch, you can use this to create the point value instead:

NSValue* point = [NSValue valueWithCGPoint:points[i]];

要从数组中获取CGPoint,您可以编写如下内容:

To get the CGPoints out of the array, you could write something like this:

for (NSValue* value in result) {
    NSPoint pt;
    [value getValue:&pt];
    NSLog(@"%f %f", pt.x, pt.y);
}

或使用CocoaTouch:

Or with CocoaTouch:

CGPoint pt = [value CGPointValue];

这篇关于如何在目标C中形成CGPoint数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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