Write a Program to implement Parallel Projection in 3-Dimensions.

 

Write a Program to implement Parallel Projection in 3-Dimensions.

  1. #include<iostream.h>  
  2. #include<conio.h>  
  3. #include<graphics.h>  
  4. #include<math.h>  
  5. #include<stdio.h>  
  6. #include<stdlib.h>  
  7. #define pi 3.14  
  8. class per  
  9. {  
  10.     float x[10], y[10], z [10], d, m[10], q1[10], r1[10], m1[10];  
  11.     float x1 [10], y1[10], xm, ym, z1 [10], cav[4] [4], p[10] [10];  
  12.     float p1[10] [10];  
  13.     float a, b, c, ang, theta, f;  
  14.     int i, k, j, n;  
  15.     public:  
  16.     void get();  
  17.     void par_v();  
  18.     void par_vnr0();  
  19.     void map();  
  20.     void graph();  
  21.     void project(float m[],float q[], float r[]);  
  22. };  
  23. void per:: perproject(float m[], float q[], float r[])  
  24. {  
  25.     cout<<"\n ENTER WIDTH FOR Z-AXIS d";  
  26.     cin>>d;  
  27.     cout<< "\n AFTER PROJECTION, VERTICES OF POLYGON ARE--";  
  28.             for(i=0;i<n;i++)  
  29.     {  
  30.         cout<<"x?["<<i<<"]="<<x1[i]<< "\t";  
  31.         cout<<"y?["<<i<<"]="<<y1[i]<< "\t";  
  32.         cout<<"z?["<<i<<"]="<<z1[i]<< "\t";  
  33.         cout<<"\n";  
  34.     }  
  35.     getch();  
  36.     clearviewport();  
  37.     graph();  
  38.     for(i=0;i<n-1;i++)  
  39.     {  
  40.         line(m[i]*100+xm,(-q[i]*100+ym),m[i+1]*100+xm,(-n1[i+1]*100+ym));  
  41.   
  42.             }  
  43.     line(m[n-1]*100+xm,(-q[n-1]*100+ym),m[0]*100+xm,(-q[0]*100+ym));  
  44. getch();  
  45. }  
  46. void per::get()  
  47. {  
  48.     cout<< "\n ENTER OF VERTICES";  
  49.             cin>>n;  
  50.     cout<<"\n ENTER";  
  51.             for (i=0;i<n;i++)  
  52.     {  
  53.         cout<<"\n x["?i<<"], y["<<i<<"] and z ["<<i<<"]";  
  54.         cin>>x[i]>>y[i]>>z[i];  
  55.     }  
  56.         cout<<"\n ENTER WIDTH FOR Z-AXIS-d";  
  57.         cin>>d;  
  58.         int ch=100;  
  59.         do  
  60.         {  
  61.             cout<< "\n 1->orthographic projection";  
  62.                                 cout<<"\n 2->oblique projection";  
  63.                                 cout<<"\n 0-> exit";  
  64.             cout<<"\n enter ch";  
  65.             cin>>ch;  
  66.             switch (ch)  
  67.             {  
  68.                 case1:  
  69.                     int ch1=100;  
  70.                 do  
  71.                 {  
  72.                     cout<<"\n 1->for xy viewplane";  
  73.                     cout<<"\n 2->for yz viewplane";  
  74.                     cout"\n 3->for zx viewplane";  
  75.                     cout<<"\n 0->exit";  
  76.                     cout<<"\n enter ch1";  
  77.                     cin>>ch1;  
  78.                     switch(ch1)  
  79.                     {  
  80.                         case1:  
  81.                             x1[i]=x[i];  
  82.                             y1[i]=y[i];  
  83.                             z1[i]=0;  
  84.                             project(x1,y1,z1);  
  85.                             break;  
  86.                         case2:  
  87.                             x1[i]=0;  
  88.                             y1[i]=0;  
  89.                             z1[i]=z[i];  
  90.                             project(x1,y1,z1);  
  91.                             break;  
  92.                         case3:  
  93.                             x1[i]=x[i];  
  94.                             y1[i]=0;  
  95.                             z1[i]=z[i];  
  96.                             project(x1,y1,z1);  
  97.                             break;  
  98.                     }  
  99.                 }  
  100.             while (ch!=0)  
  101.                  break;  
  102.                    case2:  
  103.                 ch1=100;  
  104.                 do  
  105.                 {  
  106.                     cout<<"\n 1->on xy plane";  
  107.                     cout<<"\n 2->cavalier";  
  108.                     cout<<"\n 0->exit";  
  109.                     cout<<"\n enter ch1";  
  110.                     cin>>ch1;  
  111.                     switch(ch1)  
  112.                     {  
  113.                         case1:  
  114.                             par_v();  
  115.                             break;  
  116.                         case2:  
  117.                             par_vnr0();  
  118.                             break();  
  119.                     }  
  120.                     while(ch!=0);  
  121.                     break;  
  122.                 }  
  123.                 while(ch!=0)  
  124.             }  
  125.     void per::par_v()  
  126.     {  
  127.         cout<<"\n enter v (a, b, c) " ;  
  128.         cin>>a>>b>>c;  
  129.         for(i=0; i<n;i++)  
  130.         {  
  131.             x1[i]=x[i]-((a*z[i]/c);  
  132.             y1[i]=y[i]-((b*z[i]/c);  
  133.             project(x1,y1, z1);  
  134.         }  
  135.          }  
  136.         void per::par_vnr0()  
  137.         {  
  138.         cout<<"\n enter f";  
  139.         cin>>f;  
  140.         cout<<"\n enter angle";  
  141.         cin>>angle;  
  142.         theta= ((pi/180)* ang);  
  143.         for(i=0;i<4;i++)  
  144.         {  
  145.             for(j=0;j<4;j++)  
  146.             {  
  147.                 if(i==j)  
  148.                 cav[i] [j]=1;  
  149.                 else  
  150.                     cav[i] [j]=0;  
  151.             }  
  152.         }  
  153.         cav[0] [2]=f*cos(theta);  
  154.         cav[1][2]=f*sin (theta);  
  155.         cav[2][2]=0;  
  156.         for(i=0;i<n;i++)  
  157.         {  
  158.             p[0][i]=x[i];  
  159.             p[1][i]=y[i];  
  160.             p[2][i]=z[i];  
  161.             p[3][i]=1;  
  162.         }  
  163.         for(i=0;i<4;i++)  
  164.         {  
  165.             for(j=0;j<n;j++)  
  166.             {  
  167.                 p1[i][j]=0;  
  168.                 for(k=0;k<4;k++)  
  169.                 {  
  170.                     p1[i][j]+=cav[i][k]*p[k][j];  
  171.                 }  
  172.             }  
  173.         }  
  174.         for(i=0;i<n;i++)  
  175.         {  
  176.             x1[i]=p1[0][i];  
  177.             y1[i]=p1[1][i];  
  178.             z1[i]=p1[i];  
  179.         }  
  180.         project(x1,y1,z1);  
  181.         }  
  182. void per :: map()  
  183. {  
  184.     int gd=DETECT;  
  185.     initgraph (&gd, &gm, "");  
  186.     int errorcode = graphresult();  
  187.     /*an error occurred */  
  188.            if (errorcode!=grOK)  
  189.     {  
  190.         printf("Graphics error: %s \n",grapherrormsg (errorcode));  
  191.                       printf("Press and key to halt: ");  
  192.                       getch();  
  193.                       exit(1); /* terminate with an error code */  
  194.             }  
  195. }  
  196. void per::graph()  
  197. {  
  198.     xm= get maxx()/2;  
  199.     ym=get maxy()/2  
  200.     line (xm,0, xm, 2 * ym);  
  201.     line (0, ym, 2 * xm, ym);  
  202. }  
  203. void main()  
  204. {  
  205.     class per a;  
  206.     clrscr();  
  207.     a.map();  
  208.     a.get();  
  209.     getch();  
  210. }  

Output

Computer Graphics Programs



Post a Comment

Previous Post Next Post