C Graphics Program For 2 D Transformation






C Graphics Program For 2 D Transformation

Source Code:-





1:  //Program to implement 2D Transformation  

2:  #include<math.h>
3:  #include<stdio.h>
4:  #include<process.h>
5:  #include<iostream.h>
6:  #include<conio.h>
7:  #include<graphics.h>
8:  #include "grplib.h"
9:  #define X 0
10:  #define Y 1
11:  int getx(float mat[3][1])
12:       {
13:       return ceil(mat[0][0]);
14:       }
15:  int gety(float mat[3][1])
16:       {
17:       return ceil(mat[1][0]);
18:       }
19:  void create_mat_pnt(float mat[3][1],int x,int y)
20:       {
21:       mat[0][0]=x;
22:       mat[1][0]=y;
23:       mat[2][0]=1;
24:       }
25:  void create_mat_tran(float mat[3][3],int tx,int ty)
26:       {
27:       int i,j;
28:       for(i=0;i<3;i++)
29:       for(j=0;j<3;j++)
30:            {
31:            if(i==j)
32:            {
33:            mat[i][j]=1.0;
34:            }
35:            else
36:            mat[i][j]=0;
37:            }
38:       mat[0][2]=tx;
39:       mat[1][2]=ty;
40:       }
41:  void create_mat_shearx(float mat[3][3],int tx)
42:       {
43:       int i,j;
44:       for(i=0;i<3;i++)
45:       for(j=0;j<3;j++)
46:            {
47:            if(i==j)
48:            {
49:            mat[i][j]=1.0;
50:            }
51:            else
52:            mat[i][j]=0;
53:            }
54:       mat[0][1]=tx;
55:       }
56:  void create_mat_sheary(float mat[3][3],int ty)
57:       {
58:       int i,j;
59:       for(i=0;i<3;i++)
60:       for(j=0;j<3;j++)
61:            {
62:            if(i==j)
63:            {
64:            mat[i][j]=1.0;
65:            }
66:            else
67:            mat[i][j]=0;
68:            }
69:       mat[1][0]=ty;
70:       }
71:  void create_mat_reflectx(float mat[3][3])
72:       {
73:       int i,j;
74:       for(i=0;i<3;i++)
75:       for(j=0;j<3;j++)
76:            {
77:            if(i==j)
78:            {
79:            mat[i][j]=1.0;
80:            }
81:            else
82:            mat[i][j]=0;
83:            }
84:       mat[1][1]=-1;
85:       }
86:  void create_mat_reflecty(float mat[3][3])
87:       {
88:       int i,j;
89:       for(i=0;i<3;i++)
90:       for(j=0;j<3;j++)
91:            {
92:            if(i==j)
93:            {
94:            mat[i][j]=1.0;
95:            }
96:            else
97:            mat[i][j]=0;
98:            }
99:       mat[0][0]=-1;
100:       }
101:  void create_mat_rot(float mat[3][3],float angle)
102:       {
103:       int i,j;
104:       for(i=0;i<3;i++)
105:       for(j=0;j<3;j++)
106:            {
107:            if(i==j)
108:            mat[i][j]=1;
109:            else
110:            mat[i][j]=0;
111:            }
112:       mat[0][0]=cos(angle);
113:       mat[1][0]=sin(angle);
114:       mat[0][1]=-sin(angle);
115:       mat[1][1]=cos(angle);
116:       }
117:  void create_mat_scale(float mat[3][3],float sx,float sy)
118:       {
119:       int i,j;
120:       for(i=0;i<3;i++)
121:       for(j=0;j<3;j++)
122:            {
123:            if(i==j)
124:            mat[i][j]=1;
125:            else
126:            mat[i][j]=0;
127:            }
128:       mat[0][0]=sx;
129:       mat[1][1]=sy;
130:       }
131:  void display(float mat[3][3])
132:       {
133:       int i,j;
134:       printf("\n");
135:       for(i=0;i<3;i++)
136:            {for(j=0;j<3;j++)
137:                 printf("\t%f",mat[i][j]);
138:            printf("\n");
139:            }
140:       }
141:  void display(float mat[3][1])
142:       {
143:       int i,j;
144:       printf("\n");
145:       for(i=0;i<3;i++)
146:            {for(j=0;j<1;j++)
147:                 printf("\t%f",mat[i][j]);
148:            printf("\n");
149:            }
150:       }
151:  void add(int mat1[3][3],int mat2[3][3],int res[3][3])
152:       {
153:       int i,j;
154:       for(i=0;i<3;i++)
155:            {for(j=0;j<3;j++)
156:            res[i][j]=mat1[i][j]+mat2[i][j];
157:            }
158:       }
159:  void mul(float mat1[3][1],float mat2[3][3],float res[3][1])
160:       {
161:       int i,j,k;
162:       for(i=0;i<3;i++)
163:       for(j=0;j<1;j++)
164:       {
165:       res[i][j]=0;
166:       for(k=0;k<3;k++)
167:            res[i][j]+=mat2[i][k]*mat1[k][j];
168:            }
169:  }
170:  void translate(int poly[],int n ,int tx,int ty)
171:  {
172:  float t_mat[3][3];
173:  float mat1[3][1];
174:  float res[3][1];
175:  for(int i=0;i<n*2;i+=2)
176:       {
177:       create_mat_pnt(mat1,poly[i],poly[i+1]);
178:       create_mat_tran(t_mat,tx,ty);
179:       mul(mat1,t_mat,res);
180:       poly[i]=getx(res);
181:       poly[i+1]=gety(res);
182:       }
183:  }
184:  void shearx(int poly[],int n ,int tx)
185:  {
186:  float t_mat[3][3];
187:  float mat1[3][1];
188:  float res[3][1];
189:  for(int i=0;i<n*2;i+=2)
190:       {
191:       create_mat_pnt(mat1,poly[i],poly[i+1]);
192:       create_mat_shearx(t_mat,tx);
193:       mul(mat1,t_mat,res);
194:       poly[i]=getx(res);
195:       poly[i+1]=gety(res);
196:       }
197:  }
198:  void sheary(int poly[],int n ,int tx)
199:  {
200:  float t_mat[3][3];
201:  float mat1[3][1];
202:  float res[3][1];
203:  for(int i=0;i<n*2;i+=2)
204:       {
205:       create_mat_pnt(mat1,poly[i],poly[i+1]);
206:       create_mat_sheary(t_mat,tx);
207:       mul(mat1,t_mat,res);
208:       poly[i]=getx(res);
209:       poly[i+1]=gety(res);
210:       }
211:  }
212:  void reflectx(int poly[],int n )
213:  {
214:  float t_mat[3][3];
215:  float mat1[3][1];
216:  float res[3][1];
217:  for(int i=0;i<n*2;i+=2)
218:       {
219:       create_mat_pnt(mat1,poly[i],poly[i+1]);
220:       create_mat_reflectx(t_mat);
221:       mul(mat1,t_mat,res);
222:       poly[i]=getx(res);
223:       poly[i+1]=gety(res);
224:       }
225:  }
226:  void reflecty(int poly[],int n )
227:  {
228:  float t_mat[3][3];
229:  float mat1[3][1];
230:  float res[3][1];
231:  for(int i=0;i<n*2;i+=2)
232:       {
233:       create_mat_pnt(mat1,poly[i],poly[i+1]);
234:       create_mat_reflecty(t_mat);
235:       mul(mat1,t_mat,res);
236:       poly[i]=getx(res);
237:       poly[i+1]=gety(res);
238:       }
239:  }
240:  void scale(int poly[],int n ,float sx,float sy)
241:  {
242:  float r_mat[3][3];
243:  float mat1[3][1];
244:  float res[3][1];
245:  for(int i=0;i<n*2;i+=2)
246:       {
247:       create_mat_pnt(mat1,poly[i],poly[i+1]);
248:       create_mat_scale(r_mat,sx,sy);
249:       mul(mat1,r_mat,res);
250:       poly[i]=getx(res);
251:       poly[i+1]=gety(res);
252:       }
253:  }
254:  void rotate(int poly[],int n ,float angle)
255:  {
256:  float r_mat[3][3];
257:  float mat1[3][1];
258:  float res[3][1];
259:  for(int i=0;i<n*2;i+=2)
260:       {
261:       create_mat_pnt(mat1,poly[i],poly[i+1]);
262:       create_mat_rot(r_mat,angle*.0174);
263:       mul(mat1,r_mat,res);
264:       poly[i]=getx(res);
265:       poly[i+1]=gety(res);
266:       }
267:  }
268:  void main()
269:       {
270:       int x1=300,y1=100,x2=400,y2=200,x3=400,y3=150;
271:       int gd=DETECT,gm;
272:       int c2;
273:       float mat1[3][1],mat2[3][1],mat3[3][1];
274:       float new_pnt1[3][1],new_pnt2[3][1],new_pnt3[3][1];
275:       float dnew_pnt1[3][1],dnew_pnt2[3][1];
276:       float t_mat[3][3];
277:       initgraph(&gd,&gm,"");
278:       int vert[8];
279:  int n,i,x,y;
280:  printf("Enter the no. of vertices .. ? ");
281:  scanf("%d",&n);
282:  printf(" Select the vertices >>");
283:  initmouse();
284:  showmouse();
285:  for(i=0;i<n*2;i+=2)
286:       {
287:            getposOnClick(&x,&y);
288:            hidemouse();
289:            putpixel(x,y,10);
290:            showmouse();
291:            vert[i]=x;
292:            vert[i+1]=y;
293:       }
294:  hidemouse();
295:       // Transforming ....
296:       float tx,ty;
297:       float sx,sy;
298:       float angle;
299:       int choice;
300:       while(1)
301:       {
302:       cleardevice();
303:       dpoly(n,vert);
304:       gotoxy(3,24);
305:  printf(" 1.Translation | 2. Rotation | 3. Scaling | 4. Reflection | 5.Shear | 6. Exit  ");
306:       gotoxy(10,25);
307:  printf(" 13.Trans + Scale | 12. Trans + Rot | 23. Rot + Scale ? ");
308:       scanf("%d",&choice);
309:       switch(choice)
310:            {
311:            case 1:
312:            gotoxy(15,20);
313:            showmouse();
314:  printf(" Choose Point to translate >>\n");
315:            getposOnClick(&x,&y);
316:            tx=x-vert[0];
317:            ty=y-vert[1];
318:            translate(vert,n,tx,ty);
319:            break;
320:            case 2:
321:            gotoxy(15,20);
322:            printf(" Enter the Rotation ANGLE ? ");
323:            scanf(" %f",&angle);
324:            rotate(vert,n,angle);
325:            break;
326:            case 3:
327:            gotoxy(15,20);
328:            printf(" Enter the Scale Factor sx,sy ? ");
329:            scanf(" %f%f",&sx,&sy);
330:            scale(vert,n,sx,sy);
331:            break;
332:            case 12:
333:            gotoxy(15,20);
334:            printf(" Enter the translation Factor tx,ty ? ");
335:            scanf(" %f%f",&tx,&ty);
336:            translate(vert,n,tx,ty);
337:            printf(" Enter the Rotation ANGLE ? ");
338:            scanf(" %f",&angle);
339:            rotate(vert,n,angle);
340:            break;
341:            case 13:
342:            gotoxy(15,20);
343:            printf(" Enter the translation Factor tx,ty ? ");
344:            scanf(" %f%f",&tx,&ty);
345:             translate(vert,n,tx,ty);
346:            printf(" Enter the Scale Factor sx,sy ? ");
347:            scanf(" %f%f",&sx,&sy);
348:            scale(vert,n,sx,sy);
349:            break;
350:            case 23:
351:            gotoxy(15,20);
352:            printf(" Enter the Rotation ANGLE ? ");
353:            scanf(" %f",&angle);
354:            rotate(vert,n,angle);
355:            printf(" Enter the Scale Factor sx,sy ? ");
356:            scanf(" %f%f",&sx,&sy);
357:            scale(vert,n,sx,sy);
358:            break;
359:            case 4:
360:            int px=vert[0],py=vert[1];
361:            gotoxy(1,1);
362:  printf(" 1-wrt x axis | 2-wrt y axis .. | 3-General ?");
363:            scanf("%d",&c2);
364:            if(c2 ==1)
365:                 reflectx(vert,n);
366:            else if(c2==2)
367:                 reflecty(vert,n);
368:            else if(c2 ==3)
369:            {
370:                 rotate(vert,n,180);
371:                 tx=px-vert[0];
372:                 ty=py-vert[1];
373:                 translate(vert,n,tx,ty);
374:                }
375:            break;
376:            case 5:
377:            int s;
378:            gotoxy(1,1);
379:            printf(" 1-wrt x axis | 2-wrt y axis .. ?");
380:            scanf("%d",&c2);
381:            printf(" Enter the shearing factor .. ?");
382:            scanf("%d",&s);
383:            if(c2==1)
384:                 shearx(vert,n,s);
385:            else if(c2==2)
386:                 sheary(vert,n,s);
387:            break;
388:            case 6:
389:            exit(0);
390:            }
391:  gotoxy(10,21);
392:  hidemouse();
393:  dpoly(n,vert);
394:  showmouse();
395:  }
396:  }

Comments

Popular posts from this blog

Listing/Delisting of an article in to an Assortment in SAP SCM Retail

How To Install LEX and YACC in Linux or Ubuntu

How to Create Live Search In Html Using Ajax Ad PhP - Google Live Search / Instant Search Code And Example