I have 4 or more points that make up a closed path. I want to get each point in the image within the closed path. If there any method to do something like this?

有帮助吗?

解决方案

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

其他提示

It's a large area in Computer Graphics and Image Processing with many applications in Geographical Informational Systems. Look for "Polygon-Clipping Algorithms" or try another StackExchange site:

http://gis.stackexchange.com

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top