Program to draw a circle using Mid-point circle drawing algorithm in C++


#include<iostream>
#include<graphics.h>
using namespace std;
void circleMidpoint(int,int,int);
int main()
{
    int xc,yc,r;
    cout<<"Enter center coordinate of circle:";
    cin>>xc>>yc;
    cout<<"Enter radius of circle:";
    cin>>r;
    int gd=DETECT,gm;
    char path[] = "";
    initgraph(&gd,&gm,path);
    circleMidpoint(xc,yc,r);
    getch();
    closegraph();
    return 0;
}
void circleMidpoint(int xc,int yc,int r)
{
    int x=0,y=r,P;
    P= 1-r;
    while(x<y)
    {
        putpixel(xc+x, yc+y, WHITE);
        putpixel(xc-x, yc+y, WHITE);
        putpixel(xc+x, yc-y, WHITE);
        putpixel(xc-x, yc-y, WHITE);
        putpixel(xc+y, yc+x, WHITE);
        putpixel(xc-y, yc+x, WHITE);
        putpixel(xc+y, yc-x, WHITE);
        putpixel(xc-y, yc-x, WHITE);
        if(P<0)
        {
            P = P+(2*x)+1;
            x++;
        }
        else
        {
            P=P+2*(x-y)+1;
             x++;
            y--;
        }
    }
}


OUTPUT: 



Comments

Popular posts from this blog

HOW TO SETUP CODE BLOCKS FOR GRAPHICS PROGRAMS

Smile emoji using graphics in Java using Applet