Sei sulla pagina 1di 20

After

Mid-semester
Examinations

1
Table of Contents

Program 1: Three-Dimensional Transformations-------------------------------------------------------------------------3


Program 2: Point Clipping Algorithm----------------------------------------------------------------------------------------8
Program 3: Cohen Sutherland Line Clipping Algorithm-----------------------------------------------------------------9
Program 4: Liang Barsky Line Clipping Algorithm-----------------------------------------------------------------------11
Program 5: Sutherland Hodgeman Polygon Clipping Algorithm-----------------------------------------------------13
Program 6: Perspective Projections---------------------------------------------------------------------------------------- 17
Program 7: Parallel Projections--------------------------------------------------------------------------------------------- 19

2
Program 1: Three-Dimensional Transformations
/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: 3D-Transformations
*********************************************************************/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>

int xp[2],yp[2],z;
void display();
void translate();
void scaling();
void rotation();
void matrixmul(int [4][4]);

void main()
{
int gd=DETECT,gm;
int ch,i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
for(i=0;i<2;i++)
{
printf("\nEnter X-coordinate of vertex %d : ",i+1);
scanf("%d",&xp[i]);
printf("\nEnter Y-coordinate of vertex %d : ",i+1);
scanf("%d",&yp[i]);
}
printf("\nEnter The Z-axis For 3d Figure : ");
scanf("%d",&z);
clrscr();
cleardevice();
display(xp,yp);
getche();
do
{
printf("----- MENU -----");
printf("\n1.TRANSLATION.");
printf("\t2.SCALING.");
printf("\n3.ROTATION.");
printf("\t4.EXIT.");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);

3
clrscr();
cleardevice();
display(xp,yp);
switch(ch)
{
case 1 : translate();
break;

case 2 : scaling();
break;

case 3 : rotation();
break;

case 4 : exit(0);

default:
outtextxy(1,66,"-PLEASE SELECT THE RIGHT OPTION-");
}
}
while(ch!=4);
getch();
closegraph();
}

void translate()
{
int p[4][4];
int tx,ty,tz,i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Translating Factor tx : ");
scanf("%d",&tx);
printf("\nEnter The Translating Factor ty : ");
scanf("%d",&ty);
printf("\nEnter The Translating Factor tz : ");
scanf("%d",&tz);
clrscr();
cleardevice();
display();
p[0][3]=tx;
p[1][3]=ty;
p[2][3]=tz;
matrixmul(p);
}

void scaling()
{
int p[4][4];
int sx,sy,sz,i,j;

4
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Scaling Factor sx : ");
scanf("%d",&sx);
printf("\nEnter The Scaling Factor sy : ");
scanf("%d",&sy);
printf("\nEnter The Scaling Factor sz : ");
scanf("%d",&sz);
if(sx==0)
sx=1;
if(sy==0)
sy=1;
if(sz==0)
sz=1;
clrscr();
cleardevice();
p[0][0]=sx;
p[1][1]=sy;
p[2][2]=sz;
p[3][3]=1;
matrixmul(p);
}

