Showing posts with label computer graphics. Show all posts
Showing posts with label computer graphics. Show all posts

Sunday, May 4, 2014

Liang - Barsky Plygon clipping Algorithm

algorithm for Clipping Polygon Segments
INPUT : HSA=<h1,h2,...hn> (Half-segment Array)
 w = Rectangle
OUTPUT: cHSA = clipped half-segments and the half-segments
 resulting from the evaluation of turning points
cHSA = Ø;
turningPointSets = Ø;
FOR i=1 TO n DO
 IF (hi has left dominating point) THEN
 IF (SutherlandCohenLineClipping(hi, w, clippedhs, intersection-point,
 isIntersectionPoint)) THEN
 IF (isIntersectionPoint) THEN
 EvaluateTurningPoint(w, intersectionPoint, turningPointSets, hi);
 ELSE
 cHSA.Add(clippedhs);

algorithm to  EvaluateTurningPoint 
INPUT : w = a rectangle described by the coordinates (xmin, ymin) and (xmax, ymax)
 p = Point
 turningPointSets = for each window egde there is a set recording the
 turning point of the edge
 h = halfsegment that the point p belongs to
OUTPUT: If the point is evaluated as a turning point it is added to the Turning
 Point Set of the edge
 tp = p;
IF (p.x = w.xmin) THEN //left edge
 IF (h.insideAbove) THEN
 tp.direction = UP;
 ELSE
 Tp. direction = DOWN;
 END-IF;
 turningPointSets[LEFT].add(tp);
ELSE //right edge
 IF (h.insideAbove) THEN
 tp. direction = UP;
 ELSE
 tp. direction = DOWN;
 END-IF;
 turningPointSets[RIGHT].add(tp);
END-IF;
IF (p.y = w.ymin) THEN //bottom edge
 IF (h.leftPoint > w.ymin) THEN
 tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
 ELSE
 tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
 END-IF;
 turningPointSets[BOTTOM].add(tp);
ELSE
 IF (p.y = w.ymax) THEN //top edge
 IF (h.leftPoint > w.ymin) THEN
 tp.direction = GetDirection(p, h.leftPoint, xmin, ymin, h.insideAbove);
 ELSE
 tp.direction = GetDirection(p, h.rightPoint, xmin, ymin, h.insideAbove);
 END-IF;
 turningPointSets[BOTTOM].add(tp);
 END-IF;
END-IF;

algorithm to Get Direction 
INPUT: tp = turning point
 p = point of the same half segment that the turning point tp belongs
 and is above tp
 (x, y) = the left coordinate of the vertex of the window edge
 insideAbove = insideAbove flag’s value
IF (insideAbove) THEN
 IF (tp.x > p.x) THEN
 return RIGHT;
 ELSE
 return LEFT;
 END-IF;
ELSE
 IF (tp.x > p.x) THEN
 return LEFT;
 ELSE
 return RIGHT;
 END-IF;

algorithm to Create NewSegments
INPUT: edge = indicates which edge is been handled(LEFT, RIGHT, TOP or
 BOTTOM)
 bPoint,ePoint = end points of the edge
 turningPointSet = a set of the turning points of the edge
 cHSA = set of half segments in which the new half segments will be added

OUTPUT: cHSA with the new half segments
 IF edge == TOP or edge == LEFT THEN
 InsideAbove = false;
 ELSE /*RIGHT or BOTTOM edges*/
 InsideAbove = true;
 END-IF;
 begin = 0;
 end = turningPointSet.size();
 tp = turningPointSet[begin];
 IF (tp.Direction== LEFT or tp.Direction == DOWN) and not tp.Rejected THEN
 cHSA.addHalfSegments(tp,bPoint,InsideAbove);
 DiscardTurningPoints(turningPointSet, tp, ASCENDIN_GORDER, begin);
 END-IF;
 tp = turningPointSet[end];
 IF (tp.Direction== RIGHT or tp.Direction == UP) and not tp.Rejected
 and there is no rejected turning point equals to tp THEN
 cHSA.addHalfSegments(tp,ePoint,InsideAbove);
 DiscardTurningPoints(turningPointSet, DESCENDING_ORDER, end);
 END-IF;
 WHILE (begin<end) DO
