首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >voronoi图不对

voronoi图不对
EN

Stack Overflow用户
提问于 2015-04-03 09:30:19
回答 1查看 55关注 0票数 0

我已经创建了一个voronoi代码,它不像它所想的那样工作。

我真想不出这个错误!

我调用的函数是:

代码语言:javascript
复制
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 */


}

您可以找到代码这里

我所收到的图像:

正确的图像:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-03 09:46:54

我认为您需要为每一点重新设置initDistance,如下所示

代码语言:javascript
复制
...
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 */
...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29429569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档