void rotation()
{
float res[4][1],p[4][4],t[4][1];
int ang,i,j,k,l,rch;
float rad;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
p[i][j]=(i==j);
printf("\nEnter The Rotating Angle : ");
scanf("%d",?);
rad=ang*0.0174;
printf("\nChoose the axis of roration ");
printf("\n1.X-axis");
printf("\n2.Y-axis");
printf("\n3.Z-axis");
printf("\nEnter Your Choice : ");
scanf("%d",&rch);
switch(rch)
{
case 1 : p[1][1]=cos(rad);
p[1][2]=(-1)*sin(rad);
p[2][1]=sin(rad);
p[2][2]=cos(rad);
break;

case 2 :
p[0][0]=cos(rad);

5
p[2][0]=(-1)*sin(rad);
p[0][2]=sin(rad);
p[2][2]=cos(rad);
break;

case 3 :
p[0][0]=cos(rad);
p[0][1]=(-1)*sin(rad);
p[1][0]=sin(rad);
p[1][1]=cos(rad);
break;

default :
printf("\nInvalid Choice !");
}
clrscr();
cleardevice();
for(i=0;i<2;i++)
{
t[0][0]=xp[i];
t[1][0]=yp[i];
t[2][0]=z;
t[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(p[j][l]*t[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
z=res[2][0];
}
display(xp,yp);
}

void display(int xp[2],int yp[2])


{
int x3,y3,x4,y4;
line(getmaxx()/2,0,getmaxx()/2,getmaxy());
line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
outtextxy(getmaxx()/2+5,getmaxy()/2+5,"(0,0)");
outtextxy(getmaxx()-50,getmaxy()/2+10,"X-Axis");
outtextxy(getmaxx()/2+10,20,"Y-Axis");
outtextxy(10,getmaxy()/2+10,"X'-Axis");
outtextxy(getmaxx()/2+10,getmaxy()-20,"Y'-Axis");

6
rectangle(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+xp[1],getmaxy()/2-yp[1]);
if(z>=xp[0])
{
x3=z+xp[0];
y3=z+yp[0];
x4=z+xp[1];
y4=z+yp[1];
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
else
{
x3=xp[0]-z;
y3=yp[0]-z;
x4=xp[1]-z;
y4=yp[1]-z;
rectangle(getmaxx()/2+x3,getmaxy()/2-y3,getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[0],getmaxx()/2+x3,getmaxy()/2-y3);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[1],getmaxx()/2+x4,getmaxy()/2-y4);
line(getmaxx()/2+xp[0],getmaxy()/2-yp[1],getmaxx()/2+x3,getmaxy()/2-y4);
line(getmaxx()/2+xp[1],getmaxy()/2-yp[0],getmaxx()/2+x4,getmaxy()/2-y3);
}
}

void matrixmul(int a[4][4])


{
float res[4][1],b[4][1];
int i,j,k,l;
for(i=0;i<2;i++)
{
b[0][0]=xp[i];
b[1][0]=yp[i];
b[2][0]=z;
b[3][0]=1;
for(j=0;j<4;j++)
{
for(k=0;k<1;k++)
{
res[j][k]=0;
for(l=0;l<4;l++)
{
res[j][k]=res[j][k]+(a[j][l]*b[l][k]);
}
}
}
xp[i]=res[0][0];
yp[i]=res[1][0];
}

7
z=res[2][0];
display(xp,yp);
}

Program 2: Point Clipping Algorithm


/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Point Clipping
*********************************************************************/
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class pointclip
{
private:
int x[10],y[10],a[10],b[10],c[10],d[10],n;
public:
void drawrect();
void getpixeldata();
void result();
}clip;
void pointclip::drawrect()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,”c:\\tc\\bgi”);
cout<<”Enter the coordinates of rectangle”;
cin>>x[1]>>y[1]>>x[2]>>y[2];
rectangle(x[1],y[1],x[2],y[2]);
getch();
}
void pointclip::getpixeldata()
{
cout<<”Enter the total nymber of pixels want to plot:\n:”;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<”Enter the pixel position(x,y)”<<i<<”;”;
cin>>a[i]>>b[i];
putpixel(a[i],b[i],15);
}
}
void pointclip::result()
{
cleardevice();
cout<<”Result for the point clipping program:”;

8
rectangle(x[1],y[1],x[2],y[2]);
for(int i=1;i<=n;i++)
if((a[i]<=x[2]&&a[i]>=x[i]&&b[i]<=y[2]&&b[i]>=y[i]))
putpixel(a[i],b[i],15);
getch();
closegraph();
}
void main()
{
clip.drawrect();
clip.getpixeldata();
clip.result();
}

Program 3: Cohen Sutherland Line Clipping Algorithm


/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Cohen-Sutherland
*********************************************************************/
#include<stdio.h>
#include<graphics.h>
#include<conio.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)

9
{
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
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)
code |= LEFT;

10
return(code);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
}

Program 4: Liang Barsky Line Clipping Algorithm


/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Liang-Barsky Line Clipping Algorithm
*********************************************************************/
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;

