draw part of circle

I have a nice tft I want to use it as a timer.
First I draw a full circle, than I want to gradualy fill the circle as time passes by. For that I need to be able to draw a partial circle, who knows how to do that?

timer.jpg

drawing a circle is typically done with Bresenhams circle algorithm that calculates one quarter and mirrors that 8 times to fill every quadrant.

What you need is the polar formula for the pixels, using sin() and cos();

in code:

void drawCircle(int x, int y, int radius, int color)
{
  for (int i=0; i<360; i++)  // a bigger radius might need more steps
  {
    double radians = i * PI / 180;
    double px = x + radius * cos(radians);
    double py = y + radius * sin(radians);
    drawPixel(px, py, color);
  }
}

next step is to be able to draw a pie slice with a start and end angle

void drawPieSlice(int x, int y, int radius, int color, int startAngle, int EndAngle)
{
  for (int i=startAngle; i<EndAngle; i++)
  {
    double radians = i * PI / 180;
    double px = x + radius * cos(radians);
    double py = y + radius * sin(radians);
    drawPixel(px, py, color);
  }
}

with that DrawCircle can even become

void drawCircle(int x, int y, int radius, int color)
{
  drawPieSlice(x, y, radius, color, 0, 360);
}

If the circle is big and one get "holes" replace DrawPixel() with DrawLine() and draw from current to next point.
(left as an exercise :wink: