This blog is under construction

Sunday 1 September 2013

C program to implement midpoint circle algorithm

Midpoint Circle Algorithm:
Step 1:  Input radius r and circle center (Xc, Yc) from the user.

Step 2:  Plot the first set of points on the circumference of a circle centered at (Xc, Yc).
             (Xc, Yc + r)
             (Xc, Yc - r)
             (Xc + r, Yc)
             (Xc - r, Yc)

Step 3:  Find the initial value of the decision parameter.
             P0 = (5 / 4) - r

Step 4:  If Pk < 0, the subsequent point along the circle centered at (0, 0) is
             Xk + 1 = Xk + 1
             Yk + 1 = Yk
             and Pk + 1 = Pk + 2Xk+1 + 1

             Otherwise, the subsequent point along the circle is
              Xk + 1 = Xk + 1
              Yk + 1 = Yk - 1
              and Pk + 1 = Pk + 2Xk + 1 - 2Yk + 1
              where 2Xk + 1 = 2Xk + 1 and 2Yk + 1 = 2Yk - 2

Step 5:  Find the symmetric points in all other octants.

Step 6:  Plot the above calculated pixel(x, y) onto the circular path of the circle centered at (Xc, Yc) and plot the co-ordinates.
             (Xc + x, Yc + y)
             (Xc - x, Yc + y)
             (Xc + x, Yc - y)
             (Xc - x, Yc - y)
             (Xc + y, Yc + x)
             (Xc - y, Yc + x)
             (Xc + y, Yc - x)
             (Xc - y, Yc - x)

Step 7:  Repeat step 4 through 6 until X less than or equal to Y.


Write a C program to draw a circle using midpoint circle algorithm


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

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

        /* initialize graphic mode */
        initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
        err = graphresult();

        if (err != grOk) {
                /* error occurred */
                printf("Graphics Error: %s\n",
                                grapherrormsg(err));
                return 0;
        }

        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();
        return 0;
  }




Output: (Draw a circle using midpoint ellipse algorithm)


No comments:

Post a Comment