tp1 = GetNotRejectedTurningPoint(turningPointSet, ASCENDING_ORDER, begin);
IF tp1 == NULL THEN
 return;
END-IF;
tp2 = GetNotRejectedTurningPoint(turningPointSet, DESCENDING_ORDER, end);
IF tp2 == NULL THEN
 return;
END-IF;
cHSA.addHalfSegments(tp1,tp2,InsideAbove);
END-WHILE;


algorithm for  Polygon Reconstruction 
INPUT: HSA=<h1,h2,...hn> (Halfsegment Array) 
OUTPUT: HSA = each halfsegment has the face, cycle, and edge numbers set 
VARIABLES: face = array that stores in position i the last cycle number of the 
 face i 
 hsSet = array that stores in the position i if the half segment hi 
 had already the face number, the cycle number and the edge 
 number set. This array is initialized with values false. 
 IF HSA is not sorted in halfsegment order THEN 
 Sort HSA in halfsegment order; 
END-IF; 
IF the halfsegments of HSA do not have the partner number set THEN 
 Set partner number of the halfsegments of HSA; 
END-IF; 
face[0] = 0; /*0 is assigned to the first cycle of the first face */ 
lastFaceNumber = 0; 
isFirstHS = true; 
 FOR i=1 TO n DO 
 IF hi has left dominating point and not hsSet[i] THEN 
 IF isFirstHS THEN 
 isFirstHS = false; 
 hi.faceNumber = 0; 
 hi.cycleNumber = 0; 
 ELSE 
 existingFaceNumber = GetFaceNumber(HSA, hi, hsSet, i); 
 IF existingFaceNumber is equal to -1 THEN 
 lastFaceNumber++; 
 hi.faceNumber = lastFaceNumber; 
 hi.cycleNumber = 0; 
 /*to store the first cycle number of the face lastFace*/ 
 face[faceNumber-1]=0; 
 ELSE 
 hi.faceNumber = existingFaceNumber; 
 face[faceNumber]++; 
 hi.cycleNumber = face[faceNumber]; 
 END-IF; 
 END-IF; 
 hi.edgeNumber = 0; 
 ComputeCycle(HSA, hi, hsSet); 
 END-IF; 
 END-FOR; 

window clipping in 
Signature: (line x rect) -> line, (region x rect) --> region 
Syntax: windowclippingin( _, _ ) 
Meaning: computes the part of the object that is inside the window. 
Example: query Flaechen feed extend[InWindow: windowclippingin( 
 .geoData, bbox(thecenter))] project[InWindow] 
 filter[not(isempty(.InWindow))] consume 

 window clipping out 
Signature: (line x rect) -> line, (region x rect) --> region 
Syntax: windowclippingout( _, _ ) 
Meaning: computes the part of the object that is outside the window. 
Example: query windowclippingout(trajectory(train7), bbox(thecenter)) 

Wednesday, April 2, 2014

Errors in programs of computer graphics related to GRAPHICS.H

Some of the Errors Startup and New Programmers face while writing Graphics programs in C or C++  :

  • Messages like  " Cannot open include file < graphics.h > " .
  • Linker errors relating to < graphics.h > .
  • Messages like : Graphics not initialized .
To overcome the above errors and many others, try the following steps
  1. Copy the file [ egavga.bgi ] from the folder BGI to BIN folder 
  2. GoTo OPTIONS >   LINKER > LIBRARIES and mark the Graphics option
  3. Use int gdriver , gmode = DETECT;  initgraph( &gdriver, &gmode, "" ) ;                       before writing any graphics code ;
                                       

Sunday, November 27, 2011

Implementation of Cohen-Sutherland Line clipping Algorithm

#include<graphics.h>
#include<conio.h>
#include<iostream.h>
const int t=1, b=2, r=4, l=8 ;
float xmin,ymin,xmax,ymax;
int calcode (float x,float y)
{ int code =0;
  if(y> ymax) code |=t;
  else if(y<ymin) code |= b;
  else if(x>xmax) code |= r;
  else if(x<xmin) code |= l;
  return(code);
}

