gpt4 book ai didi

objective-c - 根据外部点对 CGPoints 数组进行排序

转载 作者:行者123 更新时间:2023-12-02 21:58:57 24 4
gpt4 key购买 nike

我有一个 CGPoints 数组,想根据到外部点的最近距离对其进行排序。我已执行以下操作,但返回了错误的结果。

NSArray *sortedArray = [self.availableRenderPoints sortedArrayUsingComparator:^NSComparisonResult(NSValue *obj1, NSValue *obj2)
{
CGPoint p1 = [obj1 CGPointValue];
CGPoint p2 = [obj2 CGPointValue];

CGFloat distance1 = ccpDistance(startPoint, p1);
CGFloat distance2 = ccpDistance(startPoint, p2);

if (distance1 <= distance2) return p1.y < p2.y;
if (distance2 <= distance1) return p2.y < p1.y;

return p1.x < p2.x;
}];

如何根据距起始点的最近距离对其进行排序?

最佳答案

不应该是这样吗

NSArray *sortedArray = [self.availableRenderPoints sortedArrayUsingComparator:^NSComparisonResult(NSValue *obj1, NSValue *obj2)
{
CGPoint p1 = [obj1 CGPointValue];
CGPoint p2 = [obj2 CGPointValue];

CGFloat distance1 = ccpDistance(startPoint, p1);
CGFloat distance2 = ccpDistance(startPoint, p2);

if (distance1 < distance2) return NSOrderedAscending;
if (distance2 < distance1) return NSOrderedDescending;

return NSOrderedSame;
}];

关于objective-c - 根据外部点对 CGPoints 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280387/

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