Reputation: 3506
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?
Upvotes: 0
Views: 98
Reputation: 4191
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:
Upvotes: 0
Reputation: 13020
- (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");
}
Upvotes: 1