11
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) || ( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) || ( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )
u1 = max(r1 , u1 );
else
u2 = min(r1 , u2 );
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = max(r2 , u1 );
else
u2 = min(r2 , u2 );

12
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = max(r3 , u1 );
else
u2 = min(r3 , u2 );
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = max(r4 , u1 );
else
u2 = min(r4 , u2 );
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");


printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordiante are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d ",wxmin,wymin,wxmax,wymax);

printf("New coordinates are \n");


printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(2);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(1);
line(x1,y1,x2,y2);
getch();
setcolor(0);
line(x1,y1,x2,y2);
setcolor(3);
line(x11,y11,x22,y22);
getch();
}}}

Program 5: Sutherland Hodgeman Polygon Clipping Algorithm


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

13
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Sutherland Hodgeman
*********************************************************************/
#include <iostream.h>
#include <conio.h>
#include <graphics.h>
#define MAXVERTICES 11
#define MAXCORDINATES ((MAXVERTICES * 2) + 1)

int corArray[MAXCORDINATES],nVertices=0;
int corArray2[MAXCORDINATES],nVertices2=0;
int wx_min,wx_max,wy_min,wy_max;

enum boundry {left=0,right,bottom,top};

void initialize()
{
int gDriver = DETECT,gMode;
initgraph(&gDriver,&gMode,"C:\\tc\\bgi");
cleardevice();
}
void inputPolygon()
{
cout<<"\nEnter No of Vertices (Min. 3,Max. "<<MAXVERTICES<<"): ";
cin>>nVertices;
if(nVertices < 3 || nVertices > MAXVERTICES) nVertices = 3;
for(int i=0;i<nVertices;i++)
{
cout<<"\nEnter Cordinates of Vertex "<<i+1<<" (x,y) : ";
cin>>corArray[2*i]>>corArray[(2*i)+1];
}
corArray[2*(nVertices)] = corArray[0];
corArray[(2*(nVertices))+1] = corArray[1];
}

void samplePolygon()
{
int midx = getmaxx()/2;
int midy = getmaxy()/2;
int polygon_vertices[]={ midx,midy-125,
midx-200,midy-50,
midx-75,midy-50,
midx-75,midy+200,
midx+75,midy+200,
midx+75,midy-50,
midx+200,midy-50,
midx,midy-125};
nVertices = 8;
for(int i = 0;i<nVertices*2;i++)

14
corArray[i] = polygon_vertices[i];
}

int insideWindow(int x,int y,boundry b)


{
switch(b)
{
case left:
return (x > wx_min);
case right:
return (x < wx_max);
case bottom:
return (y < wy_min);
case top:
return (y > wy_max);
}
return 0;
}

int calcCondition(int x1,int y1,int x2,int y2,boundry b)


{
int stat1 = insideWindow(x1,y1,b);
int stat2 = insideWindow(x2,y2,b);
if(!stat1 && stat2) return 1;
if(stat1 && stat2) return 2;
if(stat1 && !stat2) return 3;
if(!stat1 && !stat2) return 4;
return 0; //never executed
}

void solveIntersection(boundry b,int x1,int y1,int x2,int y2,int &x,int &y)


{
float m = 0;
if(x2 != x1) m = ((float)(y2-y1)/(float)(x2-x1));
switch(b)
{
case left:
x = wx_min;
y = y1 + m * (x - x1);
break;
case right:
x = wx_max;
y = y1 + m * (x - x1);
break;
case bottom:
y = wy_min;
if(x1 != x2)
x = x1 + (float)(1/m) * (y - y1);
else
x = x1;
break;

15
case top:
y = wy_max;
if(x1 != x2)
x = x1 + (float)(1/m) * (y - y1);
else
x = x1;
break;
}
}

void addNewPoint(int x,int y)


{
corArray2[(2 * nVertices2)] = x;
corArray2[(2 * nVertices2)+1] = y;
nVertices2++;
}

