0

我有4個或更多的點組成封閉路徑。我想要在封閉路徑中獲取圖像中的每個點。如果有任何方法可以做這樣的事情?檢查封閉路徑內所有點的方法

+0

什麼是你的情況下的圖像?離散矩形網格的點? – HEKTO

回答

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

如果它不是矩形?或者它有多於4個頂點? –

+0

@KhawarAli檢查多個頂點 –

0

它在計算機圖形學圖像處理地理信息化系統許多應用的大面積。尋找「多邊形裁剪算法」或嘗試其它StackExchange網站:

http://gis.stackexchange.com