Bresenham's circle drawing algorithm in C Graphics


Bresenham's circle drawing algorithm in C Graphics




  #include <stdio.h>
  #include <conio.h>
  #include <graphics.h>
  #include <dos.h>

  void main() {
        /* request auto detection */
        int gdriver = DETECT, gmode, err;
        int midx, midy, x, y, radius, dp;

        /* initialize graphic mode */
        initgraph(&gdriver, &gmode, "C:/TC/BGI");

        radius = 100;
        /* mid position of x-axis */
        midx = getmaxx() / 2;
        /* mid position of y-axis */
        midy = getmaxy() / 2;

        dp = 1 - radius;
        x = 0, y = radius;

        /* draws a circle */
        do {
                /*
                 * plot points on all eight octants -
                 * circle centered at (midx, midy)
                 */
                putpixel(midx + x, midy + y, WHITE);
                putpixel(midx - x, midy + y, WHITE);
                putpixel(midx + x, midy - y, WHITE);
                putpixel(midx - x, midy - y, WHITE);
                putpixel(midx + y, midy + x, WHITE);
                putpixel(midx - y, midy + x, WHITE);
                putpixel(midx + y, midy - x, WHITE);
                putpixel(midx - y, midy - x, WHITE);
                delay(100);

                /*
                 * calculate next points(x, y) - considering
                 * the circle centered on (0, 0).
                 */
                x = x + 1;
                if (dp < 0) {
                        dp = dp + 2 * x + 1;
                } else {
                        y = y - 1;
                        dp = dp + 2 * (x - y) + 1;
                }

        } while (x < y);

        getch();

        /* deallocate memory allocated for graphic screen */
        closegraph();

  }


OUTPUT:-


Comments

Popular posts from this blog

Implement Bouncing Ball Animation Using C Graphics

Walking Stickman -Using C graphics

Sine Wave - Using C Graphics