0% found this document useful (0 votes)
86 views18 pages

College of Technology

This document contains 6 computer graphics programs written by Shubham Bansal for their lab assignment. The programs cover drawing lines using DDA and Bresenham's algorithms, drawing circles using Bresenham's and midpoint circle algorithms, clipping a line using Cohen-Sutherland algorithm, and drawing a Bezier curve. Each program contains the source code and description of the graphics concept implemented.

Uploaded by

Shubham Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views18 pages

College of Technology

This document contains 6 computer graphics programs written by Shubham Bansal for their lab assignment. The programs cover drawing lines using DDA and Bresenham's algorithms, drawing circles using Bresenham's and midpoint circle algorithms, clipping a line using Cohen-Sutherland algorithm, and drawing a Bezier curve. Each program contains the source code and description of the graphics concept implemented.

Uploaded by

Shubham Bansal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

COLLEGE OF TECHNOLOGY

DEPTT. OF INFORMATION TECHNOLOGY

LAB ASSIGNMENT
OF
COMPUTER GRAPHICS
AND
ANIMATIONS

Submitted by:

Shubham Bansal
ID No: 40725

PROGRAM 1: WRITE A PROGRAM TO DRAW A LINE USING DDA ALGORITHM.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
void draw(int x1,int y1,int x2,int y2);
void main()
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
printf("\n The Line is shown below: \n");
draw(x1,y1,x2,y2);
closegraph();
getch();
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
}
getch();
}

PROGRAM 2: WRITE A PROGRAM TO DRAW A LINE USING BRESENHAMS ALGORITHM.


#include<graphics.h>
#include<iostream.h>
#include<dos.h>
#include<math.h>
#include<conio.h>

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();
}

PROGRAM 3: WRITE A PROGRAM TO DRAW A CIRCLE USING BRESENHAMS ALGORITHM.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
void cir(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
cir(x,y,r);
closegraph();
getch();
}
void cir(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
void cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,1);
putpixel(xctr -x,yctr +y,1);
putpixel(xctr +x,yctr -y,1);
putpixel(xctr -x,yctr -y,1);
putpixel(xctr +y,yctr +x,1);
putpixel(xctr -y,yctr +x,1);
putpixel(xctr +y,yctr -x,1);
putpixel(xctr -y,yctr -x,1);
getch();
}

PROGRAM 4: WRITE A PROGRAM TO DRAW A CIRCLE USING MIDPOINT CIRCLE ALGORITHM.


#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void circleMidpoint(int, int, int);
void drawCircle(int, int, int, int);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleMidpoint(xc, yc, r);
closegraph();
getch();
}
void circleMidpoint(int xc, int yc, int r)
{
int x = 0, y = r;
int p = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;www.eazynotes.com Gursharan Singh Tatla Page No. 2
if (p < 0)
p = p + 2 * x + 1;
else
{
y--;
p = p + 2 * (x - y) + 1;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
getch();
}

PROGRAM 5: WRITE A PROGRAM TO DRAW AN ELLIPSE USING MIDPOINT ELLIPSE ALGORITHM.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
printf("enter the center points:");
scanf("%d%d",&xc,&yc);
printf("enter length of minor and major axis:");
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;

}
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(); }

PROGRAM 7: WRITE A PROGRAM TO DRAW A CUBIC BEZIER CURVE.


#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void show_screen( );
void Bezier_curve(const int [8]);
double nCr(int,int);
double factorial(int);
void Dashed_line(const int,const int,const int,const int,const int=0);
int main( ) {
int driver=VGA;
int mode=VGAHI;
int control_points[8]={0};
do {
show_screen( );
for(int count=0;count<=3;count++)
{ gotoxy(8,10);
cout<<"Coordinates of Point-"<<count<<" (x"<<count<<",y"<<count<<") :";
gotoxy(8,11);
cout<<"";
gotoxy(12,13);
cout<<"Enter the value of x"<<count<<" = ";
cin>>control_points[(count*2)];
gotoxy(12,15);
cout<<"Enter the value of y"<<count<<" = ";
cin>>control_points[((count*2)+1)];
gotoxy(8,10);
cout<<"
";
gotoxy(12,13);
cout<<"
";
gotoxy(12,15);
cout<<"
";
}
initgraph(&driver,&mode,"c:\\turboc3\\bgi");
setcolor(15);
Bezier_curve(control_points);
setcolor(15);
outtextxy(110,460,"Press <Enter> to continue or any other key to exit.");
int key=int(getch( ));
if(key!=13)
break; }
while(1);
return 0;
}
void Bezier_curve(const int cp[8])
{ int color=getcolor( );
setcolor(7);
for(int count=0;count<3;count++)

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);
}

PROGRAM 8: WRITE A PROGRAM FOR POLYGON CLIPPING.


#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum { TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do{
if(!(outcode0|outcode1))
{ accept = TRUE;
done = TRUE;
}
else if(outcode0 & outcode1)
done = TRUE;
else {
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);

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 */

initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");


outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

PROGRAM 9: WRITE A PROGRAM FOR POLYGON FILLING AS RASTER GRAPHIC DISPLAY.


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
int p=1,x,i,j,d;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
initgraph(&graphdriver,&graphmode,"C:\\turboc3\\bgi");
drawpoly(6,a);
for(i=100;i<200;i++)
{
p=1;
for(j=100;j<=200;j++)
{
x=getpixel(j,i);
for(d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1])
break;
else
{
if(x>0&&d==10)
p++;
if(p%2==0)
putpixel(j,i,4);
}
}
} }
closegraph();
getch();
}

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.");
}

You might also like