Anda di halaman 1dari 59

SUBMITTED BY :

INDEX
S.NO PROGRAMS REMARKS SIGN
1 Wap for window to viewport transformation

2 Menu generation program for 2-d transformation

3 Menu generation program for 3-d transformation

4 Wap for parallel projection

5 Wap for perspective projection

6 Wap to draw bezier curve

7 Wap to draw a line using dda algorithm.

8 Wap to draw a line using bresenham’s algorithm.

9 Wap to draw a circle using bresenham’s algorithm.

10 Wap to draw a circle using mid-pt algorithm.

/* QUES : PROGRAM FOR WINDOW TO VIEWPORT


TRANSFORMATION. */
# include<stdio.h>
# include<iostream.h>
# include<conio.h>
# include<graphics.h>
# include<stdlib.h>

void main()
{
float x,y,sx,sy,p,q,j,i,a,b,k;
int n;
int l;
float temp1,temp2;
float vmat[3][3];
float points[10][3][1];
float fscale[10][3][1];
float vmaxx,vmaxy,wmaxx,wmaxy,vminx,vminy,wminx,wminy;
int gdriver=DETECT,gmode,errorcode;
wmaxx=640;
wmaxy=480;
wminx=0;
wminy=0;
closegraph();
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode= graphresult();
clrscr();
if(errorcode != grOk)
{
printf("Graphics error: %s\n",grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
exit(1);
}
printf("enter the viewport max cordinates for x and y");
scanf("%f%f",&vmaxx,&vmaxy);
printf("\n enter the viewport min cordinates for x and y");
scanf("%f%f",&vminx,&vminy);

printf("enter the number of points not exceeding 10");


scanf("%d",&n);
printf("enter the cordinate points for the objects");
for(l=0;l<n;l++)
{
scanf("%f%f",&points[l][0][0],&points[l][1][0]);
points[l][2][0]=1;
}
sx=(vmaxx-vminx)/(wmaxx-wminx);
sy=(vmaxy-vminy)/(wmaxy-wminy);
vmat[0][0]=sx;
vmat[0][1]=0;
vmat[0][2]=-sx*wminx+vminx;
vmat[1][0]=0;
vmat[1][1]=sy;
vmat[1][2]=-sy*wminy+vminy;
vmat[2][0]=0;
vmat[2][1]=0;
vmat[2][2]=1;
printf("before scalling");
setcolor(3);
for(l=0;l<n-1;l++)
line(points[l][0][0]+320,240-points[l][1][0],points[l+1][0][0]+320,240-
points[l+1][1][0]);
line(320+points[l][0][0],240-points[l][1][0],320+points[0][0][0],240-points[0]
[1][0]);
getch();
setcolor(5);
line(vminx+320,240-vminy,vmaxx+320,240-vminy);
line(vmaxx+320,240-vminy,vmaxx+320,240-vmaxy);
line(vmaxx+320,240-vmaxy,vminx+320,240-vmaxy);
line(vminx+320,240-vminy,vminx+320,240-vmaxy);
for(l=0;l<n;l++)
{
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
fscale[l][i][j]=0;
for(k=0;k<3;k++)
{
fscale[l][i][j]+=vmat[i][k]*points[l][k][j];
}
}
}
}
setcolor(6);
temp1=(vmaxx-vminx)/2+320;
temp2=240-(vmaxy-vminy)/2;
printf("after win view");
for(l=0;l<n-1;l++)
{
line(fscale[l][0][0]+temp1,temp2-fscale[l][1][0],fscale[l+1][0]
[0]+temp1,temp2-fscale[l+1][1][0]);
}
line(temp1+fscale[l][0][0],temp2-fscale[l][1][0],temp1+fscale[0][0][0],temp2-
fscale[0][1][0]);
getch();
closegraph();
}
// 2-D TRANSFORMATIONS
# include <iostream.h>
# include <conio.h>
# include <graphics.h>
# include <process.h>
# include <math.h>

struct point
{
int x;
int y;
};

class transform
{
private:
int n;
point p[10];
public:
void input();
void joinpts(int);
void translate();
void mat_multiply(float mata[10][3], float matb[3][3], float matc[10]
[3]);
void rotate();
void scale();
};

void transform :: input()


