College of Technology
College of Technology
LAB ASSIGNMENT
OF
COMPUTER GRAPHICS
AND
ANIMATIONS
Submitted by:
Shubham Bansal
ID No: 40725
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
int i,flag,d;
clrscr();
cout<<"Enter value of (x1,y1)= ";
cin>>x1>>y1;
cout<<"Enter value of (x2,y2)= ";
cin>>x2>>y2;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
int dx,dy;
dx=abs(x2-x1);
dy=abs(y2-y1);
int x,y,t,s1,s2;
x=x1;
y=y1;
if((x2-x1)>0)
s1=1;
else
s1=-1;
if((y2-y1)>0)
s2=1;
else
s2=-1;
if(dy>dx)
{
t=dx;
dx=dy;
dy=t;
flag=1;
}
else
flag=0;
d=2*dy-dx;
outtextxy(x1,y1,"(x1,y1)");
outtextxy(x2,y2,"(x2,y2)");
i=1;
a:
putpixel(x,y,3);
delay(40);
while(d>=0)
{
if (flag==1)
x=x+s1;
else
y=y+s2;
d=d-2*dx;
}
if (flag==1)
y=y+s2;
else
x=x+s1;
d=d+2*dy;
i++;
if(i<=dx)
goto a;
closegraph();
getch();
}
}
closegraph();
getch();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
getch(); }
PROGRAM 6: WRITE A PROGRAM TO CLIP A LINE USING COHEN SUTHERLAND LINE CLIPPING
ALGORITHM.
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<dos.h>
#define MAX 20
enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
enum { FALSE, TRUE };
typedef unsigned int outcode;
outcode compute_outcode(int x, int y,
int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;
if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;
if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;
return oc;
}
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{ int accept;
int done;
outcode outcode1, outcode2;
accept = FALSE;
done = FALSE;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do {
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE; }
else {
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax; }
else if (outcode_ex & BOTTOM)
{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin; }
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax; }
else {
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin; }
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax); }
else { x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);
if (accept == TRUE)
line (x1, y1, x2, y2); }
void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;
printf ("Enter the number of lines to be clipped");
scanf ("%d", &n);
printf ("Enter the x- and y-coordinates of the line-endpoints:\n");
for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);
printf ("Enter the x- and y-coordinates of the left-top and right-");
printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);
initgraph (&gd, &gm, "..//bgi");
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();
cleardevice();
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
{ cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch(); }
closegraph();
getch(); }
Dashed_line(cp[(count*2)],cp[((count*2)+1)],
cp[((count+1)*2)],cp[(((count+1)*2)+1)]);
float x;
float y;
for(float u=0.0005;u<=1;u+=0.0005)
{
x=0;
y=0;
for(int k=0;k<=3;k++)
{ x+=(cp[(k*2)]*nCr(3,k)*pow(u,k)*powl((1-u),(3-k)));
y+=(cp[((k*2)+1)]*nCr(3,k)*pow(u,k)*powl((1-u),(3-k)));
}
putpixel((int)(x+0.5),(int)(y+0.5),color);
} }
double nCr(int n,int r)
{
double nf;
double rf;
double nrf;
double ncr;
nf=factorial(n);
rf=factorial(r);
nrf=factorial((n-r));
ncr=(nf/(rf*nrf));
return ncr;
}
double factorial(int number)
{double factorial=1;
if(number==0 || number==1);
else {
for(int count=1;count<=number;count++)
factorial=factorial*count;
}
return factorial;
}
void Dashed_line(const int x_1,const int y_1,const int x_2,
const int y_2,const int line_type)
{
int count=0;
int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{
x++;
if(p<0)
p+=two_dy;
else {
y+=inc_dec;
p+=two_dy_dx; }
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 &&
(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
} }
else {
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2){
y+=inc_dec;
if(p<0)
p+=two_dx;
else{
x++;
p+=two_dx_dy;
}
if((count%2)!=0 && line_type==0)
putpixel(x,y,color);
else if((count%5)!=4 && line_type==1)
putpixel(x,y,color);
else if((count%10)!=8 && (count%10)!=9 && line_type==2)
putpixel(x,y,color);
else if((count%20)!=18 && (count%20)!=19 && line_type==3)
putpixel(x,y,color);
else if((count%12)!=7 && (count%12)!=8 &&
(count%12)!=10 && (count%12)!=11 && line_type==4)
putpixel(x,y,color);
count++;
}}
}
void show_screen( )
{
restorecrtmode( );
clrscr( );
textmode(C4350);
cprintf("n********************************************************************************");
cprintf("*-**************************-**************************-*");
cprintf("*---------------------------- ");
textbackground(1);
cprintf(" Cubic Bezier Curve ");
textbackground(8);
cprintf(" ----------------------------*");
cprintf("*-**************************-**************************-*");
cprintf("*-*****************************************************************************");
for(int count=0;count<42;count++)
cprintf("*-*
*-*");
gotoxy(1,46);
cprintf("*-*****************************************************************************");
cprintf("*------------------------------------------------------------------------------*");
cprintf("********************************************************************************");
gotoxy(1,2);
}
y = ymax;
}
else if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin; }
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0); }
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax); }
outcode CmpOutCode(float x,float y)
{ outcode code = 0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
/* initialize graphics and local variables */
PROGRAM 10: WRITE A PROGRAM FOR ROTATION OF 3D OBJECT ABOUT ARBITRARY AXIS.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define f 0.3
#define projection_angle 45
void show_screen( );
void apply_rotation_along_z_axis(const int [5][3],const int);
void multiply_matrices(const float[4],const float[4][4],float[4]);
void draw_pyramid(int [5][3]);
void get_projected_point(int&,int&,int&);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"c:\\turboc3\\Bgi");
show_screen( );
int pyramid[5][3]={
{20,120,50}, // base front left
{80,120,50}, // base front right
{80,120,-50}, // base back right
{20,120,-50}, // base back left
{50,20,0}
// top
};
setcolor(15);
draw_pyramid(pyramid);
setcolor(15);
settextstyle(0,0,1);
outtextxy(50,415,"*** Use + & - Keys to apply Rotation along z-axis.");
int angle=0;
int key_code=0;
char Key=NULL;
do
{
Key=NULL;
key_code=0;
Key=getch( );
key_code=int(Key);
if(key_code==0)
{
Key=getch( );
key_code=int(Key); }
if(key_code==27)
break;
else if(key_code==43)
angle-=5;
else if(key_code==45)
angle+=5;
setfillstyle(1,0);
bar(40,70,600,410);
apply_rotation_along_z_axis(pyramid,angle);
}
while(1);
return 0;
}
void apply_rotation_along_z_axis(const int control_points[5][3],const int theta) {
int edge_points[5][3]={0};
float angle=(theta*(M_PI/180));
for(int count=0;count<5;count++)
{
edge_points[count][0]=control_points[count][0];
edge_points[count][1]=control_points[count][1];
edge_points[count][2]=control_points[count][2];
float matrix_a[4]={edge_points[count][0],edge_points[count][1],
edge_points[count][2],1};
float matrix_b[4][4]={
{ cos(angle),sin(angle),0,0 } ,
{ -sin(angle),cos(angle),0,0 } ,
{ 0,0,1,0 } ,
{ 0,0,0,1 } };
float matrix_c[4]={0};
multiply_matrices(matrix_a,matrix_b,matrix_c);
edge_points[count][0]=(int)(matrix_c[0]+0.5);
edge_points[count][1]=(int)(matrix_c[1]+0.5);
edge_points[count][2]=(int)(matrix_c[2]+0.5);
}
setcolor(10);
draw_pyramid(edge_points);
}
void multiply_matrices(const float matrix_1[4],const float matrix_2[4][4],float matrix_3[4]) {
for(int count_1=0;count_1<4;count_1++)
{
for(int count_2=0;count_2<4;count_2++)
matrix_3[count_1]+=
(matrix_1[count_2]*matrix_2[count_2][count_1]);
} }
void draw_pyramid(int points[5][3])
{
int edge_points[5][3];
for(int i=0;i<5;i++) {
edge_points[i][0]=points[i][0];
edge_points[i][1]=points[i][1];
edge_points[i][2]=points[i][2];
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
edge_points[i][0]+=320;
edge_points[i][1]+=240;
}
Line(edge_points[0][0],edge_points[0][1],
edge_points[1][0],edge_points[1][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[2][0],edge_points[2][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[3][0],edge_points[3][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[0][0],edge_points[0][1]);
Line(edge_points[0][0],edge_points[0][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[1][0],edge_points[1][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[4][0],edge_points[4][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[4][0],edge_points[4][1]);
} void get_projected_point(int& x,int& y,int& z)
{
float fcos0=(f*cos(projection_angle*(M_PI/180)));
float fsin0=(f*sin(projection_angle*(M_PI/180)));
float Par_v[4][4]={
{1,0,0,0},
{0,1,0,0},
{fcos0,fsin0,0,0},
{0,0,0,1}
};
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Par_v,new_xy);
x=(int)(new_xy[0]+0.5);
y=(int)(new_xy[1]+0.5);
z=(int)(new_xy[2]+0.5);
}
void Line(const int x_1,const int y_1,const int x_2,const int y_2)
{ int color=getcolor( );
int x1=x_1;
int y1=y_1;
int x2=x_2;
int y2=y_2;
if(x_1>x_2)
{
x1=x_2;
y1=y_2;
x2=x_1;
y2=y_1;
}
int dx=abs(x2-x1);
int dy=abs(y2-y1);
int inc_dec=((y2>=y1)?1:-1);
if(dx>dy)
{
int two_dy=(2*dy);
int two_dy_dx=(2*(dy-dx));
int p=((2*dy)-dx);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(x<x2)
{ x++;
if(p<0)
p+=two_dy;
else {
y+=inc_dec;
p+=two_dy_dx;
}
putpixel(x,y,color);
} }
else {
int two_dx=(2*dx);
int two_dx_dy=(2*(dx-dy));
int p=((2*dx)-dy);
int x=x1;
int y=y1;
putpixel(x,y,color);
while(y!=y2)
{
y+=inc_dec;
if(p<0)
p+=two_dx;
else
{
x++;
p+=two_dx_dy;
}
putpixel(x,y,color);
} } }
void show_screen( )
{
setfillstyle(1,1);
bar(210,26,420,38);
settextstyle(0,0,1);
setcolor(15);
outtextxy(5,5,"******************************************************************************
");
outtextxy(5,17,"***************************************************************************-*");
outtextxy(5,29,"*----------------------------------------------*");
outtextxy(5,41,"***************************************************************************-*");
outtextxy(5,53,"***************************************************************************-*");
setcolor(11);
outtextxy(218,29,"3D Rotation along Z-axis");
setcolor(15);
for(int count=0;count<=30;count++)
outtextxy(5,(65+(count*12)),"*-*
*-*");
outtextxy(5,438,"***************************************************************************-*");
outtextxy(5,450,"*-------------------------------------------------*");
outtextxy(5,462,"****************************************************************************
**");
setcolor(12);
outtextxy(229,450,"Press any Key to exit.");
}