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)


1 comment:

  1. If you're an aspiring forex trader seeking to scale your strategy without risking personal capital, the Funded Account Forex program with Winprofx is your ideal launchpad in 2025. Winprofx offers one of the most accessible and trader-friendly funded account programs in the market, allowing skilled traders to demonstrate their abilities on a demo challenge and, upon success, gain access to real capital for live trading. Whether you're a day trader, swing trader, or scalper, Winprofx supports multiple trading styles across major currency pairs, commodities, and indices with tight spreads, fast execution, and zero hidden fees. What sets Winprofx apart is its transparent rules, generous profit-sharing structure, and robust risk management tools that empower traders to stay disciplined while maximizing returns. With advanced analytics, daily performance tracking, and professional mentorship, Winprofx transforms passionate traders into consistently profitable ones. Their funded account model bridges the gap between talent and opportunity—perfect for those who are skilled but lack starting capital. If your goal is to trade big, keep profits, and grow in the global forex market, then starting your Funded Account Forex journey with Winprofx might be your smartest financial move in 2025.

    Funded Account Forex -

    ReplyDelete