{
cout << "Enter the no of points to be transformed";
cin >> n;

cout << "Enter the coordinates of each point\n";


for (int i = 0 ; i < n ; i++)
{
cout << "\nEnter the coordinates of vertex " << i + 1;
cout << "\nEnter the x coordinate";
cin >> p[i].x;
cout << "Enter the y coordinate";
cin >> p[i].y;
}
}

void transform :: joinpts(int color)


{
int maxx = getmaxx() / 2;
int maxy = getmaxy() / 2;

setcolor(color);
for (int i = 0 ; i < n - 1 ; i++)
line (p[i].x + maxx , p[i].y + maxy , p[i+1].x + maxx , p[i+1].y +
maxy);
line (p[n - 1].x + maxx , p[n - 1].y + maxy, p[0].x + maxx, p[0].y + maxy);
}

void transform :: translate()


{
int tx , ty;
float mata[10][3] , matc[10][3];

cout << "Enter the value by which u want to translate x coordinate";


cin >> tx;
cout << "Enter the value by which u want to translate y coordinate";
cin >> ty;
for (int i = 0 ; i < n ; i++)
{
mata[i][0] = p[i].x;
mata[i][1] = p[i].y;
mata[i][2] = 1;
}
float matb[3][3] = { { 1 , 0 , 0 } ,
{0,1,0},
{ tx , ty , 1} };

mat_multiply (mata , matb , matc);

for (i = 0 ; i < n ; i++)


{
p[i].x = matc[i][0];
p[i].y = matc[i][1];
}
}

void transform :: scale()


{
float sx , sy;
float mata[10][3] , matc[10][3];

cout << "Enter the value by which u want to modify x coordinate";


cin >> sx;
cout << "Enter the value by which u want to modify y coordinate";
cin >> sy;
for (int i = 0 ; i < n ; i++)
{
mata[i][0] = p[i].x;
mata[i][1] = p[i].y;
mata[i][2] = 1;
}
float matb[3][3] = { { sx , 0 , 1} ,
{ 0 , sy , 1} ,
{ 0 , 0 , 1} };

mat_multiply (mata , matb , matc);

for (i = 0 ; i < n ; i++)


{
p[i].x = matc[i][0];
p[i].y = matc[i][1];
}
}

void transform :: rotate()


{
float mata[10][3] , matc[10][3];
float theta;

cout << "Enter the angle by which u want to rotate (anticlkwise)";


cin >> theta;

for (int i = 0 ; i < n ; i++)


{
mata[i][0] = p[i].x;
mata[i][1] = p[i].y;
mata[i][2] = 1;
}

theta *= M_PI / 180;


float ct = cos(theta);
float st = sin(theta);

float matb[3][3] = { { ct , st , 0 } ,
{ -1 * st , ct , 0 } ,
{ 0 , 0 , 1} };

mat_multiply (mata , matb , matc);

for (i = 0 ; i < n ; i++)


{
p[i].x = matc[i][0];
p[i].y = matc[i][1];
}
}

void transform :: mat_multiply(float mata[10][3] , float matb[3][3] , float matc[10]


[3])
{
for (int i = 0 ; i < n ; i++)
{
for (int j = 0 ; j < 3 ; j++)
{
matc[i][j] = 0;
for (int k = 0 ; k < 3 ; k++)
matc[i][j] += mata[i][k] * matb[k][j];
}
}
}

void main()
{
clrscr();

transform ob;
int ch , ch1;
int gdriver = DETECT, gmode, errorcode;

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

errorcode = graphresult();

if (errorcode != grOk)
{
cout << "Graphics error:\n" << grapherrormsg(errorcode);
cout << "Press any key to halt:";
getch();
exit(1);
}
ob.input();
cout << "Want to perform\n";
cout << "1. Translation\n";
cout << "2. Rotation\n";
cout << "3. Scaling\n";
cout << "Enter your choice";
cin >> ch;
cleardevice(); // clear the screen by this since in graphics mode

// DRAWING AXIS
line (getmaxx() / 2 , 0 , getmaxx() / 2 , 479);
line (0 , getmaxy() / 2 , 639 , getmaxy() / 2 );

ob.joinpts(MAGENTA);

switch (ch)
{
case 1: ob.translate();
ob.joinpts(WHITE);
break;
case 2: ob.rotate();
ob.joinpts(WHITE);
break;
case 3: ob.scale();
ob.joinpts(WHITE);
break;
default: cout << "Wrong choice entered";
exit(0);
}

getch();
closegraph();
}

