# Re: Re: gdk_draw_polygon +stipple fill has problem

• To: "'gtk-app-devel-list gnome org'" <gtk-app-devel-list gnome org>
• Subject: Re: Re: gdk_draw_polygon +stipple fill has problem
• Date: Fri, 4 Jul 2003 10:39:00 +0200

Hi,

The width and height calculations are wrong. Obvioously, one cannot
calculate them before knowing what the actual minimum x and y will
be. I.e. two separate loops are needed:

for (i = 0; i < npoints; i++)
{
bounds.x = MIN (bounds.x, points[i].x);
bounds.y = MIN (bounds.y, points[i].y);
}

for (i = 0; i < npoints; i++)
{
bounds.width = MAX (bounds.width, points[i].x - bounds.x);
bounds.height = MAX (bounds.height, points[i].y - bounds.y);
}

Two separate loops are not needed if we introduce two temp variables
to hold the max coordinates:

/* Initial valid values for the bounds taken from the first point*/
bounds.x = tmpmax.x = points[0].x;
bounds.y = tmpmax.y = points[0].y;

/* Find min and max by iterating through the rest of the points*/
for (i = 1; i < npoints; i++) /* Note: start at 1 not 0 */
{
bounds.x = MIN (bounds.x, points[i].x);
bounds.y = MIN (bounds.y, points[i].y);
tmpmax.x = MAX(tmpmax.x, points[i].x);
tmpmax.y = MAX(tmpmax.y, points[i].y);
}

/* Compute extents */
bounds.width = tmpmax.x - bounds.x
bounds.height = tmpmax.y - bounds.y

(In fact, we can use bounds.width/height as temp variables, but then
the code is less readable because bounds.width/height will temporarily
contains the max coordinates instead of the width/height)

This gets rid of the second loop that in addition contained
two substractions.

Cheers

--