Friday, December 4, 2009

Pie Chart

#include
#include
#include
#include
#include"math.h"
#define round(a) (int)(a+0.5)
void pie(int stangle,int endangle,int radius,int z)
{
int i,x,y,x1,y1;
for(radius=0;radius<100;radius++)
{
for(i=stangle;i{
x=getmaxx()/2+round(radius*cos((3.142*i)/180));
y=getmaxy()/2+round(radius*sin((3.142*i)/(180)));
putpixel(x,y,z);
}
}
}

int main(void)
{
/* request auto detection */
int gdriver, gmode, errorcode,sum=0,i,n,data[10],z=1;
detectgraph(&gdriver,&gmode);
int stangle = 0, endangle = 0, radius = 100;
printf("enter the no. of data");
scanf("%d",&n);
one:
sum=0;
for(i=0;i { printf("\nenter the %age of data");
scanf("%d",&data[i]);
}
for(i=0;i sum=sum+data[i];

if(sum!=100)
{
printf("error in data entry ");
getch();
clrscr();
goto one;
}
/* initialize graphics and local variables */
initgraph(&gdriver,&gmode, "..\\bgi");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

/* set fill style and draw a pie slice */
outtextxy(100,100,"PIE CHART");
for(i=0;i {
stangle=endangle;
endangle=stangle+round((data[i]*360)/100);
pie(stangle, endangle, radius,z);
z++;
}
/* clean up */
getch();
closegraph();
return 0;
}

Fan with Regulator

#include
#include
#include
#include
#include
#define pi 3.142857
#define ROUND(a) (int)(a+0.5)
void pie(int sa,int ea,int xc,int yc,int r)
{ double a1,a2;int x,y;
a1=(pi*sa)/180;
x=xc+r*cos(a1);
y=yc+r*sin(a1);
line(xc,yc,x,y);
a2=-(pi*ea)/180;
x=xc+r*cos(a2);
y=yc+r*sin(a2);
line(xc,yc,x,y);
}



void runfan(int d)
{ int i=0,sa=0,ea=1;

while(!kbhit())
{delay(d);
cleardevice();
circle(100,100,50);
line(90,150,90,250);
line(110,150,110,250);
line(40,250,40,300);
line(160,250,160,300);
line(40,300,160,300);
line(40,250,160, 250);
outtextxy(45,275,"0");
outtextxy(75,275,"1");
outtextxy(105,275,"2");
outtextxy(135,275,"3");

pie(sa,ea,100,100,50);
pie(sa+150,ea+150,100,100,50);
pie(sa+300,ea+300,100,100,50);

i++;
sa=sa-10;ea=ea+10;
}
}

void main()
{int d=0,cho;

int gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
runfan(0);
do{

printf("\n enter speed of regulator");
scanf("%d",&d);
switch(d)
{case 0: runfan(d);
break;
case 1:
runfan(70);break;
case 2: runfan(50);break;
case 3: runfan(20);break;
}
printf("\n press 1 to continue");
scanf("%d",&cho);
}while(cho==1);


getch();
}

Bezier Curve

#include
#include

void circl(int x, int y, int r)
{
int x1,y1,p;
x1=0;
y1=r;
p=3-2*r;

while(x1 {
plotcircle(x,y,x1,y1);
if(p<0)
p=p+4*x1+6;
else
{
p=p+4*(x1-y1)+10;
y1=y1-1;
}
x1=x1+1;
}

if(x1==y1)
plotcircle(x,y,x1,y1);
}

plotcircle(int x,int y, int x1, int y1)
{
putpixel(x+x1,y+y1,RED);
putpixel(x-x1,y+y1,RED);
putpixel(x+x1,y-y1,RED);
putpixel(x-x1,y-y1,RED);
putpixel(x+y1,y+x1,RED);
putpixel(x-y1,y+x1,RED);
putpixel(x+y1,y-x1,RED);
putpixel(x-y1,y-x1,RED);
return 0;
}

void cubic(int x1,int y1,int x2, int y2, int x3, int y3, int x4, int y4, int color)
{
int Cx[4],Cy[4];
int ax,ay,bx,by,cx,cy,dx,dy,x,y;
int i=0;
double stepsize,t;
Cx[0]=x1;
Cy[0]=y1;
Cx[1]=x2;
Cy[1]=y2;
Cx[2]=x3;
Cy[2]=y3;
Cx[3]=x4;
Cy[3]=y4;
for(i=0;i<4;i++)
{
setcolor(color);
circl(Cx[i],Cy[i],3);
}
setcolor(color);
ax=-Cx[0]+3*Cx[1]+(-3*Cx[2])+Cx[3];
ay=-Cy[0]+3*Cy[1]+(-3*Cy[2])+Cy[3];
bx=3*Cx[0]+(-6*Cx[1])+3*Cx[2];
by=3*Cy[0]+(-6*Cy[1])+3*Cy[2];
cx=-3*Cx[0]+3*Cx[1];
cy=-3*Cy[0]+3*Cy[1];
dx=Cx[0];
dy=Cy[0];
for(i=0;i<4;i++)
{
putpixel(Cx[i],Cy[i],RED);
}
setcolor(WHITE);
for(i=0;i<3;i++)
{
line(Cx[i],Cy[i],Cx[(i+1)%4],Cy[(i+1)%4]);
}
setcolor(RED);
stepsize=1.0/1000.0;
for(i=0;i<1000;i++)
{
t=stepsize*(double)i;
x=ax*(t*t*t)+bx*(t*t)+cx*t+dx;
y=ay*(t*t*t)+by*(t*t)+cy*t+dy;
putpixel(x,y,RED);
}
}

main()
{
int gd,gm,x,y;
detectgraphI&gd,&gm);
initgraph(&gd,&gm,"..\\bgi ");

cleardevice();
cubic(50,300,100,50,250,50,350,300,6);
getch();
cleardevice();
cubic(50,300,350,300,100,50,250,50,6);
getch();
cleardevice();
cubic(50,300,100,50,350,300,250,50,6);
getch();
cleardevice();
cubic(350,300,50,300,100,50,250,50,6);
getch();

return 0;
}

Incircle of a triangle

#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
float xc,yc;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
//)))))))))))))))
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}

//888888888888888888888888

void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x1,y1,x2,y2,x3,y3;float a,b,c,s,area,r;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x1);
scanf("%d",&y1);

printf("\n enter the co ordinates of the triangle");
scanf("%d",&x2);
scanf("%d",&y2);
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x3);
scanf("%d",&y3);
cleardevice();
ddaline(x1,y1,x2,y2,YELLOW);
ddaline(x2,y2,x3,y3,YELLOW);
ddaline(x3,y3,x1,y1,YELLOW);

a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
r=area/s;
xc=(x1*b+x2*c+x3*a)/(a+b+c);
yc=(y1*b+y2*c+y3*a)/(a+b+c);


while(!kbhit())
{ drawcir(round(xc),round(yc),r,i);
i++;
delay(1);
}


getch();

}


