Friday, November 9, 2018

Source code for Rotation about fixed point in Computer graphics using C.

                                           CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
if(n>2)
{
 cout<<"Enter the co-ordinates of polygon:";
 cout<<"\n";
 for(int i=0;i<n;i++)
 {
 cout<<"x"<<i<<" y"<<i<<":-";
 cin>>ln[i][0]>>ln[i][1];
 }

ln[n][0]=ln[0][0];
ln[n][1]=ln[0][1];

for(i=0;i<n;i++)
{
  line(ln[i][0],ln[i][1],ln[i+1][0],ln[i+1][1]);
}
double ag;
double mul[10][10];
double rad;

double mat[3][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }
   for(int p=0;p<n;p++)
mat[2][p]=1;                 //homogeneous co-ordinate

double rot[3][3];
double t1[3][3];
double t2[3][3];
double ln1[10][2];
double mul1[10][10];
double mul2[10][10];
double mul3[10][10];
double tx,ty;
int ch;

cout<<"\n1.To Rotate Anti-clockwise!!";
cout<<"\n2.To Rotate clockwise!!";
cout<<"\nEnter your choice:";
cin>>ch;

   cout<<"Enter Center (Xc,Yc):";
cin>>tx>>ty;

t1[0][0]=1;
t1[0][1]=0;
t1[0][2]=-tx;
t1[1][0]=0;                      //to bring at origin
t1[1][1]=1;
t1[1][2]=-ty;
t1[2][0]=0;
t1[2][1]=0;
t1[2][2]=1;

t2[0][0]=1;
t2[0][1]=0;
t2[0][2]=tx;
t2[1][0]=0;
t2[1][1]=1;                       //to bring at original position
t2[1][2]=ty;
t2[2][0]=0;
t2[2][1]=0;
t2[2][2]=1;




  switch(ch)
  {

  case 1: cout<<"\nEnter the angle of rotation:";
       cin>>ag;
      rad=ag/180;
      rot[0][0]=cos(rad);
      rot[0][1]=-sin(rad);
      rot[0][2]=0;
      rot[1][0]=sin(rad);
      rot[1][1]=cos(rad);
      rot[1][2]=0;
      rot[2][0]=0;
      rot[2][1]=0;
      rot[2][2]=1;


  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*rot[k][j];
       }
      }
      }
  break;

case 2:cout<<"\nEnter the angle of rotation:";
cin>>ag;
rad=ag/180;
      rot[0][0]=cos(rad);
      rot[0][1]=sin(rad);
      rot[0][2]=0;
      rot[1][0]=-sin(rad);
      rot[1][1]=cos(rad);
      rot[1][2]=0;
      rot[2][0]=0;
      rot[2][1]=0;
      rot[2][2]=1;


  for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul1[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul1[i][j]=mul1[i][j]+t2[i][k]*rot[k][j];
       }
      }
      }
  break;

default:cout<<"Wrong Choice!!" ;
break;
}


for(i=0;i<3;i++)
    {
     for(int j=0;j<3;j++)
     {
       mul2[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul2[i][j]=mul2[i][j]+mul1[i][k]*t1[k][j];
       } } }

  for(i=0;i<3;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul3[i][j]=0;
       for(int k=0;k<3;k++)
       {
       mul3[i][j]=mul3[i][j]+mul2[i][k]*mat[k][j];
       } } }

for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
      ln1[j][i]=mul3[i][j];
      }
    }

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
for(i=0;i<n;i++)
  {
   setcolor(CYAN);
   line(ln1[i][0],ln1[i][1],ln1[i+1][0],ln1[i+1][1]);
  }

}
else
{
  cout<<"Polygon is not valid!!";
}

cout<<"\nDo you wanna continue(y/n):";
cin>>ch;
}
closegraph();
}                     

                                     OUTPUT


                          

                                                              

No comments:

Post a Comment

Source code for Happy Diwali Wishing program in C Graphics.

                                           SOURCE CODE #include<graphics.h> #include<stdio.h> #include<conio.h> ...