You must be logged in to post Login Register


Register? | Lost Your Password?

Search Forums:


 






Wildcard Usage:
*    matches any number of characters
%    matches exactly one character

Programs for 2D Transformation using C++ : Computer Graphics

UserPost

1:05 pm
September 19, 2010


indianchora

Nursery Kid

posts 7

C++ program for 2D transformation

 

#include<dos.h>
#include<math.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>

void main()
{
   int xmax,ymax,gd=0,gm,tx,ty,k;
   initgraph(&gd,&gm,"c:\\tc\\bgi");
   xmax=getmaxx();
   ymax=getmaxy();
       line(xmax/2,0,xmax/2,ymax);
       line(0,ymax/2,xmax,ymax/2);
       int a[10][10],b[10][10],c[10][10],i,j,n;
       cout<<"Enter no of vertices:-";
       cin>>n;
       for(i=0;i<n;i++)
       {
         cout<<"\nEnter co-ordi of "<<i+1<<"point:";
          for(j=0;j<3;j++)
           {
              cin>>a[i][j];
           }
       }
       setcolor(46);

   int ch1,ch2,y=1,sx,sy;
   while(y==1)
   {
       for(i=0;i<n-1;i++)
       {
          line(xmax/2+a[i][0],ymax/2-a[i][1],xmax/2+a[i+1][0],ymax/2-a[i+1][1]);
       }
       line(xmax/2+a[i][0],ymax/2-a[i][1],xmax/2+a[0][0],ymax/2-a[0][1]);

        setcolor(15);
        cout<<"\n1.translation \n2.Rotation\n3.Reflection\n4.Scaling";
        cout<<"\nEnter ur choice:-";
        cin>>ch1;
        switch(ch1)
        {
          case 1:
            b[0][0]=1;b[0][1]=0;b[0][2]=0;
            b[1][0]=0;b[1][1]=1;b[1][2]=0;
            cout<<"\nEnter tx and ty:-";
            cin>>tx>>ty;
            b[2][0]=tx;b[2][1]=ty;b[2][2]=1;
            break;
           case 2:
            cout<<"\n1.90 \t2.180\t3.270\t4.360";
            cout<<"\nEnter ur choice:-";
            cin>>ch2;
              switch(ch2)
              {
               case 1:
                b[0][0]=0;b[0][1]=1;b[0][2]=0;
                b[1][0]=-1;b[1][1]=0;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 2:
                b[0][0]=-1;b[0][1]=0;b[0][2]=0;
                b[1][0]=0;b[1][1]=-1;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 3:
                b[0][0]=0;b[0][1]=-1;b[0][2]=0;
                b[1][0]=1;b[1][1]=0;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 4:
                b[0][0]=1;b[0][1]=0;b[0][2]=0;
                b[1][0]=0;b[1][1]=1;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
              }    //end rot switch
          break;
          case 3:
            cout<<"\n1.Ref thru x axis \n2.Ref thru y axis\n3.Ref thru x=y axis\n4.Ref thru x=-y axis";
            cout<<"\nEnter ur choice:-";
            cin>>ch2;
              switch(ch2)
              {
               case 1:
                b[0][0]=1;b[0][1]=0;b[0][2]=0;
                b[1][0]=0;b[1][1]=-1;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 2:
                b[0][0]=-1;b[0][1]=0;b[0][2]=0;
                b[1][0]=0;b[1][1]=1;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 3:
                b[0][0]=0;b[0][1]=1;b[0][2]=0;
                b[1][0]=1;b[1][1]=0;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
               case 4:
                b[0][0]=0;b[0][1]=-1;b[0][2]=0;
                b[1][0]=-1;b[1][1]=0;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
                break;
              }    //end ref switch
          break;
          case 4:
            cout<<"\nEnter scaling factors sx and sy:-";
            cin>>sx>>sy;
                b[0][0]=sx;b[0][1]=0;b[0][2]=0;
                b[1][0]=0;b[1][1]=sy;b[1][2]=0;
                b[2][0]=0;b[2][1]=0;b[2][2]=1;
          break;
          default:
          cout<<"\nEnter correct choice:-";
          continue;

     }//end switch

     for(i=0;i<n;i++)
     {
       for(j=0;j<3;j++)
     {
        c[i][j]=0;
        for(k=0;k<3;k++)
         {
           c[i][j]=c[i][j]+a[i][k]*b[k][j];
         }
     }
     }
       setcolor(4);
       for(i=0;i<n-1;i++)
       {
          line(xmax/2+c[i][0],ymax/2-c[i][1],xmax/2+c[i+1][0],ymax/2-c[i+1][1]);
       }
       line(xmax/2+c[i][0],ymax/2-c[i][1],xmax/2+c[0][0],ymax/2-c[0][1]);

       cout<< "do u want to continue(1=yes/0=no):-";
       cin>>y;
       cleardevice();
       clrscr();
       setcolor(15);
       line(xmax/2,0,xmax/2,ymax);
       line(0,ymax/2,xmax,ymax/2);
   }//end while
//   getch();
}

IndianChora – The Desi Engineer !

5:32 pm
January 13, 2012


mongillaroti

Nursery Kid

posts 2

Really Great program in C++.


About the Mera Campus Forum

Forum Timezone: Asia/Kolkata

Currently Online: cbsesamplepapers
22 Guests

Currently Browsing this Topic:
1 Guest

Forum Stats:

Groups: 9
Forums: 55
Topics: 139
Posts: 190

Membership:

There are 282 Members
There have been 2 Guests

There is 1 Admin
There is 1 Moderator

Top Posters:

premsempire – 10
indianchora – 7
newsero – 6
aryan.kunal – 6
Fairfax – 4
senguptaphysics – 3

Recent New Members: cbsesamplepapers, AnnaSusi, santosh, ProwNetrete, mak1860, Kishorsing

Administrators: Hari Maurya (113 Posts)

Moderators: Mr. Spark (14 Posts)