Thursday, January 3, 2019

Source code for Happy Diwali Wishing program in C Graphics.

                                           SOURCE CODE

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>

void movingDiwali()
{
int xinc=0,yinc=0;
  for(int i=0;i<190;i++)
  {
      if(xinc>=330)
      {
      xinc=-xinc;
      }
      if(yinc>=410)
      {
      yinc=-yinc;
      }
      setcolor(rand());
      settextstyle(10,0,4);
      outtextxy(abs(xinc),abs(yinc),"Happy Diwali");
      xinc+=5;
      yinc+=5;
      delay(60);
      cleardevice();
    }
}

void concentricCircles()
{
cleardevice();
int x=getmaxx()/2;
int y=getmaxy()/2-30;

for(int i=1,j=0;i<=201,j<11;i+=20,j++)
    {
    setfillstyle(j,j);
    setcolor(WHITE);
    circle(x,y,i);
    floodfill(x+i-5,y,WHITE);
    delay(400);
   }
   settextstyle(10,HORIZ_DIR,4);
outtextxy(45,395,"Rangoli Wala Tayohaar");
}



void heart()
{
 setcolor(RED);
 circle(560,215,30);
 circle(605,215,30);

 setfillstyle(1,RED);
 floodfill(560,215,RED);
 floodfill(605,215,RED);
 floodfill(583,215,RED);

 line(583,280,535,230);     //left
 line(583,280,631,230);      //right
 floodfill(583,260,RED);
}

void textGoyal()
{
cleardevice();
setcolor(WHITE);
settextstyle(7,HORIZ_DIR,6);
outtextxy(50,150,"From:");
for(int i=0,j=0;j<158||i<170;i++,j++)
{

   if(i<170)
   {
outtextxy(50,150,"From:");
outtextxy(0+i,200,"Goyal");
delay(20);
if(i<170)
cleardevice();
   }

   if(j<158)
   {
   outtextxy(50,150,"From:");
   outtextxy(639-j*2,200,"Family");
   delay(20);
   if(j<157)
   cleardevice();
   }
}
 outtextxy(50,150,"From:");
 outtextxy(170,200,"Goyal");
 outtextxy(322,200,"Family");
   heart();
}





void diwaliText1()
{
setcolor(LIGHTGREEN) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(275,260,"..Happy Diwali..")  ;

 setcolor(YELLOW) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(275,160,"..Happy Diwali..")  ;

 setcolor(12) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(275,360,"..Happy Diwali..")  ;
}


void displayBoy()
{
    setcolor(15);
 setfillstyle(SOLID_FILL,BLACK);
 circle(140,160,30);
 floodfill(120,170,WHITE);
 line(135,190,135,200);
 line(147,190,147,200);
 rectangle(110,200,170,290);   //BOY FACE
 setfillstyle(11,14);
 rectangle(135,200,147,290);
 floodfill(142,250,WHITE);

setfillstyle(6,2);
floodfill(123,245,WHITE) ;              //REC SIDE COLOR
setfillstyle(6,2);
floodfill(159,260,WHITE) ;


setcolor(15);
 line(153,220,153,234);
 line(163,220,163,234) ;
 setfillstyle(6,14);    //pocket
 line(153,220,163,220);
 line(153,225,163,225);
 floodfill(155,221,WHITE);
 arc(158,234,180,360,5);

setcolor(15);
 circle(130,155,1);
 circle(152,155,1);

  setfillstyle(9,4);
  setcolor(15);
 line(123,290,123,350);
 line(115,290,115,336);
 line(123,350,94,333);                      //left leg
 line(115,336,87,319);
 line(94,333,94,350);
 line(87,319,87,350);

 line(123,350,115,336);
 line(94,333,87,319);
 line(94,350,87,350);
 floodfill(120,320,WHITE);
 floodfill(105,334,WHITE);
 setfillstyle(9,2);
 floodfill(91,335,WHITE);


 setfillstyle(9,4);
 line(165,290,165,350);
 line(157,290,157,336);
 line(165,350,136,333);
 line(157,336,129,319);
 line(136,333,136,350);
 line(129,319,129,350);                         //right leg

 line(165,350,157,336);
 line(136,333,129,319);
 line(136,350,129,350);
 floodfill(161,320,WHITE);
 floodfill(150,334,WHITE);
 setfillstyle(9,2);
 floodfill(134,335,WHITE);


 arc(130,155,35,160,5);                  //palak
 arc(152,155,35,160,5);

 line(140,158,145,170);            //nose
 line(139,170,145,170);
 arc(142,170,0,180,3);


 setfillstyle(11,WHITE);
 line(170,250,208,285);
 line(208,285,170,265);                            //HANDS
 floodfill(175,257,WHITE);
 setfillstyle(1,YELLOW);
 circle(211,288,5);                             //FIRE
 floodfill(211,288,WHITE);

 arc(140,170,190,350,10);
 line(137,180,141,184);                          //mouth
 line(145,180,142,185);

 setfillstyle(9,7);
 line(161,140,174,133);
 ellipse(139,151,30,193,36,31);                  //hairs
 line(103,157,110,168);
 floodfill(140,125,WHITE);
}

