ios - 错误的区域计算 objective-c ?

标签 ios objective-c geometry polygon area

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

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

然后,我用这些点计算面积:

- (float)calculateArea {

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

    for (int i = 0; i < N-1; 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);

}

面积计算正确吗?我错过了什么吗?提前谢谢你。

最佳答案

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

} 

关于ios - 错误的区域计算 objective-c ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35649172/

相关文章:

ios - Poosh Woosh - 如何在应用程序中打开推送通知中的 URL

ios - 从存储在变量中的类型获取静态属性的内容

ios - 看看这段代码,让我知道是否有任何我可以更改的地方

objective-c - 在 Cocoa 应用程序中将字符串设置为粘贴板(复制、粘贴)

ios - 如何在 iOS 中在其他标签下方显示两个单标签文本?

按顺时针顺序对 3D 点列表进行排序

ios - 在很短的时间后多次播放音频文件?

iphone - ABGroupAddMember 不适用于 iOS4 设备

ios - Objective-C - 如何知道点是否在四分之一圆内?

mysql - MySQL 中的几何