Friday, November 9, 2018

Source code for rotation about origin 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[2][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }


int ch;
int ln1[10][2];
cout<<"\n1.To Rotate Anti-clockwise!!";
cout<<"\n2.To Rotate clockwise!!";
cout<<"\nEnter your choice:";
cin>>ch;
float sn;
double sc[2][2];
  switch(ch)
  {

  case 1: cout<<"\nEnter the angle of rotation:";
       cin>>ag;
      rad=ag/180;


    sc[0][0]=cos(rad);
    sc[0][1]=-sin(rad);
    sc[1][0]=sin(rad);
    sc[1][1]=cos(rad);

  for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul[i][j]=0;
       for(int k=0;k<2;k++)
       {
       mul[i][j]=mul[i][j]+sc[i][k]*mat[k][j];
       }
      }

      }
  break;

case 2:cout<<"\nEnter the angle of rotation:";
cin>>ag;
rad=ag/180;

    sc[0][0]=cos(rad);
    sc[0][1]=sin(rad);
    sc[1][0]=-sin(rad);
    sc[1][1]=cos(rad);
  for(i=0;i<2;i++)
    {
     for(int j=0;j<n;j++)
     {
       mul[i][j]=0;
       for(int k=0;k<2;k++)
       {
       mul[i][j]=mul[i][j]+sc[i][k]*mat[k][j];
       }
      }
      }
  break;

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

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

ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
cout<<"\nPolygon after Rotation!!";
for(i=0;i<n;i++)
  {
   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> ...