void rangoli()
{
displayBoy();
diwaliText1();
for(int i=0,color=8;i<2200;i++,color++)
 {
   setcolor(color);
   arc(280,80,0+i,120+i,i/30)  ;           //bigger one
   delay(1);
   }


for( i=0,color=5;i<1100;i++,color++)
 {
   setcolor(color);
   arc(470,40,0+i,120+i,i/30)  ;       //right UPPER CORNER
   delay(1);
   }

for( i=0,color=0;i<1000;i++,color++)
 {
   setcolor(color);
   arc(50,80,0+i,120+i,i/20)  ;           //left upper corner
   delay(1);
   }



for(i=0,color=0;i<1300;i++,color++)
 {
   setcolor(color);
   arc(400,150,0+i,120+i,i/40)  ;          //right middle L
   delay(1);
   }

for(i=0,color=0;i<1200;i++,color++)
 {
   setcolor(color);
   arc(580,110,0+i,120+i,i/20)  ;          //right MIDDLE R
   delay(1);
   }
}




void animationRocket()
{
for(int i=0;i<80;i++)
{
diwaliText1();
displayBoy();
setfillstyle(6,RED);
{
 line(207,225-i*4,257,225-i*4);
 line(207,225-i*4,232,165-i*4);     //rocket head
 line(232,165-i*4,257,225-i*4);
 floodfill(232,200-i*4,WHITE);
}
 setfillstyle(1,WHITE);
 {
 line(222,226-i*4,222,285-i*4);
 line(242,226-i*4,242,305-i*4);     //rocket base
 line(222,285-i*4,242,285-i*4);
 floodfill(240,250-i*4,WHITE);
 }
 arc(225,288-i*4,228,20,10);

 settextstyle(11,VERT_DIR,3);
 setcolor(RED);
 outtextxy(237,230-i*4,"ROCKET") ;
 delay(20);
 cleardevice();
 }
}





void rocket()
{
setfillstyle(6,RED);
 line(207,225,257,225);
 line(207,225,232,165);     //rocket head
 line(232,165,257,225);
 floodfill(232,200,WHITE);

 setfillstyle(1,WHITE);
 line(222,226,222,285);
 line(242,226,242,305);     //rocket base
 line(222,285,242,285);
 floodfill(240,250,WHITE);

 arc(225,288,228,20,10);

 settextstyle(11,VERT_DIR,3);
 setcolor(RED);
 outtextxy(237,230,"ROCKET") ;
}

void diwaliText()
{
setcolor(LIGHTGREEN) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(245,210,"....Happy Diwali....")  ;
 delay(1000);
 setcolor(YELLOW) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(245,110,"....Happy Diwali....")  ;
 delay(1000);
 setcolor(12) ;
 settextstyle(10,HORIZ_DIR,4);
 outtextxy(245,310,"....Happy Diwali....")  ;
 delay(800);
}




