Sunday, February 12, 2017

Draw circle using BRESENHAM approach with C/C++ using graphics.h

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void circlepoints(int,int,int);
void main()
{
    int r,gm,gd=0,value=4,x,y;
    float d;
    initgraph(&gd,&gm,"");
    printf("enter the radius of the circle");
    scanf("%d",&r);
    cleardevice();
    line(320,0,320,480);
    line(0,240,640,240);
    x=0;
    y=r;
    d=3-2*r;
    while(x<=y)
    {
        if(d<0)
        {
            d+=4*x+6;
            x++;
        }
        else
        {
            d+=4*(x-y)+10;
            y--;
            x++;
        }
        circlepoints(x,y,value);
    }
    getch();
    closegraph();
}
void circlepoints(int a,int b,int v)
{
    putpixel(320+a,240-b,v);
    putpixel(320+b,240-a,v);
    putpixel(320+a,240+b,v);
    putpixel(320+b,240+a,v);
    putpixel(320-a,240-b,v);
    putpixel(320-b,240-a,v);
    putpixel(320-a,240+b,v);
    putpixel(320-b,240+a,v);
}

No comments:

Post a Comment