Friday, November 9, 2018

Source code for Reflection in Computer graphics using C.

                                         CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,"");
char ch='y';
while(ch=='y'||ch=='Y')
{
float mul[10][10];
float ln1[10][10];
int x[3][3]={{1,0,0},{0,-1,0},{0,0,1}};
int y[3][3]={{-1,0,0},{0,1,0},{0,0,1}};
int xny[3][3]={{-1,0,0},{0,-1,0},{0,0,1}};
int xy[3][3]={{0,1,0},{1,0,0},{0,0,1}};
int nxy[3][3]={{0,-1,0},{-1,0,0},{0,0,1}};
int a=getmaxx();
int b=getmaxy();

int n;
cout<<"\nEnter the no. of edges in a polygon:";
cin>>n;
int ln[10][2];
int choice;

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];
 }
cout<<"\n1. Along x-axis!!";
cout<<"\n2. Along y-axis!!";
cout<<"\n3. Along,when mirror is placed diagonally!!";
cout<<"\n4. Along y=x!!";
cout<<"\n5. Along y=-x!!";
cout<<"\nEnter your choice:";
cin>>choice;
cleardevice();
line(0,b/2,a,b/2);
line(a/2,0,a/2,b);

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]);
}

float 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;
   }

   switch(choice)
   {
case 1:
   for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+x[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]+=b;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

 case 2:    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+y[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][0]+=a;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

case 3:   for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+xny[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][0]+=a;
  ln1[p][1]+=b;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];
    break;

 case 4:   line(319,239,639,0);
    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+xy[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]-=b;
  ln1[p][0]+=a/3;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];

    break;

 case 5: line(319,239,0,0);
    for(i=0;i<3;i++)
    {
    for(int j=0;j<n;j++)
      {
      mul[i][j]=0;
     for(int k=0;k<3;k++)
      {
      mul[i][j]=mul[i][j]+nxy[i][k]*mat[k][j];
      }}}
       for(i=0;i<2;i++)
   {
     for(int j=0;j<n;j++)
      {
       ln1[j][i]=mul[i][j];
}
       }
  for(p=0;p<n;p++)
  {
  ln1[p][1]+=b/2;
  ln1[p][0]+=a-a/3;
  }
ln1[n][0]=ln1[0][0];
ln1[n][1]=ln1[0][1];

    break;

default:cout<<"\Wrong choice!!";
break;
}

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!!";
}
getch();
cleardevice();
cout<<"\nDo you wanna continue(y/n)?";
cin>>ch  ;
}
closegraph();
}

                                      OUTPUT







                     

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


                          

                                                              

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

                                         CODE

#include<iostream.h>
#include<graphics.h>
#include<conio.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]);
}

float mat[10][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


int ch;

cout<<"\n1.To increase the size!!";
cout<<"\n2.To reduce the size!!";
cout<<"\nEnter your choice:";
cin>>ch ;
float sc[3][3];
float t1[3][3];
float t2[3][3];
int ln1[10][2];
float mul1[10][10];
float mul2[10][10];
float mul3[10][10];
float tx,ty;
float inr;
float dec;
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 increment factor:";
cin>>inr;
sc[0][0]=inr;
sc[0][1]=0;
sc[0][2]=0;
sc[1][0]=0;
sc[1][1]=inr;                  //for scaling
sc[1][2]=0;
sc[2][0]=0;
sc[2][1]=0;
sc[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]*sc[k][j];
       } } }
break;


case 2:  cout<<"\nEnter Decrement factor:";
cin>>dec;
sc[0][0]=dec;
sc[0][1]=0;
sc[0][2]=0;
sc[1][0]=0;
sc[1][1]=dec;                  //for scaling
sc[1][2]=0;
sc[2][0]=0;
sc[2][1]=0;
sc[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]*sc[k][j];
       } } }
break;

default:cout<<"Your choice is wrong!!";
 }

   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(RED);
   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


                             








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





                                

Source code for scaling about origin in Computer graphics using C.

                                           CODE


#include<iostream.h>
#include<graphics.h>
#include<conio.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]);
}

float mat[2][10];
for(i=0;i<2;i++)
   {
    for(int j=0;j<n;j++)
    {
    mat[i][j] =ln[j][i];
    }
   }
int ch;
float mul[10][10];
cout<<"\n1.To increase the size!!";
cout<<"\n2.To reduce the size!!";
cout<<"\nEnter your choice:";
cin>>ch ;
float sc[2][2];
float inr;
float dec;
  switch(ch)
  {
  case 1:cout<<"\nEnter increment factor:";
cin>>inr;
   for(i=0;i<2;i++)
       {
for(int j=0;j<2;j++)
{
   if(i==j)
   sc[i][j]=inr ;
   else
   sc[i][j]=0;
  }
}

  break;


case 2:cout<<"\nEnter decrement factor:";
cin>>dec;
   for(i=0;i<2;i++)
       {
for(int j=0;j<2;j++)
{
   if(i==j)
   sc[i][j]=dec ;
   else
   sc[i][j]=0;
  }
}
   break;

default:cout<<"Your choice is wrong!!";
 }
 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];
     }
      }   }

int ln1[10][2];
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 Scaling!!";
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


                                









Source code for Happy Diwali Wishing program in C Graphics.

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