void animationBoy()
{
for(int i=0;i<60;i++)
{
    setcolor(15);
 setfillstyle(SOLID_FILL,BLACK);
 circle(20+i*2,160,30);
     floodfill(0+i*2,170,WHITE);
 line(15+i*2,190,15+i*2,200);
 line(27+i*2,190,27+i*2,200);
 rectangle(-10+i*2,200,50+i*2,290);   //BOY FACE
 setfillstyle(11,14);
 rectangle(15+i*2,200,27+i*2,290);
   floodfill(22+i*2,250,WHITE);

setfillstyle(6,2);
   floodfill(3+i*2,245,WHITE) ;              //REC SIDE COLOR
setfillstyle(6,2);
   floodfill(39+i*2,260,WHITE) ;


setcolor(15);
 line(33+i*2,220,33+i*2,234);
 line(43+i*2,220,43+i*2,234) ;
 setfillstyle(6,14);    //pocket
 line(33+i*2,220,43+i*2,220);
 line(33+i*2,225,43+i*2,225);
   floodfill(35+i*2,221,WHITE);
 arc(38+i*2,234,180,360,5);

setcolor(15);
 circle(10+i*2,155,1);
 circle(32+i*2,155,1);

  setfillstyle(9,4);
  setcolor(15);
 line(3+i*2,290,3+i*2,350);
 line(-5+i*2,290,-5+i*2,336);
 line(3+i*2,350,-26+i*2,333);                      //left leg
 line(-5+i*2,336,-33+i*2,319);
 line(-26+i*2,333,-26+i*2,350);
 line(-33+i*2,319,-33+i*2,350);

 line(3+i*2,350,-5+i*2,336);
 line(-26+i*2,333,-33+i*2,319);
 line(-26+i*2,350,-33+i*2,350);
floodfill(0+i*2,320,WHITE);
 floodfill(-15+i*2,334,WHITE);
 setfillstyle(9,2);
 floodfill(-29+i*2,335,WHITE);


 setfillstyle(9,4);
 line(45+i*2,290,45+i*2,350);
 line(37+i*2,290,37+i*2,336);
 line(45+i*2,350,16+i*2,333);
 line(37+i*2,336,9+i*2,319);
 line(16+i*2,333,16+i*2,350);
 line(9+i*2,319,9+i*2,350);                         //right leg

 line(45+i*2,350,37+i*2,336);
 line(16+i*2,333,9+i*2,319);
 line(16+i*2,350,9+i*2,350);
 floodfill(41+i*2,320*2,WHITE);
 floodfill(30+i*2,334,WHITE);
 setfillstyle(9,2);
 floodfill(14+i*2,335,WHITE);


 arc(10+i*2,155,35,160,5);                  //palak
 arc(32+i*2,155,35,160,5);

 line(20+i*2,158,25+i*2,170);            //nose
 line(25+i*2,170,18+i*2,170);
 arc(22+i*2,170,0,180,3);

 setfillstyle(11,WHITE);
 line(50+i*2,250,88+i*2,285);
 line(88+i*2,285,50+i*2,265);                            //HANDS
 floodfill(55+i*2,257,WHITE);
 setfillstyle(1,YELLOW);
 circle(91+i*2,288,5);                             //FIRE
 floodfill(91+i*2,288,WHITE);


 arc(20+i*2,170,190,350,10);
 line(17+i*2,180,21+i*2,184);                          //mouth
 line(25+i*2,180,22+i*2,185);

 setfillstyle(9,7);
 line(41+i*2,140,54+i*2,133);
 ellipse(19+i*2,151,30,193,36,31);                  //hairs
 line(-17+i*2,157,-10+i*2,168);
 floodfill(20+i*2,125,WHITE);
   delay(20);
   cleardevice();
   rocket();
   diwaliText1();

}
}

int main()
{
int gd=DETECT;
int gm;
 initgraph(&gd,&gm,"tc:\bgi");
 char a[20];
 int countdown;
 printf("ENTER THE TIME LEFT IN SECONDS:-");
 scanf("\n%d",&countdown);
 for(int i=countdown;i>0;i--)
 {
 sprintf(a,"Happy Diwali In %d Seconds",i);
 settextstyle(7,HORIZ_DIR,4);
 outtextxy(80,210,a);
 delay(1000);
 cleardevice();
 }
int color;

/*for( i=0,color=5;i<100;i+=10,color++)
{
settextstyle(10,HORIZ_DIR,4);
setcolor(color);
outtextxy(rand()%400,rand()%400,"Happy Diwali");
delay(700);
cleardevice();
} */
  movingDiwali();

 diwaliText();
 animationBoy();
 displayBoy();
 delay(50);
 animationRocket();
 rangoli();

 delay(2000);
 concentricCircles();
 delay(2000);
 textGoyal();

 getch();
 closegraph();
 return 0;
 }
                                                 

                                  OUTPUT

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


                                









Tuesday, October 16, 2018

Cross-Zero Game in C++.

                                     Source Code

                         //Cross-Zero Game

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

int choice;

