我已经创建了一个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 */
}您可以找到代码这里
我所收到的图像:

正确的图像:

发布于 2015-04-03 09:46:54
我认为您需要为每一点重新设置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 */
...https://stackoverflow.com/questions/29429569
复制相似问题