检查封闭路径中所有点的方法

时间:2014-02-06 06:14:28

标签: ios objective-c image-processing geometry computational-geometry

我有4个或更多点组成一个封闭的路径。我想在闭合路径中获取图像中的每个点。如果有任何方法可以做这样的事情吗?

2 个答案:

答案 0 :(得分:1)

 - (BOOL)isPointContain:(NSArray *)vertices point:(CGPoint)test {
    NSUInteger nvert = [vertices count];
    NSInteger i, j, c = 0;
    CGPoint verti, vertj;

    for (i = 0, j = nvert-1; i < nvert; j = i++) {
        verti = [(NSValue *)[vertices objectAtIndex:i] CGPointValue];
        vertj = [(NSValue *)[vertices objectAtIndex:j] CGPointValue];
        if (( (verti.y > test.y) != (vertj.y > test.y) ) &&
        ( test.x < ( vertj.x - verti.x ) * ( test.y - verti.y ) / ( vertj.y - verti.y ) + verti.x) )
            c = !c;
    }

    return (c ? YES : NO);
}


NSArray *Vertices = [NSArray arrayWithObjects:
                            [NSValue valueWithCGPoint:CGPointMake(10, 40)],
                            [NSValue valueWithCGPoint:CGPointMake(30, 48)],
                            [NSValue valueWithCGPoint:CGPointMake(50, 80)],
                            [NSValue valueWithCGPoint:CGPointMake(45, 100)],

                            nil
                            ];

CGPoint point = CGPointMake(30, 28);
if ([self isPointContain:Vertices point:point]) {
    NSLog(@"YES");
} else {
    NSLog(@"NO");
}

答案 1 :(得分:0)

计算机图形图像处理中的一个大区域,地理信息系统中有许多应用程序。查找“Polygon-Clipping Algorithms”或尝试其他StackExchange站点:

http://gis.stackexchange.com