ios - 查找关闭多边形 iOS 的总数

标签 ios objective-c algorithm graph-algorithm shapes

我想找到闭合形状的总数。

enter image description here

在图像中,有 6 个 no of close polygons 。 我尝试了以下方法

    for (NSInteger i = 0; i < [arrLinesInfo count]; i++) {

              NSDictionary *dictLineInfo = [arrLinesInfo objectAtIndex:i];

              startPoint = CGPointMake([[dictLineInfo valueForKey:@"line_start_point_x"] doubleValue], [[dictLineInfo valueForKey:@"line_start_point_y"] doubleValue]);
              endPoint = CGPointMake([[dictLineInfo valueForKey:@"line_end_point_x"] doubleValue], [[dictLineInfo valueForKey:@"line_end_point_y"] doubleValue]);

              [self isCircularRoute:startPoint withEndPoint:endPoint];
            }


            -(void) isCircularRoute:(CGPoint) lineStartPoint withEndPoint:(CGPoint) lineEndPoint 
            {
                NSPredicate *pre= [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"
    (self.line_end_point_x == '%f' && self.line_end_point_y == '%f') OR 
        (self.line_start_point_x == '%f' && self.line_start_point_y == '%f') OR 
        (self.line_end_point_x == '%f' && self.line_end_point_y == '%f') OR
         (self.line_start_point_x == '%f' && self.line_start_point_y == '%f')", lineStartPoint.x, 
        lineStartPoint.y,
         lineStartPoint.x,
        lineStartPoint.y,
         lineEndPoint.x,
         lineEndPoint.y,
         lineEndPoint.x,
         lineEndPoint.y]];

 NSMutableArray *arrSamePointRef = [[arrLinesInfo filteredArrayUsingPredicate:pre] mutableCopy];

 arrSamePointRef = [[arrSamePointRef filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"
(self.line_start_point_x != '%f' && self.line_start_point_y != '%f') &&
         (self.line_end_point_x != '%f' && self.line_end_point_y != '%f')", lineStartPoint.x
        , lineStartPoint.y
        , lineEndPoint.x
        , lineEndPoint.y]]] mutableCopy];//[arrSamePointRef removeObject:dictLineInfo];

                if(arrSamePointRef.count > 2){
                   totalPolygon = totalPolygon + 1;
                }
                NSLog(@"totalPolygon : ===== %tu", totalPolygon);

                for (NSDictionary *dictSingleLine in arrSamePointRef) {

                    CGPoint newStartPoint = CGPointMake([[dictSingleLine valueForKey:@"line_start_point_x"] doubleValue], [[dictSingleLine valueForKey:@"line_start_point_y"] doubleValue]);
                    CGPoint newEndPoint = CGPointMake([[dictSingleLine valueForKey:@"line_end_point_x"] doubleValue], [[dictSingleLine valueForKey:@"line_end_point_y"] doubleValue]);

                    [self isCircularRoute:newStartPoint withEndPoint:newEndPoint];

               }

            }

这是无限循环。

我在数组中有所有起点和终点对象。 如下所示的数组对象

[ { "point_start_lbl" : "a", "point_end_lbl" : "b", "line_start_point_x" : 200, "line_start_point_y" : 10, "line_end_point_x" : 100, "line_end_point_y" : 10, }, ... ]

请帮帮我。 提前致谢。

最佳答案

如果你有一个有序的边列表,那么你肯定有一个封闭的多边形,这样每条边都在下一条开始的顶点上结束,并且列表中没有重复的边。

我不清楚你的数据结构,但我可能因此:

定义一个对象,Edge,它标识两个顶点。

对于每个顶点,创建一个数组,其中包含接触该顶点的每条边。

然后,类似 Swift 风格的伪代码:

var successfulPaths: [Edge] = []
for edge in edges
{
    let usedEdges = [edge]
    attemptTraversalFrom(edge.vertex1, edge.vertex2, usedEdges, successfulPaths)
    attemptTraversalFrom(edge.vertex2, edge.vertex1, usedEdges, successfulPaths)
}

print("There were \(successfulPaths.count) successful paths")

[...]

func attemptTraversalFrom(startingVertex, endingVertex, usedEdges, successfulPaths) {
    let vertexEdges = endingVertex.edges
    for edge in (edges not in usedEdges) {
        let newEndingVertex = 
            (edge.vertex1 == endingVertex) ? edge.vertex2 : edge.vertex1

        if newEndingVertex == startingVertex {
            successfulPaths.add(usedEdges)
            return
        } else {
            let newUsedEdges = userEdges.addItem(edge)
            attemptTraversalFrom(startingVertex, newEndingVertex, newUsedEdges, successfulPaths)
        }
    }

    // Note: will automatically fall through to here and return
    // without adding anything to `successfulPaths` if there are
    // no further traversable edges
}

临时等。有点像 Dijkstra 寻路算法的递归部分,除了潜在路径是累积的,而不是较短的路径在完成评估之前消除较长的路径。

关于ios - 查找关闭多边形 iOS 的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074652/

相关文章:

objective-c - 在 OS X 中监控来自不同协议(protocol)的网络流量

ios - 使用 maxConcurrentOperationCount 对 NSOperationQueue 进行单元测试

java - 从 Long id 生成唯一哈希

algorithm - 如何简化 Big-O 表达式

ios - Objective C在哪里存储对象,堆或堆栈

objective-c - C作为主要类(class);或没有ObjC的 cocoa 应用

ios - 使用 takePictureAsync 时如何在 iOS 上更快地上传图像?

algorithm - 如何使用非单词标记识别文本中的单词?

ios - 如何将 uilabel 高度设置为 0。或将 uibleviewcell 高度设置为 0?

ios - 以编程方式选择 Collection View 单元格无法执行单元格的某些 isSelected 例程