SlideShare a Scribd company logo
1 of 47
1 
Program 1 
WRITE A C PROGRAM TO DRAW LINE BY USING DDA AND BRESENHAM'S 
ALGORITHM. 
# include<stdio.h> 
# include<conio.h> 
# include<graphics.h> 
# include<math.h> 
void dda(float x1,float y1,float x2,float y2) 
{ 
float dx,dy,x=x1,y=y1,m; 
int i; 
dx=x2-x1; 
dy=y2-y1; 
if(abs(dx)>=abs(dy)) 
m=abs(dx); 
else 
m=abs(dy); 
putpixel((int)x,(int)y,15); 
for(i=1;i<=m;i++) 
{ 
x=x+dx/m; 
y=y+dy/m; 
putpixel((int)x,(int)y,15); 
} 
} 
void bress(float x1,float y1,float x2,float y2) 
{ 
int x,y,end,inc=0,p,dx=abs(x2-x1); 
int dy=abs(y2-y1),c=0,current=0; 
if(dx>dy) 
{ 
p=2*dy-dx; 
if(x1<x2) 
{ 
x=x1; 
y=y1; 
end=x2; 
if(y1<y2) 
inc=1;
2 
if(y1>y2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=x1; 
if(y2<y1) 
inc=1; 
if(y2<y1) 
inc=-1; 
} 
while(x<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dy; 
else 
{ 
y=y+inc; 
p=p+2*(dy-dx); 
} 
x++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
} 
void main() 
{ 
float x1,x2,y1,y2; 
int ch; 
int gd=DETECT,gm=DETECT;
3 
initgraph(&gd,&gm," "); 
printf("ENTER THE END POINTS OF LINE (x1,y1,x2,y2): "); 
scanf("%f%f%f%f",&x1,&y1,&x2,&y2); 
printf("CHOOSE ALGORITHM: "); 
printf("n 1-DDA"); 
printf("n 2-BRESENHAM"); 
printf("n YOUR CHOICE IS: "); 
scanf("%d",&ch); 
if(ch==1) 
dda(x1,y1,x2,y2); 
if(ch==2) 
bress(x1,y1,x2,y2); 
getch(); 
closegraph(); 
}
4 
OUTPUT-1
5 
Program 2 
WRITE A C PROGRAM TO DRAW MIDPOINT CIRCLE ALGORITHM. 
# include<stdio.h> 
# include<graphics.h> 
# include<conio.h> 
# include<math.h> 
void bresenham_circle(const int h,const int k,const int r) 
{ 
int x=0,y=r,p=(3-(2*r)); 
// cleardevice(); 
line(320,1,320,480); 
line(1,240,640,240); 
do 
{ 
delay(15); 
putpixel((h+x),(k+y),25); 
putpixel((h+y),(k+x),15); 
putpixel((h+y),(k-x),25); 
putpixel((h+x),(k-y),15); 
putpixel((h-x),(k-y),25); 
putpixel((h-y),(k-x),15); 
putpixel((h-y),(k+x),25); 
putpixel((h-x),(k+y),15); 
x++; 
if(p<0) 
p+=((4*x)+6); 
else 
{ 
y--; 
p+=((4*(x-y))+10); 
} 
} 
while(x<=y); 
} 
void main(void) 
{ 
int driver=VGA,mode=VGAHI,h,k,r;
6 
initgraph(& driver,& mode,"c:tcbgi"); 
printf("n ENTER THE VALUE OF [H-COORDINATE]: "); 
scanf("%d",&h); 
printf("n ENTER THE VALUE OF [K-COORDINATE]: "); 
scanf("%d",&k); 
printf("n ENTER THE VALUE OF THE RADIUS: "); 
scanf("%d",&r); 
bresenham_circle(320+h,240-k,r); 
do 
{ 
r--; 
bresenham_circle(320+h,240-k,r); 
}while(r!=0); 
getche(); 
}
7 
OUTPUT-2
8 
Program 3 
WRITE A C PROGRAM OF WINDOW TO VIEW PORT. 
# include<conio.h> 
# include<stdio.h> 
# include<graphics.h> 
void image(); 
float wxmin,wymin,wxmax,wymax; 
float vxmin,vymin,vxmax,vymax; 
void main() 
{ 
int gd,gm; 
detectgraph(&gd,&gm); 
initgraph(&gd,&gm,"c:tc"); 
printf("nt ENTER THE COORDINATES OF WINDOW: n"); 
printf("nt wxmin,wymin: "); 
scanf("%f%f",&wxmax,&wymax); 
printf("nt ENTER THE COORDINATES OF VIEW: n"); 
printf("nt vxmin,vymin: "); 
scanf("%f%f",&vxmin,&vymin); 
printf("nt vxmax,vymax: "); 
scanf("%f%f",&vxmax,&vymax); 
rectangle(wxmin,wymin,wxmax,wymax); 
rectangle(vxmin,vymin,vxmax,vymax); 
getch(); 
cleardevice(); 
image(); 
getch(); 
} 
void image() 
{ 
float x1,y1,x2,y2,vx1,vx2,vy1,vy2; 
clrscr(); 
printf("nnt ENTER THE COORDINATES OF LINE: "); 
printf("nnt X1 Y1: "); 
scanf("%f%f",&x1,&y1); 
printf("nnt X2 Y2: "); 
scanf("%f%f",&x2,&y2);
9 
rectangle(wxmin,wymin,wxmax,wymax); 
rectangle(vxmin,vymin,vxmax,vymax); 
line(x1,y1,x2,y2); 
vx1=((vxmax-vxmin)/(wxmax-wxmin))*(x1-wxmin)+vxmin; 
vy1=((vxmax-vxmin)/(wxmax-wxmin))*(y1-wxmin)+vxmin; 
vx2=((vxmax-vxmin)/(wxmax-wxmin))*(x2-wxmin)+vxmin; 
vy2=((vxmax-vxmin)/(wxmax-wxmin))*(y2-wxmin)+vxmin; 
line(vx1,vy1,vx2,vy2); 
}
10 
OUTPUT-3
11 
Program 4 
WRITE A C PROGRAM OF COHEN-SUTHERLAND ALGORITHM. 
#include<stdio.h> 
#include<graphics.h> 
typedef unsigned int outcode; 
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; 
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax ) 
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax; 
{ 
int gd,gm; 
outcode code0,code1,codeout; 
int accept = 0, done=0; 
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); 
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); 
do{ 
if(!(code0 | code1)) 
{ accept =1 ; done =1; } 
else 
if(code0 & code1) done = 1; 
else 
{ 
float x,y; 
codeout = code0 ? code0 : code1; 
if(codeout & TOP) 
{ 
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0); 
y = ywmax; 
} 
else 
if( codeout & BOTTOM) 
{ 
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0); 
y = ywmin; 
} 
else
12 
if ( codeout & RIGHT) 
{ 
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); 
x = xwmax; 
} 
else 
{ 
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0); 
x = xwmin; 
} 
if( codeout == code0) 
{ 
x0 = x; y0 = y; 
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); 
} 
else 
{ 
x1 = x; y1 = y; 
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); 
} 
} 
} while( done == 0); 
if(accept) line(x0,y0,x1,y1); 
rectangle(xwmin,ywmin,xwmax,ywmax); 
getch(); 
} 
/*--------------------------------------------------------------------*/ 
int calcode (x,y,xwmin,ywmin,xwmax,ywmax) 
float x,y,xwmin,ywmin,xwmax,ywmax; 
{ 
int code =0; 
if(y> ywmax) 
code |=TOP; 
else if( y<ywmin) 
code |= BOTTOM; 
else if(x > xwmax) 
code |= RIGHT; 
else if ( x< xwmin)
13 
code |= LEFT; 
return(code); 
} 
/*-------------------------------------------------*/ 
void main() 
{ 
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; 
int gd,gm; 
detectgraph(&gd,&gm); 
initgraph(&gd,&gm,"C:TCBGI"); 
printf("nntEnter the co-ordinates of Line :"); 
printf("nntX1 Y1 : "); 
scanf("%f %f",&x1,&y1); 
printf("nntX2 Y2 : "); 
scanf("%f %f",&x2,&y2); 
printf("ntEnter the co_ordinates of window :n "); 
printf("ntxwmin , ywmin : "); 
scanf("%f %f",&xwmin,&ywmin); 
printf("ntxwmax , ywmax : "); 
scanf("%f %f",&xwmax,&ywmax); 
line(x1,y1,x2,y2); 
rectangle(xwmin,ywmin,xwmax,ywmax); 
getch(); 
cleardevice(); 
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); 
getch(); 
closegraph(); 
}
14 
OUTPUT-4
15 
Program 5 
WRITE A C PROGRAM TO DRAW A BEZIER – CURVE. 
#include<stdio.h> 
#include<graphics.h> 
#include<stdlib.h> 
#include<string.h> 
#include<conio.h> 
#include<math.h> 
int *x,*y; 
void bezier(int *x1,int *y1) 
{ 
int xa,ya,za; 
float u=0; 
moveto(*x1,*y1); 
while (u<=1) 
{ 
xa=(*(x1+3)*pow(u,3))+(3*(*(x1+2))*pow(u,2)*(1-u))+ 
(3*(*(x1+1))*u*pow(1-u,2))+(*x1*pow(1-u,3)); 
ya=(*(y1+3)*pow(u,3))+(3*(*(y1+2))*pow(u,2)*(1-u))+ 
(3*(*(y1+1))*u*pow(1-u,2))+(*y1*pow(1-u,3)); 
lineto(xa, ya); 
u += 0.01; 
} 
} 
void main(void) 
{ 
int gd=DETECT, gm, key, xpos, ypos, i = 0; 
char xp[4], yp[4]; 
x = (int *)malloc(sizeof(int) * 4); 
y = (int *)malloc(sizeof(int) * 4); 
//registerbgidriver(EGAVGA_driver); 
initgraph(&gd, &gm, "c:tcbgi"); 
outtextxy(10, 460, "Press Esc to exit."); 
outtextxy(400, 460, "Present position : "); 
moveto(getmaxx() / 2, getmaxy() / 2); 
do 
{
16 
xpos = getx(); 
ypos = gety(); 
itoa(xpos, xp, 10); 
itoa(ypos,yp,10); 
setviewport(550,460,639,479,1); 
clearviewport(); 
outtextxy(0,0,xp); 
outtextxy(40,0,yp); 
setviewport(0,0,639,479,1); 
moveto(xpos,ypos); 
key=getch(); 
if (key==0) key=getch(); 
switch(key) 
{ 
case 72 : ypos--; //up arrow 
moveto(xpos,ypos); 
break; 
case 80 : ypos++; //down arrow 
moveto(xpos,ypos); 
break; 
case 75 : xpos--; //left arrow 
moveto(xpos,ypos); 
break; 
case 77 : xpos++; //right arrow 
moveto(xpos,ypos); 
break; 
case 87 : 
case 119 : ypos-=50; 
moveto(xpos,ypos); 
break; 
case 65 : 
case 97 : xpos-=50; 
moveto(xpos,ypos); 
break; 
case 83 : 
case 115 :xpos+=50; 
moveto(xpos,ypos); 
break; 
case 90 : 
case 122 :ypos+=50; 
moveto(xpos,ypos); 
break; 
case 13 : putpixel(xpos,ypos,15); 
*(x+i)=xpos;
17 
*(y+i)=ypos; 
i++; 
break; 
} 
if (i==4) 
{ 
bezier(x,y); 
i=0; 
} 
} 
while(key!=27); 
closegraph(); 
getch(); 
}
18 
Output-5
19 
Program 6 
WRITE A C PROGRAM TO SHEAR A CUBOID. 
#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
#include<graphics.h> 
void bress(float x1,float y1, float x2,float y2) 
{ 
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; 
if ( dx > dy ) 
{ 
p=2*dy-dx; 
if(x1<x2) 
{ 
x=x1; 
y=y1; 
end=x2; 
if(y1<y2) 
inc=1; 
if(y1>y2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=x1; 
if(y2<y1) 
inc=1; 
if(y2>y1) 
inc=-1; 
} 
while(x<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dy; 
else 
{
20 
y=y+inc; 
p=p+2*(dy-dx); 
} 
x++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
else 
{ 
p=2*dx-dy; 
if(y1<y2) 
{ 
x=x1; 
y=y1; 
end=y2; 
if(x1<x2) 
inc=1; 
if(x1>x2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=y1; 
if(x2<x1) 
inc=1; 
if(x2>x1) 
inc=-1; 
} 
while(y<=end) 
{ 
putpixel(x,y,15); 
if(p<0)
21 
p=p+2*dx; 
else 
{ 
x=x+inc; 
p=p+2*(dx-dy); 
} 
y++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
} 
void main() 
{ 
float x1,x2,a,b,c,d,y1,y2; 
double ch; 
int t=30; 
int gd=DETECT,gm=DETECT; 
initgraph(&gd,&gm,"c:tcbgi"); 
printf("Enter the end pts of the line(x,y)"); 
scanf("%f%f%f%f",&x1,&y1,&x2,&y2); 
bress(x1,y1,x2,y1); 
bress(x2,y1,x2,y2); 
bress(x1,y2,x2,y2); 
bress(x1,y2,x1,y1); 
bress(x1+t,y1+t,x2+t,y1+t); 
bress(x2+t,y1+t,x2+t,y2+t); 
bress(x1+t,y2+t,x2+t,y2+t); 
bress(x1+t,y2+t,x1+t,y1+t); 
bress(x1,y1,x1+t,y1+t); 
bress(x2,y1,x2+t,y1+t);
22 
bress(x1,y2,x1+t,y2+t); 
bress(x2,y2,x2+t,y2+t); 
/* 
a=x1+(10*y1); 
b=x2+(10*y1); 
c=(10*x1)+y1; 
d=(10*x2)+y1;*/ 
bress(x1+50,y1+40,x2+50,y1+40); 
bress(x1+t,y2+t,x1+50,y1+40); 
bress(x2+t,y2+t,x2+50,y1+40); 
bress(x1+50,y1+40,x1,y1); 
bress(x2+50,y1+40,x2,y1); 
getch(); 
closegraph(); 
}
23 
OUTPUT-6
24 
Program 7 
WRITE A C PROGRAM TO DRAW A POLYGON AND PERFORM THE FOLLOWING 
OPERATIONS:- 
ROTATION 
TRANSLATION 
SCALING. 
#include<stdio.h> 
#include<graphics.h> 
#include<stdlib.h> 
#include<conio.h> 
#include<math.h> 
int *x,*y,i,nin; 
float x1,y1,theta; 
void drawpolygon(int *x,int *y) 
{ 
int gd=DETECT,gm,ch=0,x1,y1,theta; 
//registerbgidriver(EGAVGA_driver); 
initgraph(&gd,&gm,""); 
for (i=0;i<(nin-1);i++) 
line(*(x+i),*(y+i),*(x+i+1),*(y+i+1)); 
line(*(x+nin-1),*(y+nin-1),*x,*y); 
getch(); 
closegraph(); 
} 
void translate(float x1,float y1) 
{ 
for (i=0;i<nin;i++) 
{ 
*(x+i)+=x1; 
*(y+i)+=y1; 
} 
} 
void scale(float x1,float y1) 
{ 
int a,b; 
a=*x;
25 
b=*y; 
translate(-a,-b); 
for (i=0;i<nin;i++) 
{ 
*(x+i)*=x1; 
*(y+i)*=y1; 
} 
translate(a,b); 
} 
void rotate(float theta) 
{ 
int a,b,c,d; 
c=*x; 
d=*y; 
translate(-c,-d); 
for (i=0;i<nin;i++) 
{ 
a=(*(x+i)*cos(theta))-(*(y+i)*sin(theta)); 
b=(*(x+i)*sin(theta))+(*(y+i)*cos(theta)); 
*(x+i)=a; 
*(y+i)=b; 
} 
translate(c,d); 
} 
void main(void) 
{ 
int ch; 
x=(int *)malloc(sizeof(int)*10); 
y=(int *)malloc(sizeof(int)*10); 
clrscr(); 
printf("ENTER NUMBER OF SIDES IN POLYGON : "); 
scanf("%d",&nin); 
printf("ENTER THE COORDINATES OF THE VERTICES (x,y) :n"); 
for (i=0;i<nin;i++) 
{ 
printf("(i+1) : "); 
scanf("%d%d",&(*(x+i)),&(*(y+i))); 
} 
drawpolygon(x,y); 
while (ch!=4) 
{ 
printf("YOUR OPTIONS :n");
26 
printf("1)TRANSLATEn"); 
printf("2)SCALEn"); 
printf("3)ROTATEn"); 
printf("4)EXITn"); 
printf("nYOUR CHOICE : "); 
scanf("%d",&ch); 
clrscr(); 
switch(ch) 
{ 
case 1:printf("TRANSLATION IN X-DIRECTION : "); 
scanf("%d",&x1); 
printf("TRANSLATION IN Y-DIRECTION : "); 
scanf("%d",&y1); 
translate(x1,y1); 
drawpolygon(x,y); 
break; 
case 2:printf("SCALING IN X-DIRECTION : "); 
scanf("%f",&x1); 
printf("SCALING IN Y-DIRECTION : "); 
scanf("%f",&y1); 
scale(x1,y1); 
drawpolygon(x,y); 
break; 
case 3:printf("ANGLE OF ROTATION(ANTI-CLOCKWISE IS POSITIVE) : "); 
scanf("%f",&theta); 
theta*=3.1415/180; 
rotate(-theta); 
drawpolygon(x,y); 
break; 
case 4:exit(0); 
} 
} 
}
27 
Output-7 
After translation 
After scaling
28 
After Rotation
29 
Program 8 
WRITE A C PROGRAM TO DRAW A RECTANGLE BY USING BRESENHAM AND DDA 
ALGORITHM. 
#include<stdio.h> 
#include<conio.h> 
#include<graphics.h> 
#include<math.h> 
void dda(float x1,float y1,float x2,float y2) 
{ 
float dx,dy,x=x1,y=y1,m; 
int i; 
dx=x2-x1; 
dy=y2-y1; 
if(abs(dx)>=abs(dy)) 
m=abs(dx); 
else m=abs(dy); 
putpixel((int)x,(int)y,15); 
for(i=1;i<m;i++) 
{ 
x=x+dx/m; 
y=y+dy/m; 
putpixel((int)x,(int)y,15); 
} 
} 
void bress(float x1,float y1,float x2,float y2) 
{ 
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; 
if(dx>dy) 
{ 
p=2*dy-dx; 
if(x1<x2) 
{ 
x=x1;y=y1;end=x2; 
} 
if(y1<y2) 
inc=1; 
if(y1>y2) 
inc=-1; 
} 
else 
{ 
x=x2;y=y2;end=x1; 
if(y2<y1)
30 
inc=1; 
if(y2>y1) 
inc=-1; 
} 
while(x<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dy; 
else 
{ 
y=y+inc;p=p+2*(dy-dx); 
} 
x++; 
if(current==0&&c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1&&c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
void main() 
{ 
float x1,x2,y1,y2,x3,y3,x4,y4; 
int ch; 
int gdriver=DETECT, gmode=DETECT; 
initgraph(&gdriver, &gmode, "c:tcBGI"); 
printf("Enter end points of line (x1,y1,x2,y2)"); 
scanf("%f %f %f %f %f %f %f %f",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); 
printf("Choose algorithm(1-DDA 2-BRESENHAM)"); 
scanf("%d",&ch); 
bress(x1,y1,x2,y2); 
dda(x2,y2,x3,y3); 
bress(x4,y4,x3,y3); 
dda(x4,y4,x1,y1); 
getch(); 
closegraph(); 
}
31 
OUTPUT-8
32 
Program 9 
WRITE A C PROGRAM TO DRAW A SOLID AND FIND ITS VANISHING POINT. 
#include<stdio.h> 
#include<conio.h> 
#include<graphics.h> 
void createsolid(); 
void main() 
{ 
int gd = DETECT,gm = DETECT; 
initgraph(&gd,&gm,"c:tcbgi"); 
clrscr(); 
createsolid(); 
getch(); 
closegraph(); 
} 
void createsolid() 
{ 
//The coordinates of first rectangle 
line(250,200,370,200); 
line(370,200,370,320); 
line(370,320,250,320); 
line(250,200,250,320); 
//The coordinates of scond rectangle 
line(280,150,400,150); 
line(400,150,400,270); 
line(400,270,280,270); 
line(280,150,280,270); 
//The coordinates of the lines 
line(250,200,280,150); 
line(370,200,400,150); 
line(370,320,400,270); 
line(250,320,280,270); 
//The coordinates of the X,Y and Z axis 
line(320,300,600,300); 
line(320,50,320,300); 
line(320,300,170,450); 
//Coordinates for vanishing point 
putpixel(190,430,RED); 
}
33 
OUTPUT-9
34 
Program 10 
WRITE A C PROGRAM TO TRANSLATE A LINE BY USING DDA ALGORITHM. 
#include<stdio.h> 
#include<conio.h> 
#include<graphics.h> 
#include<math.h> 
void dda(float x1,float y1,float x2,float y2) 
{ 
float dx,dy,x=x1,y=y1,m; 
int i; 
dx=x2-x1; 
dy=y2-y1; 
if(abs(dx)>=abs(dy)) 
m=abs(dx); 
else 
m=abs(dy); 
putpixel((int)x,(int)y,15); 
for(i=1;i<=m;i++) 
{ x=x+dx/m; 
y=y+dy/m; 
putpixel((int)x,(int)y,15); 
} 
} 
void main() 
{ 
float x11,x12,y11,y12, x21, x22, y21, y22; 
float nw; 
int gd=DETECT,gm=DETECT; 
initgraph(&gd,&gm,"c:tcbin"); 
printf("enter endpoints of line(x1,y1,x2,y2)"); 
scanf("%f%f%f%f",&x11,&x12,&y11,&y12); 
dda(x11,x12,y11,y12); 
printf("Please enter new position"); 
scanf("%f",&nw); 
x21=x11+nw; 
y21=y11+nw; 
x22=x12+nw; 
y22=y12+nw; 
dda(x21,x22,y21,y22); 
getch(); 
closegraph(); 
}
35 
OUTPUT-10
36 
Program 11 
WRITE A C PROGRAM TO ROTATE A TRIANGLE. 
#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
#include<graphics.h> 
void bress(float x1,float y1, float x2,float y2) 
{ 
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; 
if ( dx > dy ) 
{ 
p=2*dy-dx; 
if(x1<x2) 
{ 
x=x1; 
y=y1; 
end=x2; 
if(y1<y2) 
inc=1; 
if(y1>y2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=x1; 
if(y2<y1) 
inc=1; 
if(y2>y1) 
inc=-1; 
} 
while(x<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dy; 
else 
{ 
y=y+inc; 
p=p+2*(dy-dx);
37 
} 
x++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
else 
{ 
p=2*dx-dy; 
if(y1<y2) 
{ 
x=x1; 
y=y1; 
end=y2; 
if(x1<x2) 
inc=1; 
if(x1>x2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=y1; 
if(x2<x1) 
inc=1; 
if(x2>x1) 
inc=-1; 
} 
while(y<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dx; 
else
38 
{ 
x=x+inc; 
p=p+2*(dx-dy); 
} 
y++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
} 
void main() 
{ 
float a[3][3],b[3][3],c[3][3],i,j,k; 
float r=(3.14*45)/180; 
int gd=DETECT,gm=DETECT; 
clrscr(); 
initgraph(&gd,&gm,"c:tcbgi"); 
//IMAGE(triangle) 
bress(0,0,100,100); 
bress(100,100,500,200); 
bress(500,200,0,0); 
a[0][0]=0; 
a[0][1]=0; 
a[0][2]=1; 
a[1][0]=100; 
a[1][1]=100; 
a[1][2]=1; 
a[2][0]=500; 
a[2][1]=200; 
a[2][2]=1; 
b[0][0]=cos(r);
39 
b[0][1]=sin(r); 
b[0][2]=0; 
b[1][0]=-sin(r); 
b[1][1]=cos(r); 
b[1][2]=0; 
b[2][0]=0; 
b[2][1]=0; 
b[2][2]=1; 
//MATRIX MULTIPLICATION 
for(i=0;i<3;i++) 
for(j=0;j<3;j++) 
{ 
c[i][j]=0; 
for(k=0;k<3;k++) 
{ 
c[i][j]+=a[i][k]*b[k][j]; 
} 
} 
//AFTER ROTATION 
line(c[0][0],c[0][1],c[1][0],c[1][1]); 
line(c[1][0],c[1][1],c[2][0],c[2][1]); 
line(c[2][0],c[2][1],c[0][0] ,c[0][1]); 
getch(); 
}
40 
OUTPUT-11
41 
Program 12 
WRITE A C PROGRAM TO DRAW BEZIER – SURFACE. 
#include<stdio.h> 
#include<conio.h> 
#include<graphics.h> 
void main() 
{ 
int gd,gm, x1,x2,x3,x4,y1,y2,y3,y4,y11,y22,y33,y44,i; 
gd=DETECT,gm=DETECT; 
clrscr(); 
initgraph(&gd,&gm,"c:tcbgi"); 
x1=100;x2=130;x3=170;x4=200;y1=150;y2=16 ;y3=160;x4=165; 
line(x1,y1,x2,y2); 
line(x2,y2,x3,y3); 
line(x3,y3,x4,y4); 
y11=y1; 
y22=y2; 
y33=y3; 
y44=y4; 
for(i=0;i<5;i++) 
{ 
y11=y11+10; 
y22=y22+10; 
y33=y33+10; 
y44=y44+10; 
line(x1,y11,x2,y22); 
line(x2,y22,x3,y33); 
line(x3,y33,x4,y44); 
} 
line(x1,y1,x1,y11); 
line(x2,y2,x2,y22); 
line(x3,y3,x3,y33); 
line(x4,y4,x4,y44); 
getch(); 
closegraph(); 
}
42 
OUTPUT-12
43 
Program 13 
WRITE A C PROGRAM TO CONVERT WINDOW COORDINATES IN TO VIEW PORT. 
#include<stdio.h> 
#include<conio.h> 
#include<math.h> 
#include<graphics.h> 
void bress(float x1,float y1, float x2,float y2) 
{ 
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; 
if ( dx > dy ) 
{ 
p=2*dy-dx; 
if(x1<x2) 
{ 
x=x1; 
y=y1; 
end=x2; 
if(y1<y2) 
inc=1; 
if(y1>y2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=x1; 
if(y2<y1) 
inc=1; 
if(y2>y1) 
inc=-1; 
} 
while(x<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dy; 
else 
{ 
y=y+inc; 
p=p+2*(dy-dx);
44 
} 
x++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
else 
{ 
p=2*dx-dy; 
if(y1<y2) 
{ 
x=x1; 
y=y1; 
end=y2; 
if(x1<x2) 
inc=1; 
if(x1>x2) 
inc=-1; 
} 
else 
{ 
x=x2; 
y=y2; 
end=y1; 
if(x2<x1) 
inc=1; 
if(x2>x1) 
inc=-1; 
} 
while(y<=end) 
{ 
putpixel(x,y,15); 
if(p<0) 
p=p+2*dx; 
else
45 
{ 
x=x+inc; 
p=p+2*(dx-dy); 
} 
y++; 
if(current==0 && c==10) 
{ 
current=1; 
c=-1; 
} 
if(current==1 && c==6) 
{ 
current=0; 
c=-1; 
} 
c++; 
} 
} 
} 
void main() 
{ 
float x1,x2,x3,x4,y1,y2,y3,y4; 
double ch; 
int t; 
float vx,vy,vx1,vx2,vy2; 
int gd=DETECT,gm=DETECT; 
initgraph(&gd,&gm,"c:tcbgi"); 
outtextxy(250,190,"World coordinates"); 
bress(200,200,400,200); 
bress(200,200,200,400); 
bress(400,200,400,400); 
bress(200,400,400,400); 
outtextxy(23,10,"View port"); 
bress(20,20,100,20); 
bress(20,20,20,100); 
bress(100,20,100,100); 
bress(20,100,100,100); 
//IMAGE(triangle) 
bress(250,250,350,250);
46 
bress(250,250,300,300); 
bress(350,250,300,300); 
//TRANSFORMATION 
vx=(((100-20)*(250-200))/(400-200))+20; 
vy=(((100-20)*(250-200))/(400-200))+20; 
vx1=(((100-20)*(350-200))/(400-200))+20; 
vx2=(((100-20)*(300-200))/(400-200))+20; 
vy2=(((100-20)*(300-200))/(400-200))+20; 
//TRANSFORMED IMAGE 
bress(vx,vy,vx1,vy); 
bress(vx,vy,vx2,vy2); 
bress(vx1,vy,vx2,vy2); 
getch(); 
}
47 
OUTPUT-13

More Related Content

What's hot

Cg my own programs
Cg my own programsCg my own programs
Cg my own programsAmit Kapoor
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithmMani Kanth
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics University of Potsdam
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)Timbal Mayank
 
Turbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse ProgrammingTurbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse ProgrammingHuzaifa Butt
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Alamgir Hossain
 
Segments in Graphics
Segments in GraphicsSegments in Graphics
Segments in GraphicsRajani Thite
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and EllipseComputer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse2013901097
 
Circle generation algorithm
Circle generation algorithmCircle generation algorithm
Circle generation algorithmAnkit Garg
 
Computer graphics basic transformation
Computer graphics basic transformationComputer graphics basic transformation
Computer graphics basic transformationSelvakumar Gna
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignmentAbdullah Al Shiam
 
Computer graphics - bresenham line drawing algorithm
Computer graphics - bresenham line drawing algorithmComputer graphics - bresenham line drawing algorithm
Computer graphics - bresenham line drawing algorithmRuchi Maurya
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithmsKumar
 

What's hot (20)

Cg my own programs
Cg my own programsCg my own programs
Cg my own programs
 
Mid point circle algorithm
Mid point circle algorithmMid point circle algorithm
Mid point circle algorithm
 
Bresenham circle
Bresenham circleBresenham circle
Bresenham circle
 
3d transformation computer graphics
3d transformation computer graphics 3d transformation computer graphics
3d transformation computer graphics
 
2D transformation (Computer Graphics)
2D transformation (Computer Graphics)2D transformation (Computer Graphics)
2D transformation (Computer Graphics)
 
Turbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse ProgrammingTurbo C Graphics and Mouse Programming
Turbo C Graphics and Mouse Programming
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.Lab report for Prolog program in artificial intelligence.
Lab report for Prolog program in artificial intelligence.
 
CS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna UniversityCS8391 Data Structures Part B Questions Anna University
CS8391 Data Structures Part B Questions Anna University
 
Segments in Graphics
Segments in GraphicsSegments in Graphics
Segments in Graphics
 
Computer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and EllipseComputer Graphic - Lines, Circles and Ellipse
Computer Graphic - Lines, Circles and Ellipse
 
Circle generation algorithm
Circle generation algorithmCircle generation algorithm
Circle generation algorithm
 
Computer graphics basic transformation
Computer graphics basic transformationComputer graphics basic transformation
Computer graphics basic transformation
 
DDA algorithm
DDA algorithmDDA algorithm
DDA algorithm
 
Computer graphics lab assignment
Computer graphics lab assignmentComputer graphics lab assignment
Computer graphics lab assignment
 
Computer graphics realism
Computer graphics realismComputer graphics realism
Computer graphics realism
 
Computer graphics - bresenham line drawing algorithm
Computer graphics - bresenham line drawing algorithmComputer graphics - bresenham line drawing algorithm
Computer graphics - bresenham line drawing algorithm
 
Fill area algorithms
Fill area algorithmsFill area algorithms
Fill area algorithms
 
Computer Graphics - clipping
Computer Graphics - clippingComputer Graphics - clipping
Computer Graphics - clipping
 
unit-1-intro
 unit-1-intro unit-1-intro
unit-1-intro
 

Viewers also liked

Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics PracticalNeha Sharma
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALVivek Kumar Sinha
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics labPriya Goyal
 
Computer Graphics Programes
Computer Graphics ProgramesComputer Graphics Programes
Computer Graphics ProgramesAbhishek Sharma
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneBhavesh Shah
 
Computer graphics file
Computer graphics fileComputer graphics file
Computer graphics fileaman1001
 
Computer Graphics 471 Project Report Final
Computer Graphics 471 Project Report FinalComputer Graphics 471 Project Report Final
Computer Graphics 471 Project Report FinalAli Ahmed
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manualAnkit Kumar
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Saikrishna Tanguturu
 
Computer graphics
Computer graphicsComputer graphics
Computer graphicsamitsarda3
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphicsRafi_Dar
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmKapil Pandit
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithmMohamed El-Serngawy
 
Computer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmComputer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmRAJEEV KUMAR SINGH
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clippingavelraj
 
Graphics Programming in C under GNU Linux (Ubuntu distribution)
Graphics Programming in C under GNU Linux (Ubuntu distribution)Graphics Programming in C under GNU Linux (Ubuntu distribution)
Graphics Programming in C under GNU Linux (Ubuntu distribution)Tushar B Kute
 
hidden surface elimination using z buffer algorithm
hidden surface elimination using z buffer algorithmhidden surface elimination using z buffer algorithm
hidden surface elimination using z buffer algorithmrajivagarwal23dei
 

Viewers also liked (20)

Computer Graphics Practical
Computer Graphics PracticalComputer Graphics Practical
Computer Graphics Practical
 
COMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUALCOMPUTER GRAPHICS LAB MANUAL
COMPUTER GRAPHICS LAB MANUAL
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
 
Computer Graphics Programes
Computer Graphics ProgramesComputer Graphics Programes
Computer Graphics Programes
 
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of PuneSE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
 
Graphics Programming in C
Graphics Programming in CGraphics Programming in C
Graphics Programming in C
 
Computer graphics file
Computer graphics fileComputer graphics file
Computer graphics file
 
Computer Graphics 471 Project Report Final
Computer Graphics 471 Project Report FinalComputer Graphics 471 Project Report Final
Computer Graphics 471 Project Report Final
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
Computer Graphics - Bresenham's line drawing algorithm & Mid Point Circle alg...
 
Computer graphics
Computer graphicsComputer graphics
Computer graphics
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphics
 
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithmWap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
 
Circle & curve clipping algorithm
Circle & curve clipping algorithmCircle & curve clipping algorithm
Circle & curve clipping algorithm
 
Computer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithmComputer graphics mini project on bellman-ford algorithm
Computer graphics mini project on bellman-ford algorithm
 
Lecture 2d point,curve,text,line clipping
Lecture   2d point,curve,text,line clippingLecture   2d point,curve,text,line clipping
Lecture 2d point,curve,text,line clipping
 
DBMS Practical File
DBMS Practical FileDBMS Practical File
DBMS Practical File
 
Clipping
ClippingClipping
Clipping
 
Graphics Programming in C under GNU Linux (Ubuntu distribution)
Graphics Programming in C under GNU Linux (Ubuntu distribution)Graphics Programming in C under GNU Linux (Ubuntu distribution)
Graphics Programming in C under GNU Linux (Ubuntu distribution)
 
hidden surface elimination using z buffer algorithm
hidden surface elimination using z buffer algorithmhidden surface elimination using z buffer algorithm
hidden surface elimination using z buffer algorithm
 

Similar to Computer Graphics Lab File C Programs

Computer graphics File for Engineers
Computer graphics File for EngineersComputer graphics File for Engineers
Computer graphics File for Engineersvarun arora
 
Computer Aided Manufacturing Design
Computer Aided Manufacturing DesignComputer Aided Manufacturing Design
Computer Aided Manufacturing DesignV Tripathi
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignmentashikul akash
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C CodeSyed Ahmed Zaki
 
Interpolation graph c++
Interpolation graph c++Interpolation graph c++
Interpolation graph c++rpiitcbme
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graphkinan keshkeh
 
Numerical Methods in C
Numerical Methods in CNumerical Methods in C
Numerical Methods in CAmbili Baby
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdffathimafancyjeweller
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignmentRutvik
 
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」Ken'ichi Matsui
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4Roziq Bahtiar
 

Similar to Computer Graphics Lab File C Programs (20)

Graphics point clipping c program
Graphics point clipping c programGraphics point clipping c program
Graphics point clipping c program
 
Computer graphics File for Engineers
Computer graphics File for EngineersComputer graphics File for Engineers
Computer graphics File for Engineers
 
Caropro
CaroproCaropro
Caropro
 
Computer Aided Manufacturing Design
Computer Aided Manufacturing DesignComputer Aided Manufacturing Design
Computer Aided Manufacturing Design
 
Computer graphics
Computer graphics   Computer graphics
Computer graphics
 
Numerical Method Assignment
Numerical Method AssignmentNumerical Method Assignment
Numerical Method Assignment
 
Assignment on Numerical Method C Code
Assignment on Numerical Method C CodeAssignment on Numerical Method C Code
Assignment on Numerical Method C Code
 
Interpolation graph c++
Interpolation graph c++Interpolation graph c++
Interpolation graph c++
 
Cs580
Cs580Cs580
Cs580
 
2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph2Bytesprog2 course_2014_c9_graph
2Bytesprog2 course_2014_c9_graph
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
C++ TUTORIAL 9
C++ TUTORIAL 9C++ TUTORIAL 9
C++ TUTORIAL 9
 
Numerical Methods in C
Numerical Methods in CNumerical Methods in C
Numerical Methods in C
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
 
Matlab assignment
Matlab assignmentMatlab assignment
Matlab assignment
 
No3
No3No3
No3
 
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
数学カフェ 確率・統計・機械学習回 「速習 確率・統計」
 
Open GL T0074 56 sm4
Open GL T0074 56 sm4Open GL T0074 56 sm4
Open GL T0074 56 sm4
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Cpds lab
Cpds labCpds lab
Cpds lab
 

More from Kandarp Tiwari

Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab FileKandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariKandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariKandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab FileKandarp Tiwari
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front PageKandarp Tiwari
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front pageKandarp Tiwari
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front PageKandarp Tiwari
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab FileKandarp Tiwari
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab FileKandarp Tiwari
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programsKandarp Tiwari
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab FileKandarp Tiwari
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 

More from Kandarp Tiwari (13)

Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front Page
 
Web technology
Web technologyWeb technology
Web technology
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front page
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front Page
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Computer Graphics Lab File C Programs

  • 1. 1 Program 1 WRITE A C PROGRAM TO DRAW LINE BY USING DDA AND BRESENHAM'S ALGORITHM. # include<stdio.h> # include<conio.h> # include<graphics.h> # include<math.h> void dda(float x1,float y1,float x2,float y2) { float dx,dy,x=x1,y=y1,m; int i; dx=x2-x1; dy=y2-y1; if(abs(dx)>=abs(dy)) m=abs(dx); else m=abs(dy); putpixel((int)x,(int)y,15); for(i=1;i<=m;i++) { x=x+dx/m; y=y+dy/m; putpixel((int)x,(int)y,15); } } void bress(float x1,float y1,float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1); int dy=abs(y2-y1),c=0,current=0; if(dx>dy) { p=2*dy-dx; if(x1<x2) { x=x1; y=y1; end=x2; if(y1<y2) inc=1;
  • 2. 2 if(y1>y2) inc=-1; } else { x=x2; y=y2; end=x1; if(y2<y1) inc=1; if(y2<y1) inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else { y=y+inc; p=p+2*(dy-dx); } x++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } } void main() { float x1,x2,y1,y2; int ch; int gd=DETECT,gm=DETECT;
  • 3. 3 initgraph(&gd,&gm," "); printf("ENTER THE END POINTS OF LINE (x1,y1,x2,y2): "); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); printf("CHOOSE ALGORITHM: "); printf("n 1-DDA"); printf("n 2-BRESENHAM"); printf("n YOUR CHOICE IS: "); scanf("%d",&ch); if(ch==1) dda(x1,y1,x2,y2); if(ch==2) bress(x1,y1,x2,y2); getch(); closegraph(); }
  • 5. 5 Program 2 WRITE A C PROGRAM TO DRAW MIDPOINT CIRCLE ALGORITHM. # include<stdio.h> # include<graphics.h> # include<conio.h> # include<math.h> void bresenham_circle(const int h,const int k,const int r) { int x=0,y=r,p=(3-(2*r)); // cleardevice(); line(320,1,320,480); line(1,240,640,240); do { delay(15); putpixel((h+x),(k+y),25); putpixel((h+y),(k+x),15); putpixel((h+y),(k-x),25); putpixel((h+x),(k-y),15); putpixel((h-x),(k-y),25); putpixel((h-y),(k-x),15); putpixel((h-y),(k+x),25); putpixel((h-x),(k+y),15); x++; if(p<0) p+=((4*x)+6); else { y--; p+=((4*(x-y))+10); } } while(x<=y); } void main(void) { int driver=VGA,mode=VGAHI,h,k,r;
  • 6. 6 initgraph(& driver,& mode,"c:tcbgi"); printf("n ENTER THE VALUE OF [H-COORDINATE]: "); scanf("%d",&h); printf("n ENTER THE VALUE OF [K-COORDINATE]: "); scanf("%d",&k); printf("n ENTER THE VALUE OF THE RADIUS: "); scanf("%d",&r); bresenham_circle(320+h,240-k,r); do { r--; bresenham_circle(320+h,240-k,r); }while(r!=0); getche(); }
  • 8. 8 Program 3 WRITE A C PROGRAM OF WINDOW TO VIEW PORT. # include<conio.h> # include<stdio.h> # include<graphics.h> void image(); float wxmin,wymin,wxmax,wymax; float vxmin,vymin,vxmax,vymax; void main() { int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:tc"); printf("nt ENTER THE COORDINATES OF WINDOW: n"); printf("nt wxmin,wymin: "); scanf("%f%f",&wxmax,&wymax); printf("nt ENTER THE COORDINATES OF VIEW: n"); printf("nt vxmin,vymin: "); scanf("%f%f",&vxmin,&vymin); printf("nt vxmax,vymax: "); scanf("%f%f",&vxmax,&vymax); rectangle(wxmin,wymin,wxmax,wymax); rectangle(vxmin,vymin,vxmax,vymax); getch(); cleardevice(); image(); getch(); } void image() { float x1,y1,x2,y2,vx1,vx2,vy1,vy2; clrscr(); printf("nnt ENTER THE COORDINATES OF LINE: "); printf("nnt X1 Y1: "); scanf("%f%f",&x1,&y1); printf("nnt X2 Y2: "); scanf("%f%f",&x2,&y2);
  • 9. 9 rectangle(wxmin,wymin,wxmax,wymax); rectangle(vxmin,vymin,vxmax,vymax); line(x1,y1,x2,y2); vx1=((vxmax-vxmin)/(wxmax-wxmin))*(x1-wxmin)+vxmin; vy1=((vxmax-vxmin)/(wxmax-wxmin))*(y1-wxmin)+vxmin; vx2=((vxmax-vxmin)/(wxmax-wxmin))*(x2-wxmin)+vxmin; vy2=((vxmax-vxmin)/(wxmax-wxmin))*(y2-wxmin)+vxmin; line(vx1,vy1,vx2,vy2); }
  • 11. 11 Program 4 WRITE A C PROGRAM OF COHEN-SUTHERLAND ALGORITHM. #include<stdio.h> #include<graphics.h> typedef unsigned int outcode; enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 }; void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax ) float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax; { int gd,gm; outcode code0,code1,codeout; int accept = 0, done=0; code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); do{ if(!(code0 | code1)) { accept =1 ; done =1; } else if(code0 & code1) done = 1; else { float x,y; codeout = code0 ? code0 : code1; if(codeout & TOP) { x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0); y = ywmax; } else if( codeout & BOTTOM) { x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0); y = ywmin; } else
  • 12. 12 if ( codeout & RIGHT) { y = y0+(y1-y0)*(xwmax-x0)/(x1-x0); x = xwmax; } else { y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0); x = xwmin; } if( codeout == code0) { x0 = x; y0 = y; code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax); } else { x1 = x; y1 = y; code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax); } } } while( done == 0); if(accept) line(x0,y0,x1,y1); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); } /*--------------------------------------------------------------------*/ int calcode (x,y,xwmin,ywmin,xwmax,ywmax) float x,y,xwmin,ywmin,xwmax,ywmax; { int code =0; if(y> ywmax) code |=TOP; else if( y<ywmin) code |= BOTTOM; else if(x > xwmax) code |= RIGHT; else if ( x< xwmin)
  • 13. 13 code |= LEFT; return(code); } /*-------------------------------------------------*/ void main() { float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax; int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"C:TCBGI"); printf("nntEnter the co-ordinates of Line :"); printf("nntX1 Y1 : "); scanf("%f %f",&x1,&y1); printf("nntX2 Y2 : "); scanf("%f %f",&x2,&y2); printf("ntEnter the co_ordinates of window :n "); printf("ntxwmin , ywmin : "); scanf("%f %f",&xwmin,&ywmin); printf("ntxwmax , ywmax : "); scanf("%f %f",&xwmax,&ywmax); line(x1,y1,x2,y2); rectangle(xwmin,ywmin,xwmax,ywmax); getch(); cleardevice(); lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); getch(); closegraph(); }
  • 15. 15 Program 5 WRITE A C PROGRAM TO DRAW A BEZIER – CURVE. #include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<string.h> #include<conio.h> #include<math.h> int *x,*y; void bezier(int *x1,int *y1) { int xa,ya,za; float u=0; moveto(*x1,*y1); while (u<=1) { xa=(*(x1+3)*pow(u,3))+(3*(*(x1+2))*pow(u,2)*(1-u))+ (3*(*(x1+1))*u*pow(1-u,2))+(*x1*pow(1-u,3)); ya=(*(y1+3)*pow(u,3))+(3*(*(y1+2))*pow(u,2)*(1-u))+ (3*(*(y1+1))*u*pow(1-u,2))+(*y1*pow(1-u,3)); lineto(xa, ya); u += 0.01; } } void main(void) { int gd=DETECT, gm, key, xpos, ypos, i = 0; char xp[4], yp[4]; x = (int *)malloc(sizeof(int) * 4); y = (int *)malloc(sizeof(int) * 4); //registerbgidriver(EGAVGA_driver); initgraph(&gd, &gm, "c:tcbgi"); outtextxy(10, 460, "Press Esc to exit."); outtextxy(400, 460, "Present position : "); moveto(getmaxx() / 2, getmaxy() / 2); do {
  • 16. 16 xpos = getx(); ypos = gety(); itoa(xpos, xp, 10); itoa(ypos,yp,10); setviewport(550,460,639,479,1); clearviewport(); outtextxy(0,0,xp); outtextxy(40,0,yp); setviewport(0,0,639,479,1); moveto(xpos,ypos); key=getch(); if (key==0) key=getch(); switch(key) { case 72 : ypos--; //up arrow moveto(xpos,ypos); break; case 80 : ypos++; //down arrow moveto(xpos,ypos); break; case 75 : xpos--; //left arrow moveto(xpos,ypos); break; case 77 : xpos++; //right arrow moveto(xpos,ypos); break; case 87 : case 119 : ypos-=50; moveto(xpos,ypos); break; case 65 : case 97 : xpos-=50; moveto(xpos,ypos); break; case 83 : case 115 :xpos+=50; moveto(xpos,ypos); break; case 90 : case 122 :ypos+=50; moveto(xpos,ypos); break; case 13 : putpixel(xpos,ypos,15); *(x+i)=xpos;
  • 17. 17 *(y+i)=ypos; i++; break; } if (i==4) { bezier(x,y); i=0; } } while(key!=27); closegraph(); getch(); }
  • 19. 19 Program 6 WRITE A C PROGRAM TO SHEAR A CUBOID. #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> void bress(float x1,float y1, float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if ( dx > dy ) { p=2*dy-dx; if(x1<x2) { x=x1; y=y1; end=x2; if(y1<y2) inc=1; if(y1>y2) inc=-1; } else { x=x2; y=y2; end=x1; if(y2<y1) inc=1; if(y2>y1) inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else {
  • 20. 20 y=y+inc; p=p+2*(dy-dx); } x++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } else { p=2*dx-dy; if(y1<y2) { x=x1; y=y1; end=y2; if(x1<x2) inc=1; if(x1>x2) inc=-1; } else { x=x2; y=y2; end=y1; if(x2<x1) inc=1; if(x2>x1) inc=-1; } while(y<=end) { putpixel(x,y,15); if(p<0)
  • 21. 21 p=p+2*dx; else { x=x+inc; p=p+2*(dx-dy); } y++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } } void main() { float x1,x2,a,b,c,d,y1,y2; double ch; int t=30; int gd=DETECT,gm=DETECT; initgraph(&gd,&gm,"c:tcbgi"); printf("Enter the end pts of the line(x,y)"); scanf("%f%f%f%f",&x1,&y1,&x2,&y2); bress(x1,y1,x2,y1); bress(x2,y1,x2,y2); bress(x1,y2,x2,y2); bress(x1,y2,x1,y1); bress(x1+t,y1+t,x2+t,y1+t); bress(x2+t,y1+t,x2+t,y2+t); bress(x1+t,y2+t,x2+t,y2+t); bress(x1+t,y2+t,x1+t,y1+t); bress(x1,y1,x1+t,y1+t); bress(x2,y1,x2+t,y1+t);
  • 22. 22 bress(x1,y2,x1+t,y2+t); bress(x2,y2,x2+t,y2+t); /* a=x1+(10*y1); b=x2+(10*y1); c=(10*x1)+y1; d=(10*x2)+y1;*/ bress(x1+50,y1+40,x2+50,y1+40); bress(x1+t,y2+t,x1+50,y1+40); bress(x2+t,y2+t,x2+50,y1+40); bress(x1+50,y1+40,x1,y1); bress(x2+50,y1+40,x2,y1); getch(); closegraph(); }
  • 24. 24 Program 7 WRITE A C PROGRAM TO DRAW A POLYGON AND PERFORM THE FOLLOWING OPERATIONS:- ROTATION TRANSLATION SCALING. #include<stdio.h> #include<graphics.h> #include<stdlib.h> #include<conio.h> #include<math.h> int *x,*y,i,nin; float x1,y1,theta; void drawpolygon(int *x,int *y) { int gd=DETECT,gm,ch=0,x1,y1,theta; //registerbgidriver(EGAVGA_driver); initgraph(&gd,&gm,""); for (i=0;i<(nin-1);i++) line(*(x+i),*(y+i),*(x+i+1),*(y+i+1)); line(*(x+nin-1),*(y+nin-1),*x,*y); getch(); closegraph(); } void translate(float x1,float y1) { for (i=0;i<nin;i++) { *(x+i)+=x1; *(y+i)+=y1; } } void scale(float x1,float y1) { int a,b; a=*x;
  • 25. 25 b=*y; translate(-a,-b); for (i=0;i<nin;i++) { *(x+i)*=x1; *(y+i)*=y1; } translate(a,b); } void rotate(float theta) { int a,b,c,d; c=*x; d=*y; translate(-c,-d); for (i=0;i<nin;i++) { a=(*(x+i)*cos(theta))-(*(y+i)*sin(theta)); b=(*(x+i)*sin(theta))+(*(y+i)*cos(theta)); *(x+i)=a; *(y+i)=b; } translate(c,d); } void main(void) { int ch; x=(int *)malloc(sizeof(int)*10); y=(int *)malloc(sizeof(int)*10); clrscr(); printf("ENTER NUMBER OF SIDES IN POLYGON : "); scanf("%d",&nin); printf("ENTER THE COORDINATES OF THE VERTICES (x,y) :n"); for (i=0;i<nin;i++) { printf("(i+1) : "); scanf("%d%d",&(*(x+i)),&(*(y+i))); } drawpolygon(x,y); while (ch!=4) { printf("YOUR OPTIONS :n");
  • 26. 26 printf("1)TRANSLATEn"); printf("2)SCALEn"); printf("3)ROTATEn"); printf("4)EXITn"); printf("nYOUR CHOICE : "); scanf("%d",&ch); clrscr(); switch(ch) { case 1:printf("TRANSLATION IN X-DIRECTION : "); scanf("%d",&x1); printf("TRANSLATION IN Y-DIRECTION : "); scanf("%d",&y1); translate(x1,y1); drawpolygon(x,y); break; case 2:printf("SCALING IN X-DIRECTION : "); scanf("%f",&x1); printf("SCALING IN Y-DIRECTION : "); scanf("%f",&y1); scale(x1,y1); drawpolygon(x,y); break; case 3:printf("ANGLE OF ROTATION(ANTI-CLOCKWISE IS POSITIVE) : "); scanf("%f",&theta); theta*=3.1415/180; rotate(-theta); drawpolygon(x,y); break; case 4:exit(0); } } }
  • 27. 27 Output-7 After translation After scaling
  • 29. 29 Program 8 WRITE A C PROGRAM TO DRAW A RECTANGLE BY USING BRESENHAM AND DDA ALGORITHM. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void dda(float x1,float y1,float x2,float y2) { float dx,dy,x=x1,y=y1,m; int i; dx=x2-x1; dy=y2-y1; if(abs(dx)>=abs(dy)) m=abs(dx); else m=abs(dy); putpixel((int)x,(int)y,15); for(i=1;i<m;i++) { x=x+dx/m; y=y+dy/m; putpixel((int)x,(int)y,15); } } void bress(float x1,float y1,float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if(dx>dy) { p=2*dy-dx; if(x1<x2) { x=x1;y=y1;end=x2; } if(y1<y2) inc=1; if(y1>y2) inc=-1; } else { x=x2;y=y2;end=x1; if(y2<y1)
  • 30. 30 inc=1; if(y2>y1) inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else { y=y+inc;p=p+2*(dy-dx); } x++; if(current==0&&c==10) { current=1; c=-1; } if(current==1&&c==6) { current=0; c=-1; } c++; } } void main() { float x1,x2,y1,y2,x3,y3,x4,y4; int ch; int gdriver=DETECT, gmode=DETECT; initgraph(&gdriver, &gmode, "c:tcBGI"); printf("Enter end points of line (x1,y1,x2,y2)"); scanf("%f %f %f %f %f %f %f %f",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); printf("Choose algorithm(1-DDA 2-BRESENHAM)"); scanf("%d",&ch); bress(x1,y1,x2,y2); dda(x2,y2,x3,y3); bress(x4,y4,x3,y3); dda(x4,y4,x1,y1); getch(); closegraph(); }
  • 32. 32 Program 9 WRITE A C PROGRAM TO DRAW A SOLID AND FIND ITS VANISHING POINT. #include<stdio.h> #include<conio.h> #include<graphics.h> void createsolid(); void main() { int gd = DETECT,gm = DETECT; initgraph(&gd,&gm,"c:tcbgi"); clrscr(); createsolid(); getch(); closegraph(); } void createsolid() { //The coordinates of first rectangle line(250,200,370,200); line(370,200,370,320); line(370,320,250,320); line(250,200,250,320); //The coordinates of scond rectangle line(280,150,400,150); line(400,150,400,270); line(400,270,280,270); line(280,150,280,270); //The coordinates of the lines line(250,200,280,150); line(370,200,400,150); line(370,320,400,270); line(250,320,280,270); //The coordinates of the X,Y and Z axis line(320,300,600,300); line(320,50,320,300); line(320,300,170,450); //Coordinates for vanishing point putpixel(190,430,RED); }
  • 34. 34 Program 10 WRITE A C PROGRAM TO TRANSLATE A LINE BY USING DDA ALGORITHM. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void dda(float x1,float y1,float x2,float y2) { float dx,dy,x=x1,y=y1,m; int i; dx=x2-x1; dy=y2-y1; if(abs(dx)>=abs(dy)) m=abs(dx); else m=abs(dy); putpixel((int)x,(int)y,15); for(i=1;i<=m;i++) { x=x+dx/m; y=y+dy/m; putpixel((int)x,(int)y,15); } } void main() { float x11,x12,y11,y12, x21, x22, y21, y22; float nw; int gd=DETECT,gm=DETECT; initgraph(&gd,&gm,"c:tcbin"); printf("enter endpoints of line(x1,y1,x2,y2)"); scanf("%f%f%f%f",&x11,&x12,&y11,&y12); dda(x11,x12,y11,y12); printf("Please enter new position"); scanf("%f",&nw); x21=x11+nw; y21=y11+nw; x22=x12+nw; y22=y12+nw; dda(x21,x22,y21,y22); getch(); closegraph(); }
  • 36. 36 Program 11 WRITE A C PROGRAM TO ROTATE A TRIANGLE. #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> void bress(float x1,float y1, float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if ( dx > dy ) { p=2*dy-dx; if(x1<x2) { x=x1; y=y1; end=x2; if(y1<y2) inc=1; if(y1>y2) inc=-1; } else { x=x2; y=y2; end=x1; if(y2<y1) inc=1; if(y2>y1) inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else { y=y+inc; p=p+2*(dy-dx);
  • 37. 37 } x++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } else { p=2*dx-dy; if(y1<y2) { x=x1; y=y1; end=y2; if(x1<x2) inc=1; if(x1>x2) inc=-1; } else { x=x2; y=y2; end=y1; if(x2<x1) inc=1; if(x2>x1) inc=-1; } while(y<=end) { putpixel(x,y,15); if(p<0) p=p+2*dx; else
  • 38. 38 { x=x+inc; p=p+2*(dx-dy); } y++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } } void main() { float a[3][3],b[3][3],c[3][3],i,j,k; float r=(3.14*45)/180; int gd=DETECT,gm=DETECT; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); //IMAGE(triangle) bress(0,0,100,100); bress(100,100,500,200); bress(500,200,0,0); a[0][0]=0; a[0][1]=0; a[0][2]=1; a[1][0]=100; a[1][1]=100; a[1][2]=1; a[2][0]=500; a[2][1]=200; a[2][2]=1; b[0][0]=cos(r);
  • 39. 39 b[0][1]=sin(r); b[0][2]=0; b[1][0]=-sin(r); b[1][1]=cos(r); b[1][2]=0; b[2][0]=0; b[2][1]=0; b[2][2]=1; //MATRIX MULTIPLICATION for(i=0;i<3;i++) for(j=0;j<3;j++) { c[i][j]=0; for(k=0;k<3;k++) { c[i][j]+=a[i][k]*b[k][j]; } } //AFTER ROTATION line(c[0][0],c[0][1],c[1][0],c[1][1]); line(c[1][0],c[1][1],c[2][0],c[2][1]); line(c[2][0],c[2][1],c[0][0] ,c[0][1]); getch(); }
  • 41. 41 Program 12 WRITE A C PROGRAM TO DRAW BEZIER – SURFACE. #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd,gm, x1,x2,x3,x4,y1,y2,y3,y4,y11,y22,y33,y44,i; gd=DETECT,gm=DETECT; clrscr(); initgraph(&gd,&gm,"c:tcbgi"); x1=100;x2=130;x3=170;x4=200;y1=150;y2=16 ;y3=160;x4=165; line(x1,y1,x2,y2); line(x2,y2,x3,y3); line(x3,y3,x4,y4); y11=y1; y22=y2; y33=y3; y44=y4; for(i=0;i<5;i++) { y11=y11+10; y22=y22+10; y33=y33+10; y44=y44+10; line(x1,y11,x2,y22); line(x2,y22,x3,y33); line(x3,y33,x4,y44); } line(x1,y1,x1,y11); line(x2,y2,x2,y22); line(x3,y3,x3,y33); line(x4,y4,x4,y44); getch(); closegraph(); }
  • 43. 43 Program 13 WRITE A C PROGRAM TO CONVERT WINDOW COORDINATES IN TO VIEW PORT. #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> void bress(float x1,float y1, float x2,float y2) { int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2-y1),c=0,current=0; if ( dx > dy ) { p=2*dy-dx; if(x1<x2) { x=x1; y=y1; end=x2; if(y1<y2) inc=1; if(y1>y2) inc=-1; } else { x=x2; y=y2; end=x1; if(y2<y1) inc=1; if(y2>y1) inc=-1; } while(x<=end) { putpixel(x,y,15); if(p<0) p=p+2*dy; else { y=y+inc; p=p+2*(dy-dx);
  • 44. 44 } x++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } else { p=2*dx-dy; if(y1<y2) { x=x1; y=y1; end=y2; if(x1<x2) inc=1; if(x1>x2) inc=-1; } else { x=x2; y=y2; end=y1; if(x2<x1) inc=1; if(x2>x1) inc=-1; } while(y<=end) { putpixel(x,y,15); if(p<0) p=p+2*dx; else
  • 45. 45 { x=x+inc; p=p+2*(dx-dy); } y++; if(current==0 && c==10) { current=1; c=-1; } if(current==1 && c==6) { current=0; c=-1; } c++; } } } void main() { float x1,x2,x3,x4,y1,y2,y3,y4; double ch; int t; float vx,vy,vx1,vx2,vy2; int gd=DETECT,gm=DETECT; initgraph(&gd,&gm,"c:tcbgi"); outtextxy(250,190,"World coordinates"); bress(200,200,400,200); bress(200,200,200,400); bress(400,200,400,400); bress(200,400,400,400); outtextxy(23,10,"View port"); bress(20,20,100,20); bress(20,20,20,100); bress(100,20,100,100); bress(20,100,100,100); //IMAGE(triangle) bress(250,250,350,250);
  • 46. 46 bress(250,250,300,300); bress(350,250,300,300); //TRANSFORMATION vx=(((100-20)*(250-200))/(400-200))+20; vy=(((100-20)*(250-200))/(400-200))+20; vx1=(((100-20)*(350-200))/(400-200))+20; vx2=(((100-20)*(300-200))/(400-200))+20; vy2=(((100-20)*(300-200))/(400-200))+20; //TRANSFORMED IMAGE bress(vx,vy,vx1,vy); bress(vx,vy,vx2,vy2); bress(vx1,vy,vx2,vy2); getch(); }