voronoi图是不对的

时间:2015-04-03 09:30:19

标签: c 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

1 个答案:

答案 0 :(得分: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 */
...