Mid-Point Circle Drawing Algorithm

Mid-Point Circle Drawing algorithm is based on determining subsequent points for drawing a circle.This algorithm also uses the concept of symmetry between octants of a circle. So, we need to use this algorithm on one octant only.Corresponding points in other octants can be plotted using concept of symmetry between octants.

#include<stdio.h>
#include<conio.h>
/*Header file for built-in graphics functions such as plot() and initgraph()*/ 
#include<graphics.h>

/* Function to draw circle in  8 octants*/

void drawCircle(int xc, int yc, int x, int y)
{
/*This function plots a pixel at coordinates(x,y) specified by first 2 arguments and third argument specifies the color of the pixel*/
 putpixel(xc+x, yc+y, RED);
 putpixel(xc-x, yc+y, RED);
 putpixel(xc+x, yc-y, RED);
 putpixel(xc-x, yc-y, RED);
 putpixel(xc+y, yc+x, RED);
 putpixel(xc-y, yc+x, RED);
 putpixel(xc+y, yc-x, RED);
 putpixel(xc-y, yc-x, RED);
}

/* Function to implement mid point circle drawing algorithm in a single octant */

void circleMidpoint(int xc, int yc, int r)
{
 int x = 0, y = r;
 int p = 1 - r;
 while (x < y)
  {
    drawCircle(xc, yc, x, y);
    x++;
    if (p < 0)
       p = p + 2 * x + 1;
    else
    {
       y--;
       p = p + 2 * (x - y) + 1;
    }
    drawCircle(xc, yc, x, y);
  }
}

int main()
{
    int xc, yc, r;
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "c:\\tc\\bgi");/*loads the graphic driver and puts syetem into graphics mode*/

    /*
    syntax for initgraph() is:
    void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);
    *graphdriver is an integer that specifies the graphics driver to be used.DETECT requests auto-detect for the driver.
    *graphmode is an integer that specifies the initial graphics mode.If *graphdriver equals DETECT,*graphmode is set by initgraph to the highest resolution available for the detected driver.
    */

    printf("\t\t\tMID POINT CIRCLE DRAWING ALGORITHM\t\t\t\n\tEnter the center co-ordinates :");
    scanf("%d %d",&xc,&yc);
    printf("\nEnter radius of circle:");
    scanf("%d",&r);

    //Function call
    circleMidpoint(xc, yc, r);
    getch();
    closegraph();
    /*closegraph function closes the graphics mode, deallocates all memory allocated by graphics system and restores the screen to the original mode*/
    return 0;
}

Mid Point Algorithm

You can run this code in Turbo C++. Dev C++ and Microsoft visual studio do not support graphics libraries.
You can download Turbo C++ using this link Turbo C++

Other articles related to Computer Graphics available on TutsHeap are:
Cohen Sutherland Line Clipping Algorithm
Boundary-Fill Algorithm
Flood-Fill Algorithm
Mid-Point Ellipse Drawing Algorithm
DDA Line Drawing Algorithm
Bresenham’s Line Drawing Algorithm

This content has been helpful to you?

Thanks for contributing!

Yes No