错误的面积计算目标-c?

时间:2016-02-26 10:24:47

标签: ios objective-c geometry polygon area

我有一个包含5个CGPoints(v0,v1,v2,v3,v4)的数组,它们是这样实现的形状的顶点:

GetFromAD()

然后,我用这些点计算区域:

_arrayVertex = [[NSMutableArray alloc] initWithObjects:[NSValue valueWithCGPoint:v0], [NSValue valueWithCGPoint:v1], [NSValue valueWithCGPoint:v2], [NSValue valueWithCGPoint:v3], [NSValue valueWithCGPoint:v4], nil];

区域是否正确计算?我错过了什么吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

SBIM:

- (float)calculateArea {

    float area = 0;
    int N = (int)_arrayVertex.count;

    for (int i = 0; i < N; i++) {

        float term = ([_arrayVertex[i] CGPointValue].x * [_arrayVertex[(i+1) % N] CGPointValue].y -
              [_arrayVertex[(i+1) % N] CGPointValue].x * [_arrayVertex[i] CGPointValue].y)/2.0;

        area += term;
    }

    return fabsf(area);

}