void lineclip(float x1,float y1,float x2,float y2)
{ unsigned int code1,code2,codeout;
  int accept = 0, done=0;
  code1 = calcode(x1,y1);
  code2 = calcode(x2,y2);
  do{ if(!(code1 | code2))
    { accept =1 ; done =1; }
    else if(code1 & code2) done = 1;
    else
    { float x,y;
       codeout = code1 ? code1 : code2;
       if(codeout & t)
    { x = x1 + (x2-x1)*(ymax-y1)/(y2-y1);y = ymax;}
       else if(codeout & b)
    {x=x1+(x2-x1)*(ymin-y1)/(y2-y1);y=ymin;}
       else if (codeout & r)
      {y=y1+(y2-y1)*(xmax-x1)/(x2-x1);x=xmax;}
       else
     {y=y1+(y2-y1)*(xmin-x1)/(x2-x1);x=xmin;}
       if(codeout == code1)
      {x1 = x; y1 = y;
      code1=calcode(x1,y1);}
       else
    {x2 = x; y2 = y;
     code2 = calcode(x2,y2);}
   }
  } while( done == 0);
  if(accept)
    line(x1,y1,x2,y2);
    rectangle(xmin,ymin,xmax,ymax);
}

main()
{ float x1,y1,x2,y2;
  int gd=DETECT,gm;
  clrscr();
  initgraph(&gd,&gm,"");
  cout<<"\n\n\t:::Enter the co-ordinates of Line::::\n\tx1 :";cin>>x1;
  cout<<"\n\ty1 :";cin>>y1;
  cout<<"\n\tx2 :";cin>>x2;
  cout<<"\n\ty2 :";cin>>y2;
  cout<<"\n\t:::Enter the co_ordinates of window:::\n ";
  cout<<"\n\txmin :";cin>>xmin;
  cout<<"\n\tymin :";cin>>ymin;
  cout<<"\n\txmax :";cin>>xmax;
  cout<<"\n\tymax :";cin>>ymax;
  clrscr();
  line(x1,y1,x2,y2);
  rectangle(xmin,ymin,xmax,ymax);
  getch();
  clrscr();
  lineclip(x1,y1,x2,y2);
  getch();
  closegraph();
  return 0;
}

