如何找到最近的CGPoint并从NSArray创建一个集群?
问题描述:
我有一个场景,为UIView中最近的CGPoint的群集。所以我必须设置CGPoint的NSArray,我试图让最近的值,做集群,但我无法得到的逻辑: //我的代码如何找到最近的CGPoint并从NSArray创建一个集群?
for (CGPoint firstObjOfCGPoint in cgPointGroupArray) {
for (CGPoint nextPoint in cgPointGroupArray) {
if (30>[self distanceBetween: firstObjOfCGPoint and:nextPoint]){
[shortestClusterArr addObject:nextPoint];
}
else{
[longestClusterArr addObject:nextPoint];
}
}
if(shortestClusterArr.count>2){
//clustered marker
[self addClusterMarker:shortestClusterArr];
}
else{
//dont cluster marker
}
}
}
//find distance
- (float)distanceBetween:(CGPoint)p1 and:(CGPoint)p2
{
return hypotf((p1.x-p2.x), (p1.y-p2.y));
}
上面的代码,循环时间去重复点以及凌驾于如果有人知道这里的逻辑评论,那么同样的对象。..
答
您需要检查添加它们之前对象是否已经存在。类似这样的:
for (CGPoint nextPoint in cgPointGroupArray) {
if (30>[self distanceBetween:point and: nextPoint]) {
if (![shortestClusterArr containsObject:nextPoint])
[shortestClusterArr addObject:nextPoint];
}
else{
if (![longestClusterArr containsObject:nextPoint])
[longestClusterArr addObject:nextPoint];
}
}