### Draw a ellipse using mid point ellipse drawing algorithm

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

void bressCir(int);
void circlepoints(int, int);

void main()
{
int gdriver=DETECT, gmode, r;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();

scanf("%d",&r);
bressCir(r);

getch();
closegraph();
}

void bressCir(int r)
{
int x=0, y=r , d=1-r;
circlepoints(x,y);

while(x <= y)
{
if(d < 0)
d += 4*x + 3;
else
{
d += 4*(x-y) + 10;
y--;
}
x++;
circlepoints(x,y);
}
}

void circlepoints(int x, int y)
{
putpixel(319+x,239+y,11);
putpixel(319+x,239-y,11);
putpixel(319-x,239+y,11);
putpixel(319-x,239-y,11);
putpixel(319+y,239+x,11);
putpixel(319+y,239-x,11);
putpixel(319-y,239+x,11);
putpixel(319-y,239-x,11);
}

### Draw a circle using bresenham’s circle drawing algorithm

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

void bressCir(int);
void circlepoints(int, int);

void main()
{
int gdriver=DETECT, gmode, r;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();

scanf("%d",&r);
bressCir(r);

getch();
closegraph();
}

void bressCir(int r)
{
int x=0, y=r , d=1-r;
circlepoints(x,y);

while(x <= y)
{
if(d < 0)
d += 4*x + 3;
else
{
d += 4*(x-y) + 10;
y--;
}
x++;
circlepoints(x,y);
}
}

void circlepoints(int x, int y)
{
putpixel(319+x,239+y,11);
putpixel(319+x,239-y,11);
putpixel(319-x,239+y,11);
putpixel(319-x,239-y,11);
putpixel(319+y,239+x,11);
putpixel(319+y,239-x,11);
putpixel(319-y,239+x,11);
putpixel(319-y,239-x,11);
}

### Draw a circle using mid point circle drawing algorithm in Second Order

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

void bressCir(int);
void circlepoints(int, int);

void main()
{
int gdriver=DETECT, gmode, r;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();

scanf("%d",&r);
bressCir(r);

getch();
closegraph();
}

void bressCir(int r)
{
int x=0, y=r , d=1-r;
circlepoints(x,y);

while(x <= y)
{
if(d < 0)
d += 4*x + 3;
else
{
d += 4*(x-y) + 10;
y--;
}
x++;
circlepoints(x,y);
}
}

void circlepoints(int x, int y)
{
putpixel(319+x,239+y,11);
putpixel(319+x,239-y,11);
putpixel(319-x,239+y,11);
putpixel(319-x,239-y,11);
putpixel(319+y,239+x,11);
putpixel(319+y,239-x,11);
putpixel(319-y,239+x,11);
putpixel(319-y,239-x,11);
}

### Draw a circle using mid point circle drawing algorithm in First Order

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

void midpoint(int);
void circlepoints(int, int);

void main()
{
int gdriver=DETECT, gmode, r;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

cleardevice();

scanf("%d",&r);

midpoint(r);

getch();
closegraph();
}
void midpoint(int r)
{
int x=0, y=r , d=1-r;
int de=3, dse=-2*r+5;

circlepoints(x,y);

while(y > x)
{
if(d < 0)
{
d += de;
dse += 2;
}
else
{
d += dse;
dse += 4;
y--;
}
de += 2;
x++;
circlepoints(x,y);
}
}

void circlepoints(int x, int y)
{
putpixel(319+x,239+y,11);
putpixel(319+x,239-y,11);
putpixel(319-x,239+y,11);
putpixel(319-x,239-y,11);
putpixel(319+y,239+x,11);
putpixel(319+y,239-x,11);
putpixel(319-y,239+x,11);
putpixel(319-y,239-x,11);
}

### Draw a line using bresenham’s line drawing algorithm with all cases

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

void bres(int, int, int, int);

void main()
{
int gdriver = DETECT, gmode, x1,x2,y1,y2;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

cleardevice();

printf("Enter the values for Starting point (X1,Y1) : ");
scanf("%d %d",&x1,&y1);

printf("Enter the values for Ending   point (X2,Y2) : ");
scanf("%d %d",&x2,&y2);

bres(x1,y1,x2,y2);

getch();

closegraph();
}

void bres(int xa, int ya, int xb, int yb)
{
int dx = abs(xb-xa), dy = abs(yb-ya);
int Ty = 2*dy, Tyx = 2*(dy-dx), p = 2*dy-dx;
int x, y, xEnd;

if(xa > xb)
{
x = xb;
y = yb;
xEnd = xa;
}
else
{
x = xa;
y = ya;
xEnd = xb;
}

putpixel(x,y,14);

while(x < xEnd)
{
x++;
delay(100);

if(p < 0)
p += Ty;
else
{
y++;
p += Tyx;
}

putpixel(x,y,14);
}
}

### Draw a line using mid point line drawing algorithm

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

void midpoint(int, int, int, int);
int midx = 319, midy = 239;

void main()
{
int gdriver=DETECT, gmode, x1, y1, x2, y2;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
cleardevice();

printf("Enter the point (X1,Y1) : ");
scanf("%d%d",&x1,&y1);
printf("Enter the point (X2,Y2) : ");
scanf("%d%d",&x2,&y2);

midpoint(x1,y1,x2,y2);

getch();
closegraph();
}

void midpoint(int x1, int y1, int x2, int y2)
{
int x=x1, y=y1, dx=x2-x1, dy=y2-y1, ca, cb, d, d1, d2;

line(0,midy,2*midx,midy);     line(midx,0,midx,2*midy);
putpixel(midx+x1,midy-y1,14); putpixel(midx+x2,midy-y2,14);
getch();

if(dx > 0)  cb = 1;
else        cb = -1;
if(dy > 0)  ca = 1;
else        ca = -1;

if(abs(dy) < abs(dx))
{
d  = 2*ca*dy - cb*dx;
d1 = 2*ca*dy;
d2 = 2*(ca*dy - cb*dx);
}
else
{
d  = -2*cb*dx + ca*dy;
d1 = -2*cb*dx;
d2 = 2*(ca*dy - cb*dx);
}

putpixel(midx+x,midy-y,4);
while(x!=x2 || y!=y2)
{
if(dx == 0)
y += ca;
else if(abs(dy) <= abs(dx))
{
x += cb;
if(d<0)
d += d1;
else
{
d += d2;
y += ca;
}
}
else
{
y += ca;
if(d>0)
d += d1;
else
{
d += d2;
x += cb;
}
}
putpixel(midx+x,midy-y,4);    delay(10);
}
}