(mods please don’t move this to “displays” since it IS a programming problem…)

Hi all,

I’m trying to incorporate a polygon drawing function into my VFD graphic display driver.

It works, but it’s not perfect. The problem is that some X,Y coordinates for a vertex are off by one, so that sometimes what SHOULD be a straight vertical or horizontal line has a slight “jink” to it. (see attached snapshot).

I’m sure it’s some kind of a rounding problem, but I’ll be darned if I can find it.

One thing I do (you’ll see in the attached code below) is to take the desired rotation angle of the polygon and subtract 90.0 degrees from it in order to make 0 degrees “straight up” (i.e. the 12:00 o’clock position). Larger rotation angles rotate the polygon CLOCKWISE.

OK, so here’s the code:

```
// draw a polygon of any number of sides, rotated by any "angle"
// note angle "0" degrees is straight up (i.e. a triangle is
// drawn like this: /\
// /__\
// and the angle goes clockwise so that an angle of 90 degrees
// points the apex to the 3:00 o'clock position.
void Noritake_GUU100::drawPolygon (uint8_t x, uint8_t y, uint8_t radius, uint8_t sides, double angle, uint8_t on)
{
uint8_t x1, y1, x2, y2;
double th, inc;
if (sides < 3) { // polygon must be at least 3 sides!
return;
}
if (sides > 31) { // too many sides is slow and just makes a circle anyway...
drawCircle (x, y, radius, on);
}
// we work in radians here
th = rad (angle - 90.0); // make 0 degrees straight up
inc = (2.0 * M_PI / sides); // radians to next vertex
x2 = round (cos (th) * radius + x); // get first vertex
y2 = round (sin (th) * radius + y);
while (sides--) {
x1 = x2; // old vertex is...
y1 = y2; // ...the new startpoint
th += inc; // angle of next vertex in radians
x2 = round (cos (th) * radius + x); // get next vertex
y2 = round (sin (th) * radius + y);
drawLine (x1, y1, x2, y2, on); // draw side
}
}
```

Any ideas will be greatly appreciated.