2015-04-03 72 views
0

我創建了一個voronoi代碼,它不工作,因爲它應該。voronoi圖是不正確的

我真的不知道錯誤!

我打電話的功能是:

void Voronoi(

    const int NbPoints, 
    const int height, 
    const int width, 
    float * X, 
    float * Y, 
    int * V, 
    int * const ouVoronoi) 
{ 

    float Xd , Yd; 
    float Distance ,initDistance = FLT_MAX; 
    int Threshold; 

    int x , y; // pixel coordinates 
    int i; 

    for (y = 0; y < height; y++) 
    { 
     for (x = 0; x < width; x++) 
     { 
      //Calculate distances for all the points 
      for (i = 0; i < NbPoints; i++) 
      { 
       Xd = X[ i ] - x; 
       Yd = Y[ i ] - y; 
       Distance = Xd * Xd + Yd * Yd; 

       //if this Point is closer , assign proper threshold 
       if (Distance < initDistance) 
       { 
        initDistance = Distance; 
        Threshold = V[ i ]; 
       } 

       *(ouVoronoi + (x + y * width)) = Threshold; 

      } /* i */ 
     } /* x */ 

    } /* y */ 


} 

你可以找到的代碼here

圖像我收到:

wrong

右圖像:

right

+0

應該'*(ouVoronoi +(X + Y *寬度))=門限;'被*環路'以外*對於(i = 0;我 2015-04-03 09:35:47

+0

@Weather Vane:不,因爲我們正在計算此循環中的閾值,所以我們必須填寫ouVoronoi – George 2015-04-03 09:39:16

+0

確定嗎?我沒有回答你原來的問題,但是因爲沒有涉及'我',所以在循環內部是多餘的。在'i'的每一次迭代中,您都會將'Threshold'寫入相同的地方。我建議在循環之後只做一次。 – 2015-04-03 09:42:48

回答

2

我想你需要重置initDistance對於每個點,這樣

... 
for (y = 0; y < height; y++) 
{ 
    for (x = 0; x < width; x++) 
    { 
     //Calculate distances for all the points 
     initDistance = FLT_MAX;        // <--- added this line 
     for (i = 0; i < NbPoints; i++) 
     { 
      Xd = X[ i ] - x; 
      Yd = Y[ i ] - y; 
      Distance = Xd * Xd + Yd * Yd; 

      //if this Point is closer , assign proper threshold 
      if (Distance < initDistance) 
      { 
       initDistance = Distance; 
       Threshold = V[ i ]; 
      } 
     } /* i */ 
     *(ouVoronoi + (x + y * width)) = Threshold;  // <-- moved out of loop 
    } /* x */ 

} /* y */ 
... 
+0

:非常感謝!!現在一切正常,我完全忘記了初始距離! – George 2015-04-03 09:53:48