2010-09-19 54 views
0

我昨天發佈了類似的東西,但沒有得到任何東西。我今天花了幾個小時解決問題,但沒有任何進展。處理/線圖 - 幫助

我正在使用處理(語言)並試圖實現一種方法,在兩點之間繪製一條線。 (我不想使用庫的line()方法。)

我的lineCreate方法對於正斜率非常有效,但對於負斜率失敗。你能幫忙弄清楚爲什麼?

這裏的lineCreate()代碼:

void createLine(int x0, int y0, int x1, int y1){ 
    //... 
    // Handle slanted lines... 
    double tempDX = x1 - x0; 
    double tempDY = y1 - y0;   // Had to create dx and dy as doubles because typecasting dy/dx to a double data type wasn't working. 
    double m = (-tempDY/tempDX);  // m = line slope. (Note - The dy value is negative 
    int deltaN = (2 * -dx);  // deltaX is the amount to increment d after choosing the next pixel on the line. 
    int deltaNE = (2 * (-dy - dx));  // ...where X is the direction moved for that next pixel. 
    int deltaE = (2 * -dy);   // deltaX variables are used below to plot line. 
    int deltaSE = (2 * (dy + dx)); 
    int deltaS = (2 * dx); 
    int x = x0; 
    int y = y0; 
    int d = 0;       // d = Amount d-value changes from pixel to pixel. Depends on slope. 
    int region = 0;     // region = Variable to store slope region. Different regions require different formulas. 
if(m > 1){       // if-statement: Initializes d, depending on the slope of the line. 
     d = -dy - (2 * dx);     // If slope is 1-Infiniti. -> Use NE/N initialization for d. 
     region = 1; 
    } 
    else if(m == 1) 
    region = 2; 
    else if(m > 0 && m < 1){ 
     d = (2 * -dy) - dx;     // If slope is 0-1 -> Use NE/E initialization for d. 
     region = 3; 
    } 
    else if(m < 0 && m > -1){   
     d = (2 * dy) + dx;     // If slope is 0-(-1) -> Use E/SE initliazation for d. 
     region = 4; 
    } 
    else if(m == -1) 
    region = 5; 
    else if(m < -1){ 
     d = dy + (2 * dx);     // If slope is (-1)-(-Infiniti) -> Use SE/S initialization for d. 
     region = 6; 
    } 
    while(x < x1){     // Until points are connected... 
     if(region == 1){   // If in region one... 
       if(d <= 0){    // and d<=0... 
       d += deltaNE;   // Add deltaNE to d, and increment x and y. 
       x = x + 1; 
       y = y - 1; 
      } 
      else{    
       d += deltaN;  // If d > 0 -> Add deltaN, and increment y. 
       y = y - 1; 
      } 
     } 
     else if(region == 2){ 
      x = x + 1; 
      y = y - 1; 
     } 
     else if(region == 3){  // If region two... 
       if(d <= 0){    
       d += deltaE; 
       x = x + 1; 
      } 
      else{ 
       d += deltaNE; 
       x = x + 1; 
       y = y - 1; 
      } 
     } 
     else if(region == 4){  // If region three... 
       if(d <= 0){    
       d += deltaSE; 
       x = x + 1; 
       y = y + 1; 
      } 
      else{ 
       d += deltaE; 
       x = x + 1; 
      } 
     } 
     else if(region == 5){ 
      x = x + 1; 
      y = y + 1; 
     } 
     else if(region == 6){  // If region four... 
       if(d <= 0){    
       d += deltaSE; 
       x = x + 1; 
       y = y + 1; 
      } 
      else{ 
       d += deltaS; 
       y = y + 1; 
      } 
      } 
     point(x, y);   // Paints new pixel on line going towards (x1,y1). 
    } 
    return; 
} 
+0

如果這是您的真實代碼,dx和dy在哪裏定義,我認爲它們是tempDX和tempDY? – 2010-09-19 09:49:43

+0

不 - 我只是展示了alogrithm的肉,因爲我之前已經發布過(有不同的問題),並且由於代碼太多而沒有得到答案。 – Towely 2010-09-19 19:19:52

回答

1

看一看這個page。它用代碼示例解釋了繪製線條的整個理論。

有許多已知的線繪圖算法。閱讀關於他們here