Sem 4‎ > ‎CG LAB‎ > ‎

### P9: WAP to perform scaling of a triangle.

posted Feb 23, 2012, 6:24 AM by Neil Mathew   [ updated Feb 23, 2012, 8:03 AM ]
 Note:To make it compatible with the drawpoly function, I converted the 2D matrices holding the coordinates of the points to a 1D matrix which is acceptable by the graphics function. Next time, use line function. The coding becomes simpler, and easier to understand.SOURCE CODE:```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 #include #include #include   int n=4;     //Variables for drawpoly() function int p[8]; int d[8];   int Sx,Sy;   //Scaling factors.       void Input() { cout<<"\n Enter the scaling factor of x:"; cin>>Sx; cout<<" Enter the scaling factor of y:"; cin>>Sy;   cout<<"\n Using default triangle values \n                               | A(0,-20), B(20,-20), C(20, 20) |"; cout<<"\n\n  Click to Continue... "; getch(); cleardevice();   p[0]=0; p[1]=-30; p[2]=-30; p[3]=+30; p[4]=+30; p[5]=+30; p[6]=0; p[7]=-30;   }     void Draw(int Xmid, int Ymid) {   for(int i=0; i<8; i++) {         if( i%2==0 )         d[i]=Xmid+p[i];         else         d[i]=Ymid+p[i]; }   drawpoly(n,d); getch(); } . void Scale() { int i,j,k;   // Matrix with Triangle Coordinates int xy1[3][3] = { p[0], p[1], 1,                   p[2], p[3], 1,                   p[4], p[5], 1,                 };   // Matrix with Scaling Factors int SI[3][3] = { Sx, 0, 0,                  0, Sy, 0,                  0,  0, 1                };   // Matrix with Coordinates after Scaling. int XY1[3][3];   //Matrix multiplication: xy1 x SI = XY1 for(i=0; i<3; i++) { for(j=0; j<3; j++) {         XY1[i][j]=0; for(k=0; k<3; k++) {         XY1[i][j]+=xy1[i][k]*SI[k][j]; } } }   // Converting Final Matrix into Single Array k=0; for(i=0; i<3; i++) { for(j=0; j<2; j++)   // Till 2 because '1' not needed. { p[k++]=XY1[i][j]; } } p[k++]=XY1[0][0]; p[k]=XY1[0][1]; }   void main() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"C:/TC/BGI"); Input();   setcolor(CYAN); outtextxy(10,getmaxy()*0.25," BEFORE SCALING: "); Draw(getmaxx()*0.25,getmaxy()*0.5 );   Scale();   setcolor(GREEN); outtextxy(getmaxx()*0.5,getmaxy()*0.25," AFTER SCALING: "); Draw(getmaxx()*0.75, getmaxy()*0.5);   closegraph(); }```OUTPUT: