gpt4 book ai didi

c - voronoi 图不正确

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:25 25 4
gpt4 key购买 nike

我创建了一个无法正常工作的 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

最佳答案

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

关于c - voronoi 图不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29429569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com