voronoi 图不正确

voronoi diagram is not right

我创建了一个 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

我收到的图片:

右图:

我认为你需要为每个点重新设置 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 */
...