3D Projection - Display of 3D object such as Cube using General Parallel Projection
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void input(int x[],int y[],int z[],int no)
{
int i;
printf("Enter the co-ordinates\n");
for(i=0;i<no;i++)
{
printf("P[%d]\t X%d:\t",i+1,i+1);
scanf("%d",&x[i]);
printf("P[%d]\t Y%d:\t",i+1,i+1);
scanf("%d",&y[i]);
printf("P[%d]\t Z%d:\t",i+1,i+1);
scanf("%d",&z[i]);
}
}
void printFigure_ortho(int x[],int y[],int z[],int no)
{
int i;
if(no==1)
{
putpixel(x[0],y[0],YELLOW);
}
else
{
for(i=0;i<no-1;++i)
{
line(x[i],y[i],x[i+1],y[i+1]);
}
}
if(no>2)
{
line(x[0],y[0],x[no-1],y[no-1]);
}
}
void getOblique(int x[],int y[],int z[],int no,float angle1,int xp[],int yp[],int choice)
{
int i;
for(i=0;i<no;++i)
{
if(choice==1)
{
xp[i]=x[i]+z[i]/tan(45)*cos(angle1);
yp[i]=y[i]+z[i]/tan(45)*sin(angle1);
}
if(choice==2)
{
xp[i]=x[i]+z[i]/tan(63.4)*cos(angle1);
yp[i]=y[i]+z[i]/tan(63.4)*sin(angle1);
}
}
}
void printFigure(int xp[],int yp[],int no)
{
int i;
if(no==1)
{
putpixel(xp[0],yp[0],YELLOW);
}
else
{
for(i=0;i<no-1;++i)
{
line(xp[i],yp[i],xp[i+1],yp[i+1]);
}
}
if(no>2)
{
line(xp[0],yp[0],xp[no-1],yp[no-1]);
}
}
int main()
{
int gd=DETECT,gm;
int x[10],y[10],z[10],no,xp[10],yp[10],choice;
float angle1;
clrscr();
printf("Enter your choice:\n");
printf("1.Point\n2.Line\n3.Triangle\n4.Square\n");
printf("Or enter no of vertices for any polygon:\n");
scanf("%d",&no);
printf("Enter the angle PHI:\t");
scanf("%f",&angle1);
input(x,y,z,no);
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nOrthographics Projectio(WHITE):");
printFigure_ortho(x,y,z,no);
printf("Oblique Projection(YELLOW).Enter your choice:");
printf("\n1.Cavalier Projection\t2.Cabinet Projection");
scanf("%d",&choice);
getOblique(x,y,z,no,angle1,xp,yp,choice);
setcolor(YELLOW);
printFigure(xp,yp,no);
getch();
return 0;
}
Comments
Post a Comment