void clipPolygon()
{
int j,k;
boundry i;
for(i=left;i<=top;i++)
{
nVertices2 = 0;
for(j=0;j<nVertices-1;j++)
{
int x1 = corArray[(2*j)];
int y1 = corArray[(2*j)+1];
int x2 = corArray[(2*(j+1))];
int y2 = corArray[((2*(j+1))+1)];
int condition = calcCondition(x1,y1,x2,y2,i);
int _x=0,_y=0;
switch(condition)
{
case 1:
solveIntersection(i,x1,y1,x2,y2,_x,_y);
addNewPoint(_x,_y);
addNewPoint(x2,y2);
break;
case 2:
addNewPoint(x2,y2);
break;
case 3:
solveIntersection(i,x1,y1,x2,y2,_x,_y);
addNewPoint(_x,_y);
break;
case 4:
break;
}
}

16
addNewPoint(corArray2[0],corArray2[1]);

//update corArray and nvertices...


nVertices = nVertices2;
for(j=0;j<nVertices*2;j++)
corArray[j] = corArray2[j];
}
}

void main()
{
clrscr();
initialize();
inputPolygon();
samplePolygon();

cout<<"\nEnter Window Parameters (wxmin,wxmax,wymin,wymax) : ";


cin>>wx_min>>wx_max>>wy_min>>wy_max;

rectangle(wx_min,wy_max,wx_max,wy_min);
drawpoly(nVertices+1,corArray);
getch();
clipPolygon();
clrscr();
cleardevice();
rectangle(wx_min,wy_max,wx_max,wy_min);
drawpoly(nVertices+1,corArray);
getch();
}

Program 6: Perspective Projections


/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Perspective Projection
*********************************************************************/
#include<stdio.h>
#include<math.h>
#include<graphics.h>

main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;

17
int xp, yp, zp, x, y, z;

a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;


a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;
a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;
a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");

ymax = getmaxy();
xp = 300; yp = 320; zp = 100;

for(j=0; j<8; j++)


{
x = a[0][j]; y = a[1][j]; z = a[2][j];
b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);
b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}

/*- front plane display -*/


for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);

/*- back plane display -*/


setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1, ymax-y1, x2, ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);

setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];

18
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch();
}

Program 7: Parallel Projections


/*********************************************************************
Name: Atishay Jain
Subject: Computer Graphics
Roll No: 10783017
Program: Oblique Projection
*********************************************************************/
#include<stdio.h>
#include<math.h>
#include<graphics.h>

main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
double L1,phi;

a[0][0] = 100; a[1][0] = 100; a[2][0] = 100;


a[0][1] = 200; a[1][1] = 100; a[2][1] = 100;
a[0][2] = 200; a[1][2] = 200; a[2][2] = 100;
a[0][3] = 100; a[1][3] = 200; a[2][3] = 100;
a[0][4] = 100; a[1][4] = 100; a[2][4] = 200;
a[0][5] = 200; a[1][5] = 100; a[2][5] = 200;
a[0][6] = 200; a[1][6] = 200; a[2][6] = 200;
a[0][7] = 100; a[1][7] = 200; a[2][7] = 200;

phi = (double) (3.14*45.0)/180 ;


L1 = 0.5;

par[0][0] = 1; par[0][1] = 0;
par[0][2] = L1*cos(phi); par[0][3] = 0;

par[1][0] = 0; par[1][1] = 1;
par[1][2] = L1*sin(phi); par[1][3] = 0;

par[2][0] = 0; par[2][1] = 0;
par[2][2] = 0; par[2][3] = 0;

par[3][0] = 0; par[3][1] = 0;
par[3][2] = 0; par[3][3] = 1;

19
m=4; n=4; p=8;

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


for(k=0; k<p; k++)
b[i][k] = 0;

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


for(k=0; k<p; k++)
for(j=0; j<n; j++)
b[i][k] += (float)par[i][j] * a[j][k];

detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");

ymax = getmaxy();

/*- front plane display -*/

for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1,ymax-y1,x2,ymax-y2);

/*- back plane display -*/

setcolor(11);
for(j=4;j<7;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1,ymax-y1,x2,ymax-y2);

setcolor(13);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1,ymax-y1,x2,ymax-y2);
}
getch();
}

20

Potrebbero piacerti anche