Write a Program to implement Parallel Projection in 3-Dimensions.
- #include<iostream.h>
- #include<conio.h>
- #include<graphics.h>
- #include<math.h>
- #include<stdio.h>
- #include<stdlib.h>
- #define pi 3.14
- class per
- {
- float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10];
- float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10];
- float p1[10] [10];
- float a, b, c, ang, theta, f;
- int i, k, j, n;
- public:
- void get();
- void par_v();
- void par_vnr0();
- void map();
- void graph();
- void project(float m[],float q[], float r[]);
- };
- void per:: perproject(float m[], float q[], float r[])
- {
- cout<<"\n ENTER WIDTH FOR Z-AXIS d";
- cin>>d;
- cout<< "\n AFTER PROJECTION, VERTICES OF POLYGON ARE--";
- for(i=0;i<n;i++)
- {
- cout<<"x?["<<i<<"]="<<x1[i]<< "\t";
- cout<<"y?["<<i<<"]="<<y1[i]<< "\t";
- cout<<"z?["<<i<<"]="<<z1[i]<< "\t";
- cout<<"\n";
- }
- getch();
- clearviewport();
- graph();
- for(i=0;i<n-1;i++)
- {
- line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym));
- }
- line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym));
- getch();
- }
- void per::get()
- {
- cout<< "\n ENTER OF VERTICES";
- cin>>n;
- cout<<"\n ENTER";
- for (i=0;i<n;i++)
- {
- cout<<"\n x["?i<<"], y["<<i<<"] and z ["<<i<<"]";
- cin>>x[i]>>y[i]>>z[i];
- }
- cout<<"\n ENTER WIDTH FOR Z-AXIS-d";
- cin>>d;
- int ch=100;
- do
- {
- cout<< "\n 1->orthographic projection";
- cout<<"\n 2->oblique projection";
- cout<<"\n 0-> exit";
- cout<<"\n enter ch";
- cin>>ch;
- switch (ch)
- {
- case1:
- int ch1=100;
- do
- {
- cout<<"\n 1->for xy viewplane";
- cout<<"\n 2->for yz viewplane";
- cout"\n 3->for zx viewplane";
- cout<<"\n 0->exit";
- cout<<"\n enter ch1";
- cin>>ch1;
- switch(ch1)
- {
- case1:
- x1[i]=x[i];
- y1[i]=y[i];
- z1[i]=0;
- project(x1,y1,z1);
- break;
- case2:
- x1[i]=0;
- y1[i]=0;
- z1[i]=z[i];
- project(x1,y1,z1);
- break;
- case3:
- x1[i]=x[i];
- y1[i]=0;
- z1[i]=z[i];
- project(x1,y1,z1);
- break;
- }
- }
- while (ch!=0)
- break;
- case2:
- ch1=100;
- do
- {
- cout<<"\n 1->on xy plane";
- cout<<"\n 2->cavalier";
- cout<<"\n 0->exit";
- cout<<"\n enter ch1";
- cin>>ch1;
- switch(ch1)
- {
- case1:
- par_v();
- break;
- case2:
- par_vnr0();
- break();
- }
- while(ch!=0);
- break;
- }
- while(ch!=0)
- }
- void per::par_v()
- {
- cout<<"\n enter v (a, b, c) " ;
- cin>>a>>b>>c;
- for(i=0; i<n;i++)
- {
- x1[i]=x[i]-((a*z[i]/c);
- y1[i]=y[i]-((b*z[i]/c);
- project(x1,y1, z1);
- }
- }
- void per::par_vnr0()
- {
- cout<<"\n enter f";
- cin>>f;
- cout<<"\n enter angle";
- cin>>angle;
- theta= ((pi/180)* ang);
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(i==j)
- cav[i] [j]=1;
- else
- cav[i] [j]=0;
- }
- }
- cav[0] [2]=f*cos(theta);
- cav[1][2]=f*sin (theta);
- cav[2][2]=0;
- for(i=0;i<n;i++)
- {
- p[0][i]=x[i];
- p[1][i]=y[i];
- p[2][i]=z[i];
- p[3][i]=1;
- }
- for(i=0;i<4;i++)
- {
- for(j=0;j<n;j++)
- {
- p1[i][j]=0;
- for(k=0;k<4;k++)
- {
- p1[i][j]+=cav[i][k]*p[k][j];
- }
- }
- }
- for(i=0;i<n;i++)
- {
- x1[i]=p1[0][i];
- y1[i]=p1[1][i];
- z1[i]=p1[i];
- }
- project(x1,y1,z1);
- }
- void per :: map()
- {
- int gd=DETECT;
- initgraph (&gd, &gm, "");
- int errorcode = graphresult();
- /*an error occurred */
- if (errorcode!=grOK)
- {
- printf("Graphics error: %s \n",grapherrormsg (errorcode));
- printf("Press and key to halt: ");
- getch();
- exit(1); /* terminate with an error code */
- }
- }
- void per::graph()
- {
- xm= get maxx()/2;
- ym=get maxy()/2
- line (xm,0, xm, 2 * ym);
- line (0, ym, 2 * xm, ym);
- }
- void main()
- {
- class per a;
- clrscr();
- a.map();
- a.get();
- getch();
- }
Output