motion of saturn n jupitor around sun!!

#include
#include
#include
#include
#define pi 3.14285
#define round(a)((int)(a+0.5))
int i;
void drawellipse(int xc,int yc,int a,int b)
{ float x,y;int i;
for(i=0;i<360;i++)
{ x=xc+a*cos(i*pi/180);
y=yc+b*sin(i*pi/180);
putpixel(round(x),round(y),11);
}
}
void parcircle(int ,int ,float,int ) ;
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}
void parc(int xc,int yc,float r1,int i)
{ int k=0,r,m; float x,y;
r=r1+60;m=r1+120;
for(k=0;k<360;k++)
{ x=xc+2*r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
delay(10);
cleardevice();
parcircle(xc,yc,r1,11);
i+=5;
drawcir(round(x),round(y),10,i);
x=xc+2*m*cos(pi*k/180);
y=yc+m*sin(pi*k/180);

drawcir(round(x),round(y),20,i);
drawellipse(round(x),round(y),30,10);

}

}
void parcircle(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);

}
}
//========================
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x,y,xc,yc; float r,r1;
i=0;

initgraph(&gd,&gm,"..\\bgi");

printf("\n enter the centre of the sun");
scanf("%d",&x);
scanf("%d",&y);
printf("\n enter the radius of the sun");
scanf("%f",&r);
xc=x;yc=y;

while(!kbhit())
{

parc(xc,yc,r,i);

}




getch();
}

Sirpinsky Triangle

#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
int max=6;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
float minside(int x1,int y1,int x2,int y2,int x3,int y3)
{float a,b,c,s;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));

b=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
if(a s=a;
else
s=b;
if(s>c)
s=c;
return s;
}
//--------------------------------------
void triangle(int x1,int y1,int x2,int y2,int x3,int y3,int i)
{ float mx1,my1,mx2,my2,mx3,my3;
i++;
if(i==max)
return;
else
{
ddaline(x1,y1,x2,y2,YELLOW);
ddaline(x2,y2,x3,y3,YELLOW);
ddaline(x3,y3,x1,y1,YELLOW);
mx1=(x1+x2)/2 ;
my1=(y1+y2)/2;
mx2=(x2+x3)/2 ;
my2=(y2+y3)/2;
mx3=(x1+x3)/2 ;
my3=(y1+y3)/2;
triangle(x1,y1,mx1,my1,mx3,my3,i);
triangle(x2,y2,mx1,my1,mx2,my2,i);
triangle(x3,y3,mx2,my2,mx3,my3,i);
}
}

//)))))))))))))))
void main()
{ int gd,gm; int x1,y1,x2,y2,x3,y3;float small;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x1);
scanf("%d",&y1);

printf("\n enter the co ordinates of the triangle");
scanf("%d",&x2);
scanf("%d",&y2);
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x3);
scanf("%d",&y3);
small=minside(x1,y1,x2,y2,x3,y3);

max=2+(int)small/20;
cleardevice();
triangle(x1,y1,x2,y2,x3,y3,0);

getch();
}

graphs of sin(x)/x,sin(x),&sin(x)+cos(x)

#include
#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
float xc,yc;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,k,steps;
float xinc,yinc,x,y;
dx=x2-x1;dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/(float)steps;
yinc=(float)dy/(float)steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
//)))))))))))))))
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
float x=0,y;
initgraph(&gd,&gm,"..\\bgi");
ddaline(0,120,600,120,11);
while(x<600)
{ x++;
y=120+50*(sin(-pi*x/180)/(pi*x/180));
putpixel(round(x),round(y),12);delay(10);
}
getch();
ddaline(0,240,600,240,11);
x=0;
while(x<600)
{ x++;
y=240+50*(sin(-pi*x/180));
putpixel(round(x),round(y),12);delay(10);
}
getch();
ddaline(0,360,600,360,11);
x=0;
while(x<600)
{ x++;
y=320+10*(sin(-pi*x/180)+cos(-pi*x/180));
putpixel(round(x),round(y),12); delay(10);
}
getch();
}

ball moving on a sphere


