gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:31:57 25 4
gpt4 key购买 nike

我有一个包含 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com