ios - 检查封闭路径内所有点的方法

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

我有 4 个或更多点构成闭合路径。我想获得闭合路径内图像中的每个点。有没有什么方法可以做到这样的事情?

最佳答案

 - (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");
}

关于ios - 检查封闭路径内所有点的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21595411/

相关文章:

iphone - 如何检查数组中的值是否不为 NULL?

visual-c++ - HaarTraining 与 OpenCV 错误

ios - GameCenter - 每个级别都有一个排行榜

ios - Firestore 数据库创建错误

iOS - 向 Copy.com 发送文件上传请求

c++ - ITK:未能正确调整图像大小

java - 在Android中合并两个图像

ios - Firebase 的 putData (存储)和 setData (数据库)是否会不断尝试直到成功/失败?

ios - 返回 App 时如何读取设置包?

objective-c - fmdb executeUpdate 失败