#include
#include
#include
#include
#define pi 3.14285
#define round(a)((int)(a+0.5))
int i;
void parcircle(int ,int ,float,int ) ;
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}
void parc(int xc,int yc,float r1,int i)
{ int k=0,r; float x,y;
r=r1+20;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);

cleardevice();
parcircle(xc,yc,r1,11);
i+=5;
drawcir(round(x),round(y),20,i);

}

}
void parcircle(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);

}
}
//========================
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x,y,xc,yc; float r,r1;
i=0;

initgraph(&gd,&gm,"..\\bgi");

printf("\n enter the centre");
scanf("%d",&x);
scanf("%d",&y);
printf("\n enter the radius");
scanf("%f",&r);
xc=x;yc=y;

while(!kbhit())
{

parc(xc,yc,r,i);


}

}

Analog Clock With Pendulum

#include
#include
#include
#include
#include
# define round(a)((int)(a+0.5))
# define pi 3.142857
int psang;
void parcir(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
// delay(10);
}
}
//++++++++++++++++++++++++++++++++++++++++++

void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
//++++++++++++++++++
void drawclock()
{
setcolor(WHITE);
circle(200,200,70);
setcolor(CYAN);
circle(200,200,72);
setcolor(13);
outtextxy(190,135,"12");
outtextxy(195,260,"6");
outtextxy(135,200,"9");
outtextxy(260,200,"3");
setcolor(YELLOW);
ddaline(150,270,150,370,CYAN);
ddaline(150,370,250,370,CYAN);
ddaline(250,370,250,270,CYAN);
}
//++++++++++++++++

void main()
{int gd,gm;
detectgraph(&gd,&gm);
float xh,yh,xs,ys,mx,my,x,y;float hang=-90,mang=-90,sang=-90;
struct time t;

gettime(&t);

hang+=t.ti_hour*30;
mang+=t.ti_min*6;
sang+=t.ti_sec*0.1;
psang=60;
printf("%d: %d: %d",t.ti_hour,t.ti_min,t.ti_sec);
initgraph(&gd,&gm,"..\\bgi");
while(!kbhit())
{ cleardevice();
drawclock(); // gettime(&t); printf("%d: %d: %d",t.ti_hour,t.ti_min,t.ti_sec);
xh=200+20*cos(pi*hang/180);
yh=200+20*sin(pi*hang/180);
ddaline(200,200,round(xh),(yh),YELLOW);
mx=200+40*cos(pi*mang/180);
my=200+40*sin(pi*mang/180);
ddaline(200,200,round(mx),round(my),RED);
xs=200+60*cos(pi*sang/180);
ys=200+60*sin(pi*sang/180);
ddaline(200,200,round(xs),round(ys),BLUE);
hang+=(1/120);
mang+=0.1;
sang+=6;
if(psang==60)
{ while(psang!=120)
{
x=200+70*cos(psang*pi/180);
y=270+70*sin(psang*pi/180);
psang+=1;
ddaline(200,270,round(x),round(y),RED);
parcir(round(x),round(y),10,RED);
ddaline(200,270,round(x),round(y),BLACK);
parcir(round(x),round(y),10,BLACK);
delay(5);
}
}
if(psang==120)
{ while(psang!=60)
{
x=200+70*cos(psang*pi/180);
y=270+70*sin(psang*pi/180);
psang-=1;
ddaline(200,270,round(x),round(y),RED);
parcir(round(x),round(y),10,RED);
ddaline(200,270,round(x),round(y),BLACK);
parcir(round(x),round(y),10,BLACK);
delay(5);
}
}
delay(700);
}
getch();
}

TrAnSFoRmAtIoNs..

#include
#include
#include
#include
#include
#define round(a)((int)(a+0.5))
# define pi 3.1428
float a[3][3];b[3],c[3];int n,x[20],y[20];
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float) dx/steps;
yinc=(float) dy/steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
void parcir(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
delay(10);
}

}
void daxis()
{ ddaline(0,240,640,240,WHITE);
ddaline(320,0,320,480,WHITE);
}
//==================
void mulmat()
{ int k,j;


for (j=0;j<3;j++)
{ c[j]=0;
for(k=0;k<3;k++)
{ c[j]+=a[j][k]*b[k];
}
}
}

// **************************
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int cho,ch=1,choice,i,j; int tx,ty,ang;float sx,sy,shx,shy;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
x[i]+=320;
scanf("%d",&y[i]);
y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0]; cleardevice();
printf("\n coordinate axes");
daxis();
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],YELLOW);
}