void display(char b[3][3])
{
clrscr();
cout<<"\n\t\tCross-Zero Game!!";
cout<<"\n\t\tPlayer_1 Symbol = X";
cout<<"\n\t\tPlayer_2 Symbol = O";

cout<<"\n\n";
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[0][0]<<"  "<<"|  "<<b[0][1]<<"  "<<"|  "<<b[0][2]<<"  "<<endl;
cout<<"\t\t_____|_____|_____"<<endl;
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[1][0]<<"  "<<"|  "<<b[1][1]<<"  "<<"|  "<<b[1][2]<<"  "<<endl;
cout<<"\t\t_____|_____|_____"<<endl;
cout<<"\t\t     |     |     "<<endl;
cout<<"\t\t"<<"  "<<b[2][0]<<"  "<<"|  "<<b[2][1]<<"  "<<"|  "<<b[2][2]<<"  "<<endl;
cout<<"\t\t     |     |     "<<endl;
}

void player_turn(char b[3][3],int &turn,int &row,int &col)
{
if((b[0][0]!='X'&& b[0][0]!='O') || (b[0][1]!='X'&& b[0][1]!='O') || (b[0][2]!='X'&& b[0][2]!='O')|| (b[1][0]!='X'&& b[1][0]!='O')|| (b[1][1]!='X'&& b[1][1]!='O')|| (b[1][2]!='X'&& b[1][2]!='O')|| (b[2][0]!='X'&& b[2][0]!='O')|| (b[2][1]!='X'&& b[2][1]!='O')|| (b[2][2]!='X'&& b[2][2]!='O'))
{
  if(turn==1)
  {
  cout<<"\n\tPlayer_1 [X] Turn:";
  cin>>choice;
  }
  else if(turn==0)
  {
  cout<<"\n\tPlayer_2 [O] Turn:";
  cin>>choice;
  }


switch(choice)
  {
  case 1:row=0;col=0; break;
  case 2:row=0;col=1; break;
  case 3:row=0;col=2; break;
  case 4:row=1;col=0; break;
  case 5:row=1;col=1; break;
  case 6:row=1;col=2; break;
  case 7:row=2;col=0; break;
  case 8:row=2;col=1; break;
  case 9:row=2;col=2; break;
  default:cout<<"Wrong choice!!";
       break;
   }
    if(turn==1 && b[row][col]!='X' && b[row][col]!='O')
       {
  b[row][col]='X';
       turn=0;
}


    else if(turn==0 && b[row][col]!='X' && b[row][col]!='O')
{
b[row][col]='O';
turn=1;
}
    else {
cout<<"\nAlready Filled, Choose Other Box!!";
player_turn(b,turn,row,col);
}
}
}


int gameover(char b[3][3],int &draw)
{

for(int i=0;i<3;i++)
{
if((b[i][0]==b[i][1] && b[i][0]==b[i][2]) || (b[0][i]==b[1][i] && b[0][i]==b[2][i]))
return 0;
}

if((b[0][0]==b[1][1] && b[0][0]==b[2][2]) || (b[0][2]==b[1][1] && b[0][2]==b[2][0]))
     return 0;

else if((b[0][0]=='X'|| b[0][0]=='O') && (b[0][1]=='X'|| b[0][1]=='O') &&(b[0][2]=='X'|| b[0][2]=='O') &&(b[1][0]=='X'|| b[1][0]=='O') &&(b[1][1]=='X'|| b[1][1]=='O') &&(b[1][2]=='X'|| b[1][2]=='O') &&(b[2][0]=='X'|| b[2][0]=='O') &&(b[2][1]=='X'|| b[2][1]=='O') && (b[2][2]=='X'|| b[2][2]=='O'))
  {
  draw=0;
  return 0;
  }
else
return 1;
}

void main()
{

char ch='y';
while(ch=='y'||ch=='Y')
{
int row=0;
int col=0;
int turn=1;
int draw=1;

char b[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
   int t=1;
   while(t==1)
   {
   display(b);
   player_turn(b,turn,row,col);
   t=gameover(b,draw);
   display(b);
   }

if(turn==0 && draw==1)
    cout<<"\n\t\tPlayer1 [X] is Winner!!" ;
else if(turn==1 && draw==1)
    cout<<"\n\t\tPlayer2 [O] is Winner!!" ;
else if(draw==0 && (turn==0 || turn==1))
    cout<<"\n\t\tGame Draw!!";
cout<<"\n\tDo You Wanna Restart The Game(y/n):" ;
cin>>ch;
}
}
                                     OUTPUT
                 Click here for output

Source code for Happy Diwali Wishing program in C Graphics.

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