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







                     

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> ...