JCash / voronoi

A C implementation for creating 2D voronoi diagrams
MIT License
621 stars 92 forks source link

bounds are calculated incorrectly #42

Closed GrahamAsher closed 5 years ago

GrahamAsher commented 5 years ago

If no bounding rectangle is passed to jcv_diagram_generate_useralloc or jcv_diagram_generate, the function _jcv_calc_bounds is used to calculate it. This function is incorrect, because it receives the modified number of points after duplicates have been removed, but uses the unmodified point array data passed by the user. The fix is as follows:

  1. Change the call to _jcv_calc_bounds to this:

_jcv_calc_bounds(num_points,internal->sites, &d->min, &d->max);

  1. Change _jcv_calc_bounds to the following:
static inline void _jcv_calc_bounds(int num_points, const jcv_site* points, jcv_point* min, jcv_point* max)
{
    jcv_point _min = points[0].p;
    jcv_point _max = points[0].p;
    for( int i = 1; i < num_points; ++i )
    {
        if( points[i].p.x < _min.x )
            _min.x = points[i].p.x;
        else if( points[i].p.x > _max.x )
            _max.x = points[i].p.x;

        if( points[i].p.y < _min.y )
            _min.y = points[i].p.y;
        else if( points[i].p.y > _max.y )
            _max.y = points[i].p.y;
    }
    min->x = floor(_min.x);
    min->y = floor(_min.y);
    max->x = ceil(_max.x);
    max->y = ceil(_max.y);
}