do{
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ a[i][j]=0;
}
}
printf("\n menu");
printf("\n 1>translation\n2>scaling\n3>rotation \n 4>shearing \n 5>reflection");
scanf("%d",&cho);
switch(cho)
{case 1: printf("\n enter x disp n y disp");
scanf("%d ",&tx);
scanf("%d",&ty);
a[0][0]=1;a[0][2]=tx;
a[1][1]=1;
a[1][2]=ty;
a[2][2]=1;
for(i=0;i { x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];
b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];
x[i]=x[i]+320;
y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0];
for(i=0;i { ddaline(x[i],y[i],x[i+1],y[i+1],RED);
}

// trans(tx,ty);
break;
case 2: printf("\n enter scaling factors in x and y directions");
scanf("%f",&sx);
scanf("%f",&sy);
a[0][0]=sx;
a[1][1]=sy;
a[2][2]=1;
for(i=0;i {
x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];
x[i]=x[i]+320;y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0];
cleardevice();
daxis();
for(i=0;i { ddaline(round(x[i]),round(y[i]),
round(x[i+1]),round(y[i+1]),RED);
}

break;

case 3:
printf("\n enter the angle of rotation(give -ve for anti clckwise)");
scanf("%d",&ang);
ang=(ang*pi/180);
a[0][0]=cos(ang);
a[0][1]=-sin(ang);

a[1][0]=sin(ang);
a[1][1]=cos(ang);
for(i=0;i {x[i]=x[i]-320;y[i]=240-y[i];

b[0]=x[i];b[1]=y[i];b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];
x[i]=x[i]+320;y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0];
cleardevice();
daxis();
for(i=0;i { ddaline(round(x[i]),round(y[i]),
round(x[i+1]),round(y[i+1]),RED);
}


break;
case 4: printf("\n 1>in x dir \n 2> in y dir");

scanf("%d",&choice);
if(choice==1)
{ printf("\n enter shx");
scanf("%f",&shx);
a[0][0]=1;a[0][1]=shx;
a[1][1]=1;
a[2][2]=2;

for(i=0;i { x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];
b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];

x[i]=x[i]+320;y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0];

}
else
{ printf("\n enter shy");
scanf("%f",­);
a[0][0]=1;
a[1][0]=shy;a[1][1]=1;
a[2][2]=1;
for(i=0;i { x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];
b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];

x[i]=x[i]+320;y[i]=240-y[i];
}
x[i]=x[0]; y[i]=y[0];
}

daxis();
for(i=0;i { ddaline(x[i],y[i],x[i+1],y[i+1],RED);
}


break;
case 5:
printf("\n 1>ref abt x axis \n 2>ref abt y axis");
scanf("%d",&choice);
if(choice==1)
{ a[0][0]=1;
a[1][1]=-1;
a[2][2]=1;
for(i=0;i { x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];
b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];

x[i]=x[i]+320;y[i]=240-y[i];
}
x[i]=x[0];y[i]=y[0];
}

else
{
a[0][0]=-1;a[1][1]=1;a[2][2]=1;
for(i=0;i {x[i]=x[i]-320;y[i]=240-y[i];
b[0]=x[i];b[1]=y[i];
b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];

x[i]=x[i]+320;y[i]=240-y[i];
}


x[i]=x[0];y[i]=y[0];
}
cleardevice();
daxis();
for(i=0;i { ddaline(x[i],y[i],x[i+1],y[i+1],RED);
}


break;
default:printf("\n wrong choice");
}
printf("\n press 1 to continue 2 to exit");
scanf("%d",&ch);
}while(ch==1);
getch();
}

Boundary and Flood Filling (recursive and non recursive)

#include
#include
#include
#include
#define round(a)((int)(a+0.5))
struct point{ int xc;int yc;}curpix, stk [9999];
int top=-1;
void push(int x,int y)
{ top++;
stk[top].xc=x;
stk[top].yc=y;
}
void pop()
{ curpix.xc=stk[top].xc;
curpix.yc=stk[top].yc;
top--;
}

void ddaline(int x1,int y1,int x2,int y2,int col)
{
int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(round(x),round(y),col);
for(k=0;k { x+=xinc;
y+=yinc;
putpixel(round(x),round(y),col);
}
}
void bfill(int x,int y,int bclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr!=fclr && curclr!=bclr)
{
putpixel(x,y,fclr);
bfill(x+1,y,bclr,fclr);
bfill(x,y+1,bclr,fclr);
bfill(x-1,y,bclr,fclr);
bfill(x,y-1,bclr,fclr);
}
}

void ffill(int x,int y,int oclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
ffill(x+1,y,oclr,fclr);
ffill(x,y+1,oclr,fclr);
ffill(x-1,y,oclr,fclr);
ffill(x,y-1,oclr,fclr);
}
}

void rff(int x,int y,int oclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr==oclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
void rbf(int x,int y,int bclr,int fclr)
{ int curclr;
while(top!=-1)
{ curclr=getpixel(x,y);
if(curclr!=bclr && curclr!=fclr)
{
putpixel(x,y,fclr);
push(x+1,y);
push(x,y+1);
push(x-1,y);
push(x,y-1);
}
pop();
x=curpix.xc;
y=curpix.yc;

}
}
//*********************************************
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x[10],y[10],n,i,sumx=0,sumy=0,seedx,seedy,old;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
scanf("%d",&y[i]);
sumx+=x[i];sumy+=y[i];
}
x[i]=x[0];y[i]=y[0];
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],2);
}
seedx=sumx/n;
seedy=sumy/n;
old=getpixel(seedx,seedy);
push(seedx,seedy);
rbf(seedx,seedy,2,RED);
getch();
}



Collision Of 2 balls!!

#include
#include
#include
#include
#define pi 3.14285
#define round(a)((int)(a+0.5))
int i,j;

void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}

}
void main()
{int gd,gm; int x1=20,x2=20,xc1=200,xc2=600;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
i=0;
while((xc2-xc1)>40)
{ delay(100);
cleardevice();
drawcir(xc1,200,20,i);
xc1+=x1;
i++;
drawcir(xc2,200,20,j);
xc2-=x2;
j--;

}
cleardevice();
drawcir(xc1,200,20,i);
drawcir(xc2,200,20,j);
delay(100);
i=0;j=0;
while(xc1!=10)
{ delay(100);
cleardevice();


drawcir(xc1,200,20,i);
xc1-=x1;
i--;
drawcir(xc2,200,20,j);
xc2+=x2;
j++;

}
getch();
}

Clip And Rotate A Star*

