Re: semicircle [OFFTOPIC]

Search this archive.

From: Chris Atenasio (chris@crud.net)
Date: Wed 01 Dec 1999 - 03:38:22 IST


Maybe I'm missing something here, so just ignore me in that case, but
wouldnt the best solution be the Bresenham circle algorithm?  It's
fast, elegant, and takes aspect ratio into account.


/* draws a circle at (xc, yc) with radius r in color c
**
** note: the scaling factor of (SCREEN_WIDTH / SCREEN_HEIGTH) is used when
** updating d.  This makes round circles.  If you want ellipses, you can
** modify that ratio.
*/

void bresenham_circle(int xc, int yc, int r, char c)
{
      int x = 0, y = r, d = 2 * (1 - r);

      while(y > 0)
      {
            plotdot(xc + x, yc + y, c);
            plotdot(xc + x, yc - y, c);
            plotdot(xc - x, yc + y, c);
            plotdot(xc - x, yc - y, c);
            if(d + y > 0)
            {
                  y -= 1;
                  d -= (2 * y * SCREEN_WIDTH / SCREEN_HEIGTH) - 1;
            }
            if(x > d)
            {
                  x += 1;
                  d += (2 * x) + 1;
            }
      }
}

To make a filled circle, remove the plotdots and instead draw a
horizontal span(line) from (xc - x, yc - y) - (xc + x, yc - y) (this
should draw the top half of the filled semicircle).

My $0.03.

- Chris
------------------------------------------------------------------------
Chris Atenasio <chris@crud.net> - Friends don't let friends use Windows.
Send mail subject "send pgp key" or "word of the day" for auto-response.
Today's word of the day: undaunted


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:22 IST