2013-03-26 79 views
0

我想蟒蛇算法(在這裏:Extending a line segment to fit into a bounding box)轉換到我的iPhone應用程序..但它只能當我startPoint.x>然後endPointx如何在一個矩形擬合線

是什麼在這裏改變? 我不明白它..

如果我畫一條從左上角到右下角的線,它的工作原理!但是,如果我從右上角畫一條線,則會失敗。因此,它只能在一個方向。我想我必須要改變一些變量如果從右到左

MIN爲(0,0)和MAX取決於設備,但對於iPhone的Retina(300568)

- (NSMutableArray *) extendAlgorithm:(CGPoint)start withEnd:(CGPoint)end withBorderMin:(CGPoint)min andBorderMax:(CGPoint)max { 

    int x1 = (int) start.x;  int y1 = (int) start.y; 
    int x2 = (int) end.x;  int y2 = (int) end.y; 
    int xmin = (int) min.x;  int ymin = (int) min.y; 
    int xmax = (int) max.x;  int ymax = (int) max.y; 

    if(y1 == y2) { 
     return [[NSMutableArray alloc] initWithObjects: 
       [NSNumber numberWithDouble:xmin], 
       [NSNumber numberWithDouble:y1], 
       [NSNumber numberWithDouble:xmax], 
       [NSNumber numberWithDouble:y1], 
       nil]; 
    } 

    if(x1 == x2) { 
     return [[NSMutableArray alloc] initWithObjects: 
       [NSNumber numberWithDouble:x1], 
       [NSNumber numberWithDouble:ymin], 
       [NSNumber numberWithDouble:x1], 
       [NSNumber numberWithDouble:ymax], 
       nil]; 
    } 

    double y_for_xmin = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1); 
    double y_for_xmax = y1 + (y2 - y1) * (xmax - x1)/(x2 - x1); 

    double x_for_ymin = x1 + (x2 - x1) * (ymin - y1)/(y2 - y1); 
    double x_for_ymax = x1 + (x2 - x1) * (ymax - y1)/(y2 - y1); 

    if(ymin <= y_for_xmin <= ymax) { 

     if(xmin <= x_for_ymax <= xmax) { 
      return [[NSMutableArray alloc] initWithObjects: 
        [NSNumber numberWithDouble:xmin], 
        [NSNumber numberWithDouble:y_for_xmin], 
        [NSNumber numberWithDouble:x_for_ymax], 
        [NSNumber numberWithDouble:ymax], 
        nil]; 
     } 

     if(xmin <= x_for_ymin <= xmax) { 
      return [[NSMutableArray alloc] initWithObjects: 
        [NSNumber numberWithDouble:xmin], 
        [NSNumber numberWithDouble:y_for_xmin], 
        [NSNumber numberWithDouble:x_for_ymin], 
        [NSNumber numberWithDouble:ymin], 
        nil]; 
     } 
    } 

    if(ymin <= y_for_xmax <= ymax) { 

     if(xmin <= x_for_ymin <= xmax) { 
      return [[NSMutableArray alloc] initWithObjects: 
        [NSNumber numberWithDouble:x_for_ymin], 
        [NSNumber numberWithDouble:ymin], 
        [NSNumber numberWithDouble:xmax], 
        [NSNumber numberWithDouble:y_for_xmax], 
        nil]; 
     } 

     if(xmin <= x_for_ymax <= xmax) { 
      return [[NSMutableArray alloc] initWithObjects: 
        [NSNumber numberWithDouble:x_for_ymax], 
        [NSNumber numberWithDouble:ymax], 
        [NSNumber numberWithDouble:xmax], 
        [NSNumber numberWithDouble:y_for_xmax], 
        nil]; 
     } 
    } 

    return nil; 
} 
+0

會不會是你很難用調試器到達那裏,或者在檢查這些值時是否有任何問題? – 2013-03-26 17:38:23

+0

那麼讓我給你一個提示,你沒有足夠的條件來涵蓋所有的情況。 – 2013-03-26 18:05:56

回答

1
​​

不會做你希望是什麼(也許在Python,但絕對不是C),你必須寫爲

if (ymin <= y_for_xmin && y_for_xmin <= ymax) 

我不知道,如果你的算法涵蓋所有​​情況,所以我試圖獨立地找到一個算法。這是我的了:

- (NSArray *) extendAlgorithm:(CGPoint)start withEnd:(CGPoint)end withBorderMin:(CGPoint)min andBorderMax:(CGPoint)max 
{ 
    CGFloat u1 = MAXFLOAT; 
    CGFloat u2 = -MAXFLOAT; 
    CGFloat tmp; 
    if (end.x != start.x) { 
     tmp = (max.x - start.x)/(end.x - start.x); 
     if (tmp > 0 && tmp < u1) 
      u1 = tmp; 
     else if (tmp < 0 && tmp > u2) 
      u2 = tmp; 
     tmp = (min.x - start.x)/(end.x - start.x); 
     if (tmp > 0 && tmp < u1) 
      u1 = tmp; 
     else if (tmp < 0 && tmp > u2) 
      u2 = tmp; 
    } 
    if (end.y != start.y) { 
     tmp = (max.y - start.y)/(end.y - start.y); 
     if (tmp > 0 && tmp < u1) 
      u1 = tmp; 
     else if (tmp < 0 && tmp > u2) 
      u2 = tmp; 
     tmp = (min.y - start.y)/(end.y - start.y); 
     if (tmp > 0 && tmp < u1) 
      u1 = tmp; 
     else if (tmp < 0 && tmp > u2) 
      u2 = tmp; 
    } 
    CGPoint newStart, newEnd; 
    newStart.x = start.x + u2 * (end.x - start.x); 
    newStart.y = start.y + u2 * (end.y - start.y); 
    newEnd.x = start.x + u1 * (end.x - start.x); 
    newEnd.y = start.y + u1 * (end.y - start.y); 
    NSLog(@"%@, %@", NSStringFromCGPoint(newStart), NSStringFromCGPoint(newEnd)); 

    return [[NSArray alloc] initWithObjects: 
      [NSNumber numberWithDouble:newStart.x], 
      [NSNumber numberWithDouble:newStart.y], 
      [NSNumber numberWithDouble:newEnd.x], 
      [NSNumber numberWithDouble:newEnd.y], 
      nil]; 
} 

(我們的想法是可考慮行

(x, y) = (start.x, start.y) + u * (end.x - start.x, end.y - start.y) 

,然後確定參數u1u2其中線越過邊界。)

+0

我也嘗試過,沒有任何區別。 – 2013-03-26 19:28:32

+0

@ Christian'fuzi'Orgler:我試圖找出一些東西,我希望那些作品。 – 2013-03-26 19:44:51

+0

@「Martin R」:它的工作非常完美。非常感謝! – 2013-03-26 23:18:18

0

我沒有檢查有關算法什麼,但跳出的東西是這些行:

double y_for_xmin = y1 + (y2 - y1) * (xmin - x1)/(x2 - x1); 
// ... 

該表達的到底是一個整數除法(你施放XMIN,X1,X2等到整數)。這些將有整數結果,截斷。所以如果你期待1/2 == 0.5這樣的東西,那麼你會在(int)0處感到驚訝。

嘗試將堆棧變量聲明爲CGFloat,這就是它們在CGPoint結構中的樣子。

+0

我試過用CGFloats,..沒有其他結果,看看頂部:我編輯我的問題,因爲一些行工作! – 2013-03-26 17:46:40