Implementation of Sutherland–Hodgman Polygon Clipping Algorithm

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);
enum  {  TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode code1,code2,codeout;
int accept = FALSE,done = FALSE;
code1 = CompOutCode(x0,y0);
code2 = CompOutCode(x1,y1);
do
{
if(!(code1|code2))
{
accept = TRUE;
done = TRUE;
}
else
if(code1 & code2)
done = TRUE;
else
{
float x,y;
 
codeout = code1?code1:code2;
if(codeout & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(codeout & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(codeout & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(codeout==code1)
{
x0 = x;
y0 = y;
code1 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
code2 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
 
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}
void main( )
{float x1,y1,x2,y2;
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{scanf("%d",&poly[i]);}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);
initgraph(&gdriver, &gmode, "");
 
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

program to show movement of circle along a line

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
// function for drawing circle of radius r having centre at m,n
void draw(int m,int n,int r)
{int x,y,p;
x=0;
y=r;
putpixel(m+y,n+x,1);
putpixel(m-y,n+x,1);
putpixel(m+x,n+y,1);
putpixel(m+x,n-y,1);
p=3-(2*r);
while(y>x)
{  if (p<0)
   p=(p+(4*x)+6);
   else
   { y=y-1;
     p=p+((4*(x-y)+10));
   }
   x++;
   putpixel(m+x,n+y,1);
   putpixel(m+x,n-y,1);
   putpixel(m-x,n-y,1);
   putpixel(m-x,n+y,1);
   putpixel(m-y,n-x,1);
   putpixel(m-y,n+x,1);
   putpixel(m+y,n+x,1);
   putpixel(m+y,n-x,1);
}
}

  void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c,r;
       cout<<"Enter the radius ";
       cin>>r;
      cout<<"enter the end points of line to move the circle along ( x1 x2 y1 y2 )\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);dy=abs(y2-y1);
      if(x2>x1)
    s1=1;
    else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;
  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
      clrscr();
      // calling function draw the circle
      draw(x,y,r);
      line(x1,y1,x,y);
    while(e>0)
     { if(c==1)
    { x+=s1;}
       else y=y+s2;
    e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}


implementation of concept of rotation in computer graphics


 #include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
double object[3][3],translate[3][3],output[3][3], rotate[3][3],output2[3][3],output3[3][3];
int n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}
void matmulti(double a[10][3],double b[3][3],double c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(double a[10][3],int n)
{ for(int i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}


void main()
{int i,j,ch,gd=DETECT,gm;
double theta;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.rotate a line";
 cout<<"\n 2.rotate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the fix points tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 cout<<"\nenter the angle to rotate";
 cin>>theta;theta=-theta;
 theta=(3.14)*theta/180;
 rotate[0][0]=cos(theta);
 rotate[0][1]=sin(theta);
 rotate[1][0]=-sin(theta);
 rotate[1][1]=cos(theta);
 rotate[2][2]=1;

 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(translate,rotate,output);
 translate[2][0]=(-translate[2][0]);
 translate[2][1]=(-translate[2][1]);
 matmulti(output,translate,output2);
 matmulti(object,output2,output3);

  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output3,n);
 getch();

}

implementation of concept of translation in computer graphics

Program in c for 2-D Translation of line and Triangle

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
int object[3][3],translate[3][3],output[3][3],n;
void input(int n)
{for(int i=0;i<n;i++)
     {cout<<"\nenter x and y for cordinate #"<<i+1<<endl;
      cin>>object[i][0]>>object[i][1];
     }
}    
void matmulti(int a[10][3],int b[3][3],int c[10][3])
{for(int i=0;i<3;i++)
  for(int j=0;j<3;j++)
   for(int k=0;k<3;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(int a[10][3],int n)
{ for(int i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}     
     

void main()
{int i,j,ch,gd=DETECT,gm;
translate[0][0]=1;
translate[1][1]=1;
translate[2][2]=1;
object[0][2]=1;
object[1][2]=1;
object[2][2]=1;


 cout<<"\n 1.translate a line";
 cout<<"\n 2.translate a triangle";
 cout<<"\n enter your choice : ";
 cin>>ch;
 if(ch==1)
   n=2;
 if(ch==2)
   n=3;
 input(n);
 initgraph(&gd,&gm,"");
 display(object,n);
 getch();
 closegraph();
 cout<<"\nenter the translation factors tx and ty\n";
 cin>>translate[2][0]>>translate[2][1];
 matmulti(object,translate,output);
  initgraph(&gd,&gm,"");
 display(object,n);
 setcolor(12);
 display(output,n);
 getch();

}

Implementation of Midpoint Circle Algorithm

 The Algorithm
 
function line(x0, y0, x1, y1)
   dx := abs(x1-x0)
   dy := abs(y1-y0) 
   if x0 < x1 then sx := 1 else sx := -1
   if y0 < y1 then sy := 1 else sy := -1
   err := dx-dy
 
   loop
     setPixel(x0,y0)
     if x0 = x1 and y0 = y1 exit loop
     e2 := 2*err
     if e2 > -dy then 
       err := err - dy
       x0 := x0 + sx
     end if
     if e2 <  dx then 
       err := err + dx
       y0 := y0 + sy 
     end if
   end loop
 
 
 The Program 
 
 #include<iostream.h>
     #include<conio.h>
     #include<graphics.h>
     #include<math.h>
     #include<dos.h>
     void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c;
      cout<<"enter the coordinates of the line in the form x1 x2 y1 y2\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
      if(x2>x1)
 s1=1;
 else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;

  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
   putpixel(x,y,10);
    while(e>0)
     { if(c==1)
 { x+=s1;}
       else y=y+s2;
 e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}

Wednesday, November 23, 2011

Implementation of DDA Line Drawing Algorithm

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>

void main()
{
clrscr();
float x,y;int x1,x2,y1,y2,dx,dy,l,i=0;
cout<<"Enter the coordinates in the form of x1,y1,x2,y2";
cin>>x1>>y1>>x2>>y2;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
if(abs(x2-x1)>abs(y2-y1))
l=abs(x2-x1);
else
l=abs(y2-y1);
dx=(x2-x1)/l;
dy=(y2-y1)/l;
x=x+0.5;
y=y+0.5;
while(i<=l)
{
putpixel(int(x),int(y),9);
x=x+dx;
y=y+dy;
i++;
}
getch();
}

Implementation of Bresenham's Algorithm For Line Drawing

 The Algorithm
 
function line(x0, y0, x1, y1)
   dx := abs(x1-x0)
   dy := abs(y1-y0) 
   if x0 < x1 then sx := 1 else sx := -1
   if y0 < y1 then sy := 1 else sy := -1
   err := dx-dy
 
   loop
     setPixel(x0,y0)
     if x0 = x1 and y0 = y1 exit loop
     e2 := 2*err
     if e2 > -dy then 
       err := err - dy
       x0 := x0 + sx
     end if
     if e2 <  dx then 
       err := err + dx
       y0 := y0 + sy 
     end if
   end loop
 
 
 The Program 
 
 #include<iostream.h>
     #include<conio.h>
     #include<graphics.h>
     #include<math.h>
     #include<dos.h>
     void main()
     { int dx,dy,s1,s2,x1,x2,y1,y2,x,y,temp,e,i,c;
      cout<<"enter the coordinates of the line in the form x1 x2 y1 y2\n";
      cin>>x1>>y1>>x2>>y2;
      int gdriver=DETECT,gmode;
      initgraph(&gdriver,&gmode,"");
      x=x1;y=y1;
      dx=abs(x2-x1);
      dy=abs(y2-y1);
      if(x2>x1)
 s1=1;
 else s1=-1;
      if(y2>y1)
      s2=1;
      else s2=-1;
  if(dy>dx)
  { temp=dx;
  dx=dy;
  dy=temp;
  c=1;
  }
  else c=0;

  e=2*dy-dx;
  for(i=1;i<=dx;i++)
  {  delay(40);
   putpixel(x,y,10);
    while(e>0)
     { if(c==1)
 { x+=s1;}
       else y=y+s2;
 e-=2*dx;
     }
    if(c==1)
     y+=s2;
    else
     x=x+s1;
  e+=2*dy;
  }
  getch();
}

Monday, October 17, 2011

Implementation of 2-D transformations

C program for translations-scaling -rotation-reflection

#include<iostream.h>
#include<conio.h>
#include<graphics.h>
long double object[10][10],translate[10][10],translate2[10][10],output[10][10],theta;
long double scale[10][10], mirror[10][10],rotate[10][10],output2[10][10],output3[10][10];
int n ,i,j,k;
void init(long double a[10][10])
{ for(i=0;i<10;i++)
     for(j=0;j<10;j++)
     a[i][j]=0;
}
void input(long double a[10][10],int b)
{for( i=0;i<b;i++)
     {cout<<"\nenter row  #"<<i+1<<endl;
      cin>>a[i][0]>>a[i][1]>>a[i][2];
     }
}
void matmulti(long double a[10][10],long double b[10][10],long double c[10][10])
{for(i=0;i<n;i++)
  for(j=0;j<3;j++)
   for(k=0;k<n;k++)
       c[i][j]+=a[i][k]*b[k][j];
}

void display(long double a[10][10])
{ for(i=0;i<n-1;i++)
      line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
       line(a[0][0],a[0][1],a[n-1][0],a[n-1][1]);
}

void matdisp(long double a[10][10],int b)
{  for(i=0;i<b;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<endl;
  }
}

void matdisp(long double a[10][10],long double b[10][10],long double c[10][10])
{  for(i=0;i<3;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"   ";
   cout<<"           ";
   for(j=0;j<3;j++)
   cout<<c[i][j]<<"   ";
   cout<<endl;
  }
}
void matdisp(long double a[10][10],long double b[10][10])
{  for(i=0;i<n;i++)
  {for(j=0;j<3;j++)
   cout<<a[i][j]<<"  ";
   cout<<"              ";
   for(j=0;j<3;j++)
   cout<<b[i][j]<<"  ";
   cout<<endl;
  }
}

int main()
{clrscr();
 int i,j,ch,gd=DETECT,gm;
long double theta;
char choice='n';
 do{
 if(choice=='n')
 {cout<<"\n enter the no of verices in the figure";
  cin>>n;
  cout<<"\ enter the object matrix";
  input(object,n);
 }
  initgraph(&gd,&gm,"");
  display(object);
 getch();
 closegraph();
 cout<<"\n choose an operation";
 cout<<"\n 0.translate ";
 cout<<"\n 1.simple scale";
 cout<<"\n 2.fix point scale";
 cout<<"\n 3.simple rotate";
 cout<<"\n 4.fix point rotate";
 cout<<"\n 5.reflection about x-axis";
  cout<<"\n 6.reflection about y- axis";
 cout<<"\n Enter CHOICE : ";

 cin>>ch;
 if(ch==0)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  matmulti(object,translate,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n translation matrix \n";
  matdisp(translate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
  getch();
}
 if(ch==1)
{ cout<<"\n enter the scaling matrix";
  input(scale,3);
  matmulti(object,scale,output3);
  cout<<endl;
  cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
  cout<<"\n scaling matrix \n";
  matdisp(scale,3);            
  cout<<"\noutput matrix \n";
  matdisp(output3,n);
  getch();

  getch();
}
 if(ch==2)
{ cout<<"\n enter the translation matrix";
  input(translate,3);
  cout<<"\n enter the scaling matrix";
  input(scale,3);
  cout<<"\n enter the inverse translation matrix";
  input(translate2,3);
  cout<<"\n object matrix\n";
  matdisp(object,n);
  cout<<"\n transformation matrixes       \n";
  cout<<"    TRANSLATION            SCALING            DE TRANSLATION  \n";
  matdisp(translate,scale,translate2);
  getch();
  matmulti(translate,scale,output);
  matmulti(output,translate2,output2);
  matmulti(object,output2,output3);
  cout<<"\n output matrix is \n";
  matdisp(output3,n);
  cout<<endl;
  getch();
}
 if(ch==3)
{  cout<<"\n enter the rotation matrix";
   input(rotate,3);
   matmulti(object,rotate,output3);
   cout<<endl;
   cout<<"\n OBJECT MATRICX\n";
  matdisp(object,n);
  getch();
    cout<<"\n rotation matrix \n";
  matdisp(rotate,3);
  cout<<"\n output matrix \n";
  matdisp(output3,n);
   getch();
}

 if(ch==4)
{  cout<<"\n enter the translation matrix";
   input(translate,3);
   cout<<"\n enter the rotation matrix";
   input(rotate,3);
   cout<<"\n enter the inverse translation matrix";
   input(translate2,3);
   cout<<"\n object matrix\n";
   matdisp(object,n);
   cout<<"\n transformation matrixes       \n";
   cout<<"    TRANSLATION            ROTATION            DE TRANSLATION  \n";
   matdisp(translate,rotate,translate2);
   getch();
   matmulti(translate,rotate,output);
   matmulti(output,translate2,output2);
   matmulti(object,output2,output3);
   cout<<"\n output matrix is \n";
   matdisp(output3,n);
   cout<<endl;
   getch();
}
if(ch==5)
{ translate[2][1]=-240;
 translate[2][0]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][1]=240;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
    cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
if(ch==6)
{ translate[2][0]=-320;
 translate[2][1]=0;
  translate[0][0]=1;
  translate[1][1]=1;
  translate[2][2]=1;
  cout<<"\nenter reflection matrix\n";
  input(mirror,3);
  matmulti(translate,mirror,output);
  translate[2][0]=320;
  matmulti(output,translate,output2);
  matmulti(object,output2,output3);
  cout<<"\n input matrix\n";
  matdisp(object,n);
  getch();
  cout<<"\n mirror matrix\n";
  matdisp(mirror,3);
  getch();
  cout<<"\n output matric\n";
  matdisp(output3,n);
  getch();
}
 initgraph(&gd,&gm,"");
 display(object);
 setcolor(12);
 display(output3);
 if(ch==5||ch==6)
 {setcolor(10);
  line(0,240,640,240);
  setcolor(11);
  line(320,0,320,480);
 }
 getch();
 closegraph();
 init(output);
 init(output2);
 init(output3);
 cout<<"\n want to go to main menu  (y/n)\n";
 choice=getche();
}while(choice!='n');
  return 0;
}