#include
#include
#include
#include
#include
#define round(a)((int)(a+0.5))
#define pi 3.1428
int xclip=100,x[20],y[20],n,color=0;float a[3][3];b[3],c[3];
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float) dx/steps;
yinc=(float) dy/steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
//==================
void mulmat()
{ int k,j;


for (j=0;j<3;j++)
{ c[j]=0;
for(k=0;k<3;k++)
{ c[j]+=a[j][k]*b[k];
}
}
}

// **************************
void rotate()
{ float ang=-90;int i,j;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ a[i][j]=0;
}
}
ang=(ang*pi/180);
a[0][0]=cos(ang);
a[0][1]=-sin(ang);
a[0][2]=100*(1-cos(ang))+150*sin(ang);
a[1][0]=sin(ang);
a[1][1]=cos(ang);
a[1][2]=150*(1-cos(ang))-100*sin(ang);
for(i=0;i { b[0]=x[i];b[1]=y[i];b[2]=1;
mulmat();
x[i]=c[0];y[i]=c[1];
}
x[i]=x[0];y[i]=y[0];
cleardevice();

for(i=0,color=1;i { ddaline(round(x[i]),round(y[i]),
round(x[i+1]),round(y[i+1]),i+1);
}
}


void clip()
{
int u[20],v[20],i=0,j=0;
float m,c;
for(i=0;i
{ //printf("\n left clip");
if(x[i] else if(x[i]>=xclip && x[i+1]>=xclip)
{ u[j]=x[i];v[j++]=y[i];
}
else
{ m=(float)(y[i+1]-y[i])/(x[i+1]-x[i]);
c=(float)(y[i]-m*x[i]);
if(x[i]>xclip)
{ u[j]=x[i];
v[j++]=y[i];
}
u[j]=xclip;
v[j++]=(int)(m*xclip+c);

}//end of else
}//end of for
ddaline(xclip,0,xclip,400,WHITE);
for(i=0;i { x[i]=u[i];
y[i]=v[i];
}
x[j]=x[0];
y[j]=y[0];
cleardevice();
ddaline(xclip,0,xclip,400,WHITE);
color+=2;
for(i=0,color=1;i {
ddaline(x[i],y[i],x[i+1],y[i+1],color);
}
getch();
}
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int i=0;
initgraph(&gd,&gm,"..\\bgi");
n=8;
x[0]=50;
x[1]=100;
x[2]= 125;
x[3]=150;
x[4]=200;
x[5]=150;
x[6]=125;
x[7]=100 ;
x[8]=50;
y[0]=175;
y[1]=200;
y[2]= 250;
y[3]=200;
y[4]=175;
y[5]=150;
y[6]=100;
y[7]=150;
y[8]=175;
for(i=0,color=1;i {
ddaline(x[i],y[i],x[i+1],y[i+1],color);
}
ddaline(xclip,0,xclip,400,WHITE);
printf("\n press enter to clip");
getch();
clip();
printf("\n press enter to rotate");
getch();
rotate();
ddaline(xclip,0,xclip,400,WHITE);
printf("\n press enter to clip");
getch();
clip();
getch();
cleardevice();
printf("\n final figure");
for(i=0,color=1;i {
ddaline(x[i],y[i],x[i+1],y[i+1],color);
}
getch();
}

Circumcircle of a triangle

#include
#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))
float xc,yc;
//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,k,steps;
float xinc,yinc,x,y;
dx=x2-x1;dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/(float)steps;
yinc=(float)dy/(float)steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
//)))))))))))))))
void parcir(int xc,int yc,float r,int sang,int eang,int col)
{ int k;
float x,y;

for(k=sang;k { x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
}
}

void drawcir(int x,int y,float r,int sang)
{ int k=0;
int eang;
eang=sang+120;
while(k!=3)
{
parcir(x,y,r,sang,eang,(k+1));
sang=eang;
eang+=120;
k++;
}
}
//)))))))))))))))))

//888888888888888888888888
void pcircum(int x1,int y1,int x2,int y2,int x3,int y3,int flag)
{ float r;int i=0;
if(flag==1)
{if(y1 { r=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
r=r/2;
xc=(x1+x3)/2;
yc=(y1+y3)/2;
}
else
{ r=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
r=r/2;
xc=(x2+x3)/2;
yc=(y2+y3)/2;
}
}
if(flag==2)
{if(y1 { r=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
r=r/2;
xc=(x1+x2)/2;
yc=(y1+y2)/2;
}
else
{ r=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
r=r/2;
xc=(x2+x3)/2;
yc=(y2+y3)/2;
}
}
if(flag==3)
{if(y2 { r=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
r=r/2;
xc=(x1+x2)/2;
yc=(y1+y2)/2;
}
else
{ r=sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
r=r/2;
xc=(x1+x3)/2;
yc=(y1+y3)/2;
}
}
while(!kbhit())
{ drawcir(round(xc),round(yc),r,i);
i++;
delay(1);
}
}
//;;;;;;;;;;;;;;;;
float findy(int x1,int y1,int x2,int y2,int xf)
{ float y,m1,c1,midx,midy;
m1=(float)(y1-y2)/(float)(x1-x2);
m1=(float)(-1/m1);
midx=(x1+x2)/2;
midy=(y1+y2)/2;
c1=(float)(midy-m1*midx);
y=(float)(m1*xf+c1);
return y;
}
//000000000000000000000000000
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x1,y1,x2,y2,x3,y3,i,flag=0;float r,xf,yf;
int mx1,my1,mx2,my2;
float m1,m2,c1,c2;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x1);
scanf("%d",&y1);

printf("\n enter the co ordinates of the triangle");
scanf("%d",&x2);
scanf("%d",&y2);
printf("\n enter the co ordinates of the triangle");
scanf("%d",&x3);
scanf("%d",&y3);
cleardevice();
ddaline(x1,y1,x2,y2,YELLOW);
ddaline(x2,y2,x3,y3,YELLOW);
ddaline(x3,y3,x1,y1,YELLOW);
if(x1==x2)
{pcircum(x1,y1,x2,y2,x3,y3,1);
exit(0);
}
if(x1==x3)
{pcircum(x1,y1,x2,y2,x3,y3,2);
exit(0);
}
if(x2==x3)
{pcircum(x1,y1,x2,y2,x3,y3,3);
exit(0);
}
m1=(float)(y1-y2)/(float)(x1-x2);
m2=(float)(y2-y3)/(float)(x2-x3);
if(m1!=0) m1=(float)(-1/m1);
else
{ xf=(x1+x2)/2;
flag=1;
yf= findy(x1,y1,x3,y3,xf);
r=sqrt((xf-x3)*(xf-x3)+(yf-y3)*(yf-y3));
}
if(m2!=0) m2=(float)(-1/m2);
else
{ xf=(x2+x3)/2;
flag=1;
yf= findy(x1,y1,x2,y2,xf);
r=sqrt((xf-x3)*(xf-x3)+(yf-y3)*(yf-y3));
}
if(!flag)
{
mx1=(x1+x2)/2;
my1=(y1+y2)/2;
c1=(float)(my1-m1*mx1);
mx2=(x2+x3)/2;
my2=(y2+y3)/2;
c2=(float)(my2-m2*mx2);
xc=(float)(c2-c1)/(float)(m1-m2);
yc=(float)(m1*xc+c1);

r=sqrt((xc-x3)*(xc-x3)+(yc-y3)*(yc-y3));
}
i=0;


while(!kbhit())
{ if(flag)
drawcir(round(xf),round(yf),r,i);
else
drawcir(round(xc),round(yc),r,i);
i++;
delay(1);
}
getch();
}

Circle Drawing Algorithms

#include
#include
#include
#include
# define round(a)((int)(a+0.5))
# define pi 3.142857
void eqcircle(int ,int,float,int);
void parcir(int,int,float,int);
void midcir(int,int,float,int);
void brescir(int,int,float,int);
void plot(int ,int,int,int,int);

void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int xc,yc,ch;float r;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the centre");
scanf("%d",&xc);
scanf("%d",&yc);
printf("\n enter the radius");
scanf("%f",&r);
printf("\n menu..\n1>equation\n2>parametric\n3>midpoint\n4>bresenham\n");
printf("\n enter choice");
scanf("%d",&ch);
switch(ch)
{ case 1:
eqcircle(xc,yc,r,2);
break;
case 2: parcir(xc,yc,r,10);
break;
case 3: midcir(xc,yc,r,3);
break;
case 4:
brescir(xc,yc,r,4);

break;
default: printf("\n wrong choice");
}
getch();
}
void eqcircle(int xc,int yc, float r,int col)
{ float x,y;
x=0;y=r;
while(x { plot(round(x),round(y),xc,yc,col);

y=sqrt(r*r-x*x);
x++;
}
}
void parcir(int xc,int yc,float r,int col)
{ int k=0; float x,y;
for(k=0;k<360;k++)
{ x=xc+r*cos(pi*k/180);
y=yc+r*sin(pi*k/180);
putpixel(round(x),round(y),col);
delay(10);
}
}

void midcir(int xc,int yc,float r,int col)
{ float p,x,y;
x=0;y=r; p=(5/4)-r;

while(x { if(p<0)
{p+=2*x+1;
}
else
{ p+=2*(x-y)+1;
y--;
}
x++;
plot(round(x),round(y),xc,yc,col);
}
}


//**************************************

void brescir(int xc,int yc,float r,int col)
{
float Dd,delta,x,y;
x=0;y=r;Dd=2*(1-r);

while(x { if(Dd<0)
{ delta=2*Dd+2*y-1;
if(delta<0)
{ x++;
Dd+=2*x+1;
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}
}
if(Dd>0)
{delta=2*x-2*Dd+1;
if(delta<0)
{ y--;
Dd+=-2*y+1;
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}
}
else
{ x++;
y--;
Dd+=2*x-2*y+2;
}

plot(round(x),round(y),xc,yc,col);

}//end of while


}//end of fun


void plot(int x,int y,int xc,int yc,int col)
{ putpixel(xc+x,yc+y,col);
putpixel(xc-x,yc+y,col);
putpixel(xc+x,yc-y,col);
putpixel(xc-x,yc-y,col);
putpixel(xc+y,yc+x,col);
putpixel(xc-y,yc+x,col);
putpixel(xc+y,yc-x,col);
putpixel(xc-y,yc-x,col);
}

BARGRAPH

#include
#include
#include
#include
# define pi 3.142857
# define round(a)((int)(a+0.5))

//))))))))))))))))
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}
//00000000000
void bfill(int x,int y,int bclr,int fclr)
{ int curclr;
curclr=getpixel(x,y);
if(curclr!=fclr && curclr!=bclr)
{
putpixel(x,y,fclr);
bfill(x+1,y,bclr,fclr);
bfill(x,y+1,bclr,fclr);
bfill(x-1,y,bclr,fclr);
bfill(x,y-1,bclr,fclr);
}
}
//------------------
void main()
{ int gd,gm; int y[10],i,x,n,y1,sx,sy;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of data\n");
scanf("%d",&n);
printf("\n enter the data(<30)as stack overflows\n");
for(i=0;i {

scanf("%d",&y[i]);
y[i]=5*y[i];
}
cleardevice();
outtextxy(200,20,"SCALE: xaxis:1div=20 yaxis:1div=5");
ddaline(30,0,30,300,11);
ddaline(30,300,600,300,11);
y1=300;
while(y1>0)
{ y1-=5;
ddaline(30,y1,35,y1,11);
}
x=50;
for(i=0;i { ddaline(x,300,x,300-y[i],11);
ddaline(x,300-y[i],x+20,300-y[i],11);
ddaline(x+20,300-y[i],x+20,300,11);
sx=x+10;
sy=300-y[i]/2;
bfill(sx,sy,11,i+1);
x+=40;
}
getch();
}

Sutherland Hodgeman Polygon Clipping

#include
#include
#include
#include
#include
#define round(a)((int)(a+0.5))
int xmin=50,ymin=50,xmax=300,ymax=300,x[20],y[20];
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float) dx/steps;
yinc=(float) dy/steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
void drawin()
{ ddaline(xmin,ymin,xmax,ymin,5);
delay(2);
ddaline(xmax,ymin,xmax,ymax,5);
delay(2);
ddaline(xmax,ymax,xmin,ymax,5);
delay(2);
ddaline(xmin,ymax,xmin,ymin,5);
}
int clip(int n,int d,int col)
{
int u[20],v[20],i=0,j=0;
float m,c;
for(i=0;i { if(d==1)
{ //printf("\n left clip");
if(x[i] else if(x[i]>=xmin && x[i+1]>=xmin)
{ u[j]=x[i];v[j++]=y[i];
}
else
{ m=(float)(y[i+1]-y[i])/(x[i+1]-x[i]);
c=(float)(y[i]-m*x[i]);
if(x[i]>xmin)
{ u[j]=x[i];
v[j++]=y[i];
}
u[j]=xmin;
v[j++]=(int)(m*xmin+c);

}//end of else
}//end of if
//^^^^^^^^^^^^^^^
if(d==2)
{//printf("\n right clip");
if(x[i]>xmax && x[i+1]>xmax);
else if(x[i]<=xmax && x[i+1]<=xmax)
{ u[j]=x[i];v[j++]=y[i];
}
else
{ m=(float)(y[i+1]-y[i])/(x[i+1]-x[i]);
c=(float)(y[i]-m*x[i]);
if(x[i] { u[j]=x[i];
v[j++]=y[i];
}
u[j]=xmax;
v[j++]=(int)(m*xmax+c);

}//end of else
}//end of if
//********************
if(d==3)
{ //printf("\n bottom clip");
if(y[i]>ymax&&y[i+1]>ymax);
else if(y[i]<=ymax && y[i+1]<=ymax)
{ u[j]=x[i];
v[j++]=y[i];
}
else
{ if(x[i]==x[i+1])
{ if(y[i]<=ymax)
{ u[j]=x[i];
v[j++]=y[i];
}
u[j]=x[i];
v[j++]=ymax;
}
else
{
m=(float)(y[i+1]-y[i])/(x[i+1]-x[i]);
c=(float)(y[i]-m*x[i]);
if(y[i]<=ymax)
{u[j]=x[i];v[j++]=y[i];
}
u[j]=(int)(ymax-c)/m;
v[j++]=ymax;
}
} //end of else
}//end of if
//*******************************
if(d==4)
{ //printf("\n top clip");
if(y[i] else if(y[i]>=ymin && y[i+1]>=ymin)
{ u[j]=x[i];
v[j++]=y[i];
}
else
{ if(x[i]==x[i+1])
{ if(y[i]>=ymin)
{ u[j]=x[i];
v[j++]=y[i];
}
u[j]=x[i];
v[j++]=ymin;
}
else
{
m=(float)(y[i+1]-y[i])/(x[i+1]-x[i]);
c=(float)(y[i]-m*x[i]);
if(y[i]>=ymin)
{u[j]=x[i];v[j++]=y[i];
}
u[j]=(int)(ymin-c)/m;
v[j++]=ymin;
}
} //end of else
}//end of if
}//end of for
//*******************************
drawin();
for(i=0;i { x[i]=u[i];
y[i]=v[i];
}
x[j]=x[0];
y[j]=y[0];

for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],col);
}
getch();
return(j);
}

// **************************
void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int n,q,r,s,t,i;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
scanf("%d",&y[i]);
}
x[i]=x[0];y[i]=y[0];
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],WHITE);
}
drawin();
printf("\n press enter to clip");
getch();cleardevice();
printf("\n left clip");
q=clip(n,1,1);getch();
printf("\n right clip");
r=clip(q,2,2);getch();
printf("\n bottom clip");
s=clip(r,3,3);getch() ;
printf("\n top clip");
t=clip(s,4,4);cleardevice();getch();

for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],WHITE);
}
drawin();
getch();
}

Sutherland Cohen Line Clipping

#include
#include
#include
#include
#define round(a)((int)(a+0.5))
int xmin=50,ymin=50,xmax=300,ymax=300;
void dda(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,steps,k;
float x,y,xinc,yinc;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float) dx/steps;
yinc=(float) dy/steps;
x=x1;y=y1;
for(k=0;k { putpixel(round(x),round(y),col);
x+=xinc;
y+=yinc;
}
}
void drawin()
{ dda(xmin,ymin,xmax,ymin,2);
delay(2);
dda(xmax,ymin,xmax,ymax,2);
delay(2);
dda(xmax,ymax,xmin,ymax,2);
delay(2);
dda(xmin,ymax,xmin,ymin,2);
}
void clip(int xa,int ya,int xb,int yb)
{ int a[4],b[4],c[4],i;
int x1,x2,y1,y2,dy=yb-ya,dx=xb-xa;

float m=(float)(dy/dx),c1=(float)(m*xa-ya);
printf("\n %d %d %d %d",dx,dy,m,c);
if(xa==xb)
{ if(xaxmax||(ya>ymax&&yb>ymax)||(ya { printf("\outside the window");
cleardevice();
drawin();
}
else
{ if(ya>ymax)
ya=ymax;
if(yb>ymax)
yb=ymax;
if(ya ya=ymin;
if(yb yb=ymin;
}
dda(xa,ya,xb,yb,8);
drawin();
}
//*****************************
else
{


for(i=0;i<4;i++)
{ a[i]=b[i]=c[i]=0;}
if(xa if(xa>xmax) a[2]=1;
if(ya if(ya>ymax) a[0]=1;
if(xb if(xb>xmax) b[2]=1;
if(yb if(yb>ymax) b[0]=1;

for(i=0;i<4;i++)
{ c[i]=a[i]&&b[i];

}
if(c[0]==0 && c[1]==0 && c[2]==0 && c[3]==0)
{ if(a[0]==0 && a[1]==0 && a[2]==0 && a[3]==0 &&
b[0]==0 && b[1]==0 && b[2]==0 && b[3]==0)
{ printf("\n the line is totally visible");
dda(xa,ya,xb,yb,WHITE);
drawin();
}
else
{ printf("\n line is partially visible");


printf("\n %d %d ",m,c);
if(a[3]==1&&a[2]==0)
{
x1=xmin;y1=(int)(m*xmin+c1);
printf("\n %d %d ",x1,y1);
}
else if(b[3]==1&&b[2]==0)
{
x2=xmin;y2=(int)(m*xmin+c1);
}
if(a[3]==0&&a[2]==1)
{
x1=xmax;y1=(int)(m*xmax+c1);
}
else if(b[3]==0&&b[2]==1)
{
x2=xmax;y2=(int)(m*xmax+c1);
}
if(a[1]==1 && a[0]==0)
{ x1=(int)(ymin-c1)/m;
y1=ymin;
}
if(b[1]==1 && b[0]==0)
{ x2=(int)(ymin-c1)/m;
y2=ymin;
}
if(a[1]==0 && a[0]==1)
{ x1=(int)(ymax-c1)/m;
y1=ymax;
}
if(b[1]==0 && b[0]==1)
{ x2=(int)(ymax-c1)/m;
y2=ymax;
}

printf("\n %d %d %d %d ", x1,y1,x2,y2);
dda(x1,y1,x2,y2,WHITE);
drawin();
}//end of else
}//end of if
else
{printf("\n the line is invisible");
drawin();
}
}//end of else
}//end of fun
void main()
{int gd,gm;

detectgraph(&gd,&gm);
int xa,ya,xb,yb;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter starting x n y cordinates for line");
scanf("%d",&xa);
scanf("%d",&ya);
printf("\n enter ending x n y cordinates for line");
scanf("%d",&xb); scanf("%d",&yb);
/* printf("\n enter xmin n ymin cordinates for line");//use it if u
//want to define yr own dimentions for window
scanf("%d",&xmin); scanf("%d",&ymin);
printf("\n enter xmax n ymax cordinates for line");
scanf("%d",&xmax);
scanf("%d",&ymax);*/
drawin();
dda(xa,ya,xb,yb,WHITE);
printf("\n press enter to clip");
getch();
cleardevice();
clip(xa,ya,xb,yb);
getch();
}


DDA and BRESENHAM'S line drawing

#include
#include
#include
#include
# define round(a)((int)(a+0.5))
void ddaline(int,int,int,int,int);
void bresenham(int,int,int,int,int);

void main()
{ int gd,gm;
detectgraph(&gd,&gm);
int x[10],y[10],n,i;
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the no of sides of polygon\n");
scanf("%d",&n);
printf("\n enter the end points of polygon\n");
for(i=0;i {
scanf("%d",&x[i]);
scanf("%d",&y[i]);
}
x[i]=x[0];y[i]=y[0];
for(i=0;i {
ddaline(x[i],y[i],x[i+1],y[i+1],i);
}
getch();

/* printf("\n enter the 2 end points x1,y1,x2,y2\n");
scanf("%d",&x1);
scanf("%d",&y1);
scanf("%d",&x2);
scanf("%d",&y2);
bresenham(x1,y1,x2,y2,3);*/
getch();

}
void ddaline(int x1,int y1,int x2,int y2,int col)
{ int steps,dx,dy,i;float xinc,yinc,x,y;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
x=x1;y=y1;
putpixel(x,y,col);
for(i=0;i {
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),col);
}
}

void bresenham(int x1,int y1,int x2,int y2,int col)
{ int dx,dy,p,xend,b,s;float x,y;
dx=abs(x2-x1);
dy=abs(y2-y1);
if(x1>x2)
{ x=x2;y=y2;xend=x1;}
else
{x=x1;y=y1;xend=x2;}
p=2*(dy)-dx;
while(x { putpixel(round(x),round(y),col);

if(p<0)
{ p=p+2*dy;
}
else
{ y++;
p=p+2*(dy-dx);
}
x++;
}
if(x1==x2)
{ if(y1>y2)
{b=y1;s=y2;}
else
{b=y2;s=y1;}
while(s { putpixel(round(x1),round(s),col);
s++;
}
}
}