/* QUES : MENU GENERATION FOR TRANSFORMATION


PROGRAMS. */
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void trans();
void rotate();
void scaling();
void main()
{
int choice,i,j;
do
{
do
{
clrscr();
gotoxy(6,6);
printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\t\t\t\tMENU");
printf("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\n\t\t\t 1.TRANSLATION");
printf("\n\n\t\t\t 2.ROTATION");
printf("\n\n\t\t\t 3.SCALING");
printf("\n\n\t\t\t 4.EXIT");
printf("\n\n\t\t\t ENTER UR CHOICE :");
fflush(stdin);
gotoxy(44,25);
scanf("%d",&choice);
}while(choice<1||choice>4);
switch(choice)
{
case 1: trans();
break;
case 2: rotate();
break;
case 3: scaling();
break;
}
}while(choice!=7);
}
void trans()
{
clrscr();
float x,y,tx,ty,p,q,j,i,a,b,k;
int n,l;
float tran[3][3];
float points[10][3][1];
float final[10][3][1];
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER TRANLATION FACTOR FOR x & y:");
scanf("%d%d",&tx,&ty);
tran[0][0]=1;
tran[0][1]=0;
tran[0][2]=tx;
tran[1][0]=0;
tran[1][1]=1;
tran[1][2]=ty;
tran[2][0]=0;
tran[2][1]=0;
tran[2][2]=1;
printf("ENTER THE NO OF POINTS NOT EXCEEDING 10 : ");
scanf("%d",&n);
printf("ENTER THE COORDINATE POINTS FOR THE OBJECT : ");
for(i=0;i<n;i++)
{
scanf("%f%f",&points[i][0][0],&points[i][1][0]);
points[i][2][0]=1;
}
setcolor(3);
printf("BEFORE TRANSLATION");
for(l=0;l<n-1;l++)
{
line(points[1][0][0]+320,240-points[1][1][0],320+points[1+1][0]
[0],240-points[1+1][1][0]);
}
line(points[0][0][0]+320,240-points[0][1][0],points[1][0][0]+320,240-
points[1][1][0]);
getche();
for(l=0;l<n;l++)
{
for(i=0;i<3;i++)

{
for(j=0;j<1;j++)
{
final[l][i][j]=0;
for(k=0;k<3;k++)
{
final[l][i][j]+=tran[i][k]*points[l][k][j];
}
}
}
}
getche();
printf("AFTER TRANSLATION");
for(l=0;l<n-1;l++)
{
line(final[1][0][0]+320,240-final[1][1][0],320+final[1+1][0][0],240-
final[1+1][1][0]);
printf("test");
}
line(final[0][0][0]+320,240-final[0][1][0],320+final[1][0][0],240-final[1][1][0]);
getche();
closegraph();
}
void rotate()
{
clrscr();
clrscr();
float x,y,p,tx,ty,q,j,i,a,b,k;
int n,l;
double cangle,sangle,angle;
float rotate[3][3];
float points[10][3][1];
float final[10][3][1];
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER THE ANGLE FOR ROTATION:");
scanf("%lf",&angle);
setcolor(5);
cangle=cos(22*(angle)/7*180);
sangle=sin(22*(angle)/7*180);
printf("ENTER THE NO OF POINTS NOT EXCEEDING 10");
scanf("%d",&n);
printf("ENTER THE COORDINATE POINTS FOR THE OBJECT");
for(i=0;i<n;i++)
{
scanf("%f%f",&points[i][0][0],&points[i][1][0]);
if(i==0)
{
tx=points[i][0][0];
ty=points[i][1][0];
}
points[i][2][0]=1;
}
rotate[0][0]=(float)cangle;
rotate[0][1]=(float)-sangle;
rotate[0][2]=tx*(1-cangle)+ty*sangle;
rotate[1][0]=(float)sangle;
rotate[1][1]=(float)cangle;
rotate[1][2]=ty*(1-cangle)-tx*sangle;
rotate[2][0]=0;
rotate[2][1]=0;
rotate[2][2]=1;
printf("BEFORE ROTATION");
for(l=0;l<n-1;l++)
{
line(points[1][0][0]+320,240-points[1][1][0],320+points[1+1][0]
[0],240-points[1+1][1][0]);
}
line(320+points[1][0][0]+320,240-points[1][1][0],points[0][0][0]+320,240-
points[0][1][0]);
for(l=0;l<n;l++)
{
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
final[l][i][j]=0;
for(k=0;k<3;k++)
{
final[l][i][j]+=rotate[i][k]*points[l][k][j];
}
}
}
}
setcolor(2);
getche();
printf("\n\nAFTER ROTATION");
for(l=0;l<n-1;l++)
{
line(final[1][0][0]+320,240-final[1][1][0],320+final[1+1][0][0],240-
final[1+1][1][0]);
}
line(final[1][0][0]+320,240-final[1][1][0],320+final[0][0][0],240-final[0][1][0]);
getche();
closegraph();
}
void mul(float a[3][3],float b[3],float e[3])
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<2;j++)
{
e[i]+=a[i][j]*b[j];
}
}
}
void scaling()
{
int gd=DETECT;
int sx,sy,x1,y1,x2,y2,x3,y3,gm;
float a[3][3],b[3],c[3],d[3],e[3];
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(8);
setbkcolor(BLACK);
printf("ENTER THE AMOUNT OF SCALING: ");
scanf("%d%d",&sx,&sy);
a[0][0]=sx;
a[0][1]=0;
a[0][2]=0;
a[1][0]=0;
a[1][1]=sy;
a[1][2]=0;
a[2][0]=0;
a[2][1]=0;
a[2][2]=1;
line(150,150,250,200);
line(250,200,150,200);
line(150,200,150,150);
b[0]=150;
b[1]=150;
b[2]=1;
c[0]=250;
c[1]=200;
c[2]=1;
d[0]=150;
d[1]=200;
d[2]=1;
e[0]=e[1]=e[2]=0;
mul(a,b,e);
x1=e[0];
y1=e[1];
e[0]=e[1]=e[2]=0;
mul(a,c,e);
x2=e[0];
y2=e[1];
e[0]=e[1]=e[2]=0;
mul(a,d,e);
x3=e[0];
y3=e[1];
line(x1,y1,x2,y2);
line(x3,y3,x1,y1);
line(x2,y2,x3,y3);
setcolor(3);
getch();
closegraph();
}

~~~~~~~~~~~~~~~~~~~~~~~~~
MENU
~~~~~~~~~~~~~~~~~~~~~~~~~

1.TRANSLATION

2.ROTATION

3.SCALING

4.EXIT

ENTER UR CHOICE :
/* QUES : PROGRAM FOR MIDPOINT ELLIPSE ALGORITHM. */

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

void main()
{
float x,y,fx,fy,p,q,j,i,a,b;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");

line(320,0,320,480);
line(0,240,640,240);
printf("enter the radius along x-axis: ");
scanf("%f",&a);
printf("enter the radius along y-axis: ");
scanf("%f",&b);
p=b*b+(a*a)/4-a*a*b;
x=0;
y=b;
fx=2*b*b*x;
fy=2*a*a*y;
while(fx<fy)
{
putpixel(320+x,240-y,2);
putpixel(x+320,240-(-y),4);
putpixel(320+(-x),240-y,6);
putpixel(320+(-x),240-(-y),8);
x++;
fx=fx+2*b*b;
if(p<0)
{
p=p+fx+(b*b);
}
else
{
y--;
fy=fy-2*a*a;
p=p+fx+b*b-fy;
}
}
putpixel(320+x,240-y,4);
putpixel(x+320,240-(-y),6);
putpixel(320+(-x),240-y,8);
putpixel(320+(-x),240-(-y),2);

p=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>0)
{
y--;
fy=fy-2*a*a;
if(p>=0)
p=p-fy+a*a;
else
{
x++;
fx=fx+b*b*2;
p=p+fx-fy+a*a;
}
putpixel(320+x,240-y,2);
putpixel(x+320,240-(-y),4);
putpixel(320+(-x),240-y,6);
putpixel(320+(-x),240-(-y),8);
}
getch();
closegraph();
}
/*QUES : WAP TO DRAW BEZIER CURVE.*/

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.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,"..\\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(" Bezier Curve ");
textbackground(8);
cprintf(" ----------------------------*");
cprintf("*-**************************- -**************************-*");
cprintf("*-****************************************************************************-
*");
for(int count=0;count<42;count++)
cprintf("*-* *-*");
gotoxy(1,46);
cprintf("*-****************************************************************************-
*");
cprintf("*------------------------------------------------------------------------------*");

cprintf("********************************************************************************");
gotoxy(1,2);
}
/* QUES : A C++ PROGRAM TO SHOW THE PROJECTION OF 3D
OBJECTS USING PARALLEL PROJECTION */

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
# define f 0.3
# define angle 45

void draw_cube(int [8][3]);


void draw_pyramid(int [5][3]);
void get_projected_point(int&,int&,int&);
void multiply_matrices(const float[4],const float[4][4],float[4]);
void Line(const int,const int,const int,const int);

int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..\\Bgi");
int cube[8][3]={
{370,200,50}, // front left top
{470,200,50}, // front right top
{470,300,50}, // front right bottom
{370,300,50}, // front left bottom
{370,200,-50}, // back left top
{470,200,-50}, // back right top
{470,300,-50}, // back right bottom
{370,300,-50} // back left bottom
};
setcolor(15);
draw_cube(cube);
settextstyle(0,0,1);
outtextxy(400,320,"Cube");
int pyramid[5][3]={
{120,300,50}, // base front left
{220,300,50}, // base front right
{220,300,-50}, // base back right
{120,300,-50}, // base back left
{170,150,0} // top
};
setcolor(15);
draw_pyramid(pyramid);
settextstyle(0,0,1);
outtextxy(135,320,"Pyramid");
getch( );
closegraph( );
return 0;
}

void draw_cube(int edge_points[8][3])


{
for(int i=0;i<8;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
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[4][0],edge_points[4][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[5][0],edge_points[5][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[6][0],edge_points[6][1],
edge_points[7][0],edge_points[7][1]);
Line(edge_points[7][0],edge_points[7][1],
edge_points[4][0],edge_points[4][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[5][0],edge_points[5][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[7][0],edge_points[7][1]);
}

void draw_pyramid(int edge_points[5][3])


{
for(int i=0;i<5;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
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(angle*(M_PI/180)));
float fsin0=(f*sin(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 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 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);
}
}
}
/* QUES : A PROGRAM TO SHOW THE PROJECTION OF 3D
OBJECTS USING PERSPECTIVE PROJECTION.*/

# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
# define n1 -1.0
# define n2 1.0
# define n3 1.0
# define x0 1.0
# define y0 1.0
# define z0 1.0
# define a 0.2
# define b 0.2
# define c 0.2
void show_screen( );
void draw_cube(int [8][3]);
void draw_pyramid(int [5][3]);
void get_projected_point(int&,int&,int&);
void multiply_matrices(const float[4],const float[4][4],float[4]);
void Line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
initgraph(&driver,&mode,"..\\Bgi");
show_screen( );
int cube[8][3]={
{370,200,50}, // front left top
{470,200,50}, // front right top
{470,300,50}, // front right bottom
{370,300,50}, // front left bottom
{370,200,-50}, // back left top
{470,200,-50}, // back right top
{470,300,-50}, // back right bottom
{370,300,-50} // back left bottom
};
setcolor(15);
draw_cube(cube);
settextstyle(0,0,1);
outtextxy(400,320,"Cube");
int pyramid[5][3]={
{120,300,50}, // base front left
{220,300,50}, // base front right
{220,300,-50}, // base back right
{120,300,-50}, // base back left
{170,150,0} // top
};
setcolor(15);
draw_pyramid(pyramid);
settextstyle(0,0,1);
outtextxy(135,320,"Pyramid");
getch( );
closegraph( );
return 0;
}
void draw_cube(int edge_points[8][3])
{
for(int i=0;i<8;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);
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[4][0],edge_points[4][1],
edge_points[5][0],edge_points[5][1]);
Line(edge_points[5][0],edge_points[5][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[6][0],edge_points[6][1],
edge_points[7][0],edge_points[7][1]);
Line(edge_points[7][0],edge_points[7][1],
edge_points[4][0],edge_points[4][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[5][0],edge_points[5][1]);
Line(edge_points[2][0],edge_points[2][1],
edge_points[6][0],edge_points[6][1]);
Line(edge_points[3][0],edge_points[3][1],
edge_points[7][0],edge_points[7][1]);
}
void draw_pyramid(int edge_points[5][3])
{
for(int i=0;i<5;i++)
get_projected_point(edge_points[i][0],
edge_points[i][1],edge_points[i][2]);

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 d0=((n1*x0)+(n2*y0)+(n3*z0));
float d1=((n1*a)+(n2*b)+(n3*c));
float d=(d0-d1);
float Per_NRC[4][4]={
{(d-(a*n1)),(b*n1),(c*n1),n1},
{(a*n2),(d+n2*b),(c*n2),n2},
{(a*n3),(b*n3),(d+(c*n3)),n3},
{(-a*d0),(-b*d0),(-c*d0),-d1}
};
float xy[4]={x,y,z,1};
float new_xy[4]={0};
multiply_matrices(xy,Per_NRC,new_xy);
x=(int)(new_xy[0]+0.5);
y=(int)(new_xy[1]+0.5);
z=(int)(new_xy[2]+0.5);
}
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 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(182,26,438,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(189,29," Perspective Projection");
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(227,450," Press any key to exit ");
}
/* QUES: WRITE A PROGRAM TO DRAW A LINE USING DDA
ALGORITHM. */
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int x1,y1,x2,y2,dx,dy,x,y;
float steps,xinc,yinc;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER THE VALUE OF Ist CORDINATE : ");
scanf("%d%d",&x1,&y1);
printf("ENTER THE VALUE OF IInd CORDINATE : ");
scanf("%d%d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(dx>dy)
{
steps=dx;
}
else
{
steps=dy;
}
xinc=dx/steps;
yinc=dy/steps;
x=x1+0.5;
y=y1;
putpixel(x,y,2);
for(int k=1;k<=steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(x,y,2);
delay(100);
}
getche();
}
/* QUES : WRITE A PROGRAM TO DRAW A LINE USING
BRESHEMANS ALGORITHM. */

#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int x1,y1,x2,y2,dx,dy,x,y,inc1,inc2,d1;
float xend;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER THE VALUE OF Ist CORDINATE : ");
scanf("%d%d",&x1,&y1);
printf("ENTER THE VALUE OF IInd CORDINATE : ");
scanf("%d%d",&x2,&y2);
printf("%d%d",x1,y1);
printf("%d%d",x2,y2);
dx=x2-x1;
dy=y2-y1;
inc1=2*dy;
inc2=(2*dy-2*dx);
d1=2*dy-dx;
if(dx<0)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,4);
for(int k=1;k<=xend;k++)
{
if(d1<0)
{
d1=d1=inc1;
x=x+1;
y=y;
putpixel(x,y,4);
delay(100);
}
else
{
d1=d1+inc2;
x=x+1;
y+y+1;
putpixel(x,y,4);
delay(100);
}
}
getche();
}
/* QUES : WRITE A PROGRAM TO DRAW A CIRCLE USING
BRESHENMANS ALGORITHM. */

#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int x1,y1,r,d1;
int x,y;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER THE VALUE OF COORDINATE : ");
scanf("%d%d",&x1,&y1);
printf("ENTER THE RADIUS :");
scanf("%d",&r);
x=0;
y=r;
d1=3-2*r;
while(x<=y)
{
putpixel(x1+x,y1+y,4);
putpixel(x1-x,y1+y,4);
putpixel(x1+x,y1-y,4);
putpixel(x1-x,y1-y,4);
putpixel(x1+y,y1+x,4);
putpixel(x1+y,y1-x,4);
putpixel(x1-y,y1-x,4);
putpixel(x1-y,y1+x,4);
if(d1<0)
{
d1=d1+4*x+6;
y=y;
}
else
{
d1=d1+4*x-4*y+10;
y=y-1;
}
x=x+1;
}
getche();
}
/* QUES : WRITE A PROGRAM TO DRAW A CIRCLE USING MID
POINT ALGORITHM. */

#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int x1,y1,r,di;
int x=0,y=r;
int d1=3-2*r;
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("%s",grapherrormsg(errorcode));
}
printf("ENTER THE VALUE OF COORDINATE : ");
scanf("%d%d",&x1,&y1);
printf("%d%d",x1,y1);
printf("ENTER THE RADIUS :");
scanf("%d",&r);
printf("%d",r);
while(x<=y)
{
putpixel(x1+x,y1+y,2);
putpixel(x1-x,y1+y,2);
putpixel(x1+x,y1-y,2);
putpixel(x1-x,y1-y,2);
putpixel(x1+y,y1+x,2);
putpixel(x1+y,y1-x,2);
putpixel(x1-y,y1-x,2);
putpixel(x1-y,y1+x,2);
}
if(d1<0)
{
di=d1+4*x+6;
}
else
{
di=d1+4*x-4*y+10;
y=y-1;
}
x=x+1;
getche();
}
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                
                                                                                

Anda mungkin juga menyukai