C Graphics Program For 3D Transformation




C Graphics Program For 3D Transformation



Source Code:-




1:  //Program to implement 3D 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[4][1])
12:       {
13:       return ceil(mat[0][0]);
14:       }
15:  int gety(float mat[4][1])
16:       {
17:       return ceil(mat[1][0]);
18:       }
19:  int getz(float mat[4][1])
20:       {
21:       return ceil(mat[2][0]);
22:       }
23:  void create_mat_pnt(float mat[4][1],int x,int y,int z)
24:       {
25:       mat[0][0]=x;
26:       mat[1][0]=y;
27:       mat[2][0]=z;
28:       mat[3][0]=1;
29:       }
30:  void create_mat_tran(float mat[4][4],int tx,int ty,int tz)
31:       {
32:       int i,j;
33:       for(i=0;i<4;i++)
34:       for(j=0;j<4;j++)
35:            {
36:            if(i==j)
37:            {
38:            mat[i][j]=1.0;
39:            }
40:            else
41:            mat[i][j]=0;
42:            }
43:       mat[0][3]=tx;
44:       mat[1][3]=ty;
45:       mat[2][3]=tz;
46:       }
47:  void create_mat_shearx(float mat[4][4],int tx)
48:       {
49:       int i,j;
50:       for(i=0;i<3;i++)
51:       for(j=0;j<3;j++)
52:            {
53:            if(i==j)
54:            {
55:            mat[i][j]=1.0;
56:            }
57:            else
58:            mat[i][j]=0;
59:            }
60:       mat[0][1]=tx;
61:       }
62:  void create_mat_sheary(float mat[4][4],int ty)
63:       {
64:       int i,j;
65:       for(i=0;i<3;i++)
66:       for(j=0;j<3;j++)
67:            {
68:            if(i==j)
69:            {
70:            mat[i][j]=1.0;
71:            }
72:            else
73:            mat[i][j]=0;
74:            }
75:       mat[1][0]=ty;
76:       }
77:  void create_mat_reflectx(float mat[4][4])
78:       {
79:       int i,j;
80:       for(i=0;i<3;i++)
81:       for(j=0;j<3;j++)
82:            {
83:            if(i==j)
84:            {
85:            mat[i][j]=1.0;
86:            }
87:            else
88:            mat[i][j]=0;
89:            }
90:       mat[1][1]=-1;
91:       }
92:  void create_mat_reflecty(float mat[4][4])
93:       {
94:       int i,j;
95:       for(i=0;i<3;i++)
96:       for(j=0;j<3;j++)
97:            {
98:            if(i==j)
99:            {
100:            mat[i][j]=1.0;
101:            }
102:            else
103:            mat[i][j]=0;
104:            }
105:       mat[0][0]=-1;
106:       }
107:  void create_mat_rot(float mat[4][4],float angle)
108:       {
109:       int i,j;
110:       for(i=0;i<3;i++)
111:       for(j=0;j<3;j++)
112:            {
113:            if(i==j)
114:            mat[i][j]=1;
115:            else
116:            mat[i][j]=0;
117:            }
118:       mat[0][0]=cos(angle);
119:       mat[1][0]=sin(angle);
120:       mat[0][1]=-sin(angle);
121:       mat[1][1]=cos(angle);
122:       }
123:  void create_mat_scale(float mat[4][4],float sx,float sy)
124:       {
125:       int i,j;
126:       for(i=0;i<3;i++)
127:       for(j=0;j<3;j++)
128:            {
129:            if(i==j)
130:            mat[i][j]=1;
131:            else
132:            mat[i][j]=0;
133:            }
134:       mat[0][0]=sx;
135:       mat[1][1]=sy;
136:       }
137:  void display(float mat[4][4])
138:       {
139:       int i,j;
140:       printf("\n");
141:       for(i=0;i<4;i++)
142:            {for(j=0;j<4;j++)
143:                 printf("\t%f",mat[i][j]);
144:            printf("\n");
145:            }
146:       }
147:  void display(float mat[4][1])
148:       {
149:       int i,j;
150:       printf("\n");
151:       for(i=0;i<3;i++)
152:            {for(j=0;j<1;j++)
153:                 printf("\t%f",mat[i][j]);
154:            printf("\n");
155:            }
156:       }
157:  void add(int mat1[4][4],int mat2[4][4],int res[4][4])
158:       {
159:       int i,j;
160:       for(i=0;i<4;i++)
161:            {for(j=0;j<4;j++)
162:            res[i][j]=mat1[i][j]+mat2[i][j];
163:            }
164:       }
165:  void mul(float mat1[4][1],float mat2[4][4],float res[4][1])
166:       {
167:       int i,j,k;
168:       for(i=0;i<4;i++)
169:       for(j=0;j<1;j++)
170:       {
171:       res[i][j]=0;
172:       for(k=0;k<4;k++)
173:            res[i][j]+=mat2[i][k]*mat1[k][j];
174:            }
175:       }
176:  void translate(int poly[],int n ,int tx,int ty,int tz=0)
177:  {
178:  float t_mat[4][4];
179:  float mat1[4][1];
180:  float res[4][1];
181:  for(int i=0;i<n*3;i+=3)
182:       {
183:       create_mat_pnt(mat1,poly[i],poly[i+1],poly[i+2]);
184:       create_mat_tran(t_mat,tx,ty,tz);
185:       mul(mat1,t_mat,res);
186:       poly[i]=getx(res);
187:       poly[i+1]=gety(res);
188:       poly[i+2]=getz(res);
189:       }
190:  }
191:  void shearx(int poly[],int n ,int tx)
192:  {
193:  float t_mat[4][4];
194:  float mat1[4][1];
195:  float res[4][1];
196:  for(int i=0;i<n*2;i+=2)
197:       {
198:       create_mat_shearx(t_mat,tx);
199:       mul(mat1,t_mat,res);
200:       poly[i]=getx(res);
201:       poly[i+1]=gety(res);
202:       }
203:  }
204:  void sheary(int poly[],int n ,int tx)
205:  {
206:  float t_mat[4][4];
207:  float mat1[4][1];
208:  float res[4][1];
209:  for(int i=0;i<n*2;i+=2)
210:       {
211:       create_mat_sheary(t_mat,tx);
212:       mul(mat1,t_mat,res);
213:       poly[i]=getx(res);
214:       poly[i+1]=gety(res);
215:       }
216:  }
217:  void reflectx(int poly[],int n )
218:  {
219:  float t_mat[4][4];
220:  float mat1[4][1];
221:  float res[4][1];
222:  for(int i=0;i<n*2;i+=2)
223:       {
224:       create_mat_reflectx(t_mat);
225:       mul(mat1,t_mat,res);
226:       poly[i]=getx(res);
227:       poly[i+1]=gety(res);
228:       }
229:  }
230:  void reflecty(int poly[],int n )
231:  {
232:  float t_mat[4][4];
233:  float mat1[4][1];
234:  float res[4][1];
235:  for(int i=0;i<n*2;i+=2)
236:       {
237:       create_mat_reflecty(t_mat);
238:       mul(mat1,t_mat,res);
239:       poly[i]=getx(res);
240:       poly[i+1]=gety(res);
241:       }
242:  }
243:  void scale(int poly[],int n ,float sx,float sy)
244:  {
245:  float r_mat[4][4];
246:  float mat1[4][1];
247:  float res[4][1];
248:  for(int i=0;i<n*2;i+=2)
249:       {
250:       create_mat_scale(r_mat,sx,sy);
251:       mul(mat1,r_mat,res);
252:       poly[i]=getx(res);
253:       poly[i+1]=gety(res);
254:       }
255:  }
256:  void rotate(int poly[],int n ,float angle)
257:  {
258:  float r_mat[4][4];
259:  float mat1[4][1];
260:  float res[4][1];
261:  for(int i=0;i<n*2;i+=2)
262:       {
263:       create_mat_rot(r_mat,angle*.0174);
264:       mul(mat1,r_mat,res);
265:       poly[i]=getx(res);
266:       poly[i+1]=gety(res);
267:       }
268:  }
269:  int vert[100],vert2[100];
270:  int n,i,x,y;
271:  void term(int &x,int &y,int z)
272:       {
273:       float c=1000;
274:       float z1=c-z;
275:       x=(x*c)/z1;
276:       y=(y*c)/z1;
277:       }
278:  tpoly(int sides,int vert[],int c=15)
279:       {
280:       int i=0;
281:       int max=(sides*3)-1;
282:       setcolor(10);
283:       for(i=0;i<n*3;i+=3)
284:       {
285:       term(vert[i],vert[i+1],vert[i+2]);
286:       }
287:       bshmLine(*vert,*(vert+1),*(vert+max-2),*(vert+max-1),c);
288:       for(i=0;i<max-2;i+=3)
289:            {
290:       setcolor(10);
291:       bshmLine(*(vert+i),*(vert+1+i),*(vert+3+i),*(vert+4+i),c);
292:            }
293:       }
294:  draw_3d()
295:       {
296:  hidemouse();
297:  tpoly(n,vert);
298:  for(int i=0;i<n*3;i+=3)
299:       {
300:       vert2[i]=vert[i];
301:       vert2[i+1]=vert[i+1];
302:       vert2[i+2]=vert[i+2]+300;
303:       }
304:  tpoly(n,vert2,10);
305:       for(i=0;i<n*3;i+=3)
306:       {
307:       bshmLine(*(vert+i),*(vert+1+i),*(vert2+i),*(vert2+1+i));
308:       }
309:  showmouse();
310:       }
311:  scale_to(float f)
312:       {
313:       for(i=0;i<n*3;i+=3)
314:            {
315:            vert[i]=vert[i]*f;
316:            vert[i+1]=vert[i+1]*f;
317:            }
318:       }
319:  rotate_with_z(float an)
320:       {
321:       an*=.0174;
322:       for(i=0;i<n*3;i+=3)
323:            {
324:            gotoxy(1,1);
325:            vert[i]=(vert[i]*cos(an))-(vert[i+1]*sin(an));
326:            vert[i+1]=(vert[i]*sin(an))+(vert[i+1]*cos(an));
327:            vert2[i]=(vert2[i]*cos(an))-(vert2[i+1]*sin(an));
328:            vert2[i+1]=(vert2[i]*sin(an))+(vert2[i+1]*cos(an));
329:            }
330:       }
331:  rotate_with_x(float an)
332:       {
333:       an*=.0174;
334:       for(i=0;i<n*3;i+=3)
335:            {
336:            vert[i+1]=(vert[i+1]*cos(an))-(vert[i+2]*sin(an));
337:            vert[i+2]=(vert[i+1]*sin(an))+(vert[i+2]*cos(an));
338:            vert2[i+1]=(vert2[i+1]*cos(an))-(vert2[i+2]*sin(an));
339:            vert2[i+2]=(vert2[i+1]*sin(an))+(vert2[i+2]*cos(an));
340:            }
341:       }
342:  rotate_with_y(float an)
343:       {
344:       an*=.0174;
345:       for(i=0;i<n*3;i+=3)
346:            {
347:            vert[i+2]=(vert[i+2]*cos(an))-(vert[i]*sin(an));
348:            vert[i]=(vert[i+2]*sin(an))+(vert[i]*cos(an));
349:            vert2[i+2]=(vert2[i+2]*cos(an))-(vert2[i]*sin(an));
350:            vert2[i]=(vert2[i+2]*sin(an))+(vert2[i]*cos(an));
351:            }
352:       }
353:  move_to(int x,int y,int z=0)
354:       {
355:       for(i=0;i<n*3;i+=3)
356:            {
357:            vert[i]=vert[i]+x;
358:            vert2[i]=vert2[i]+x;
359:            vert[i+1]=vert[i+1]+y;
360:            vert2[i+1]=vert2[i+1]+y;
361:            vert[i+2]=vert[i+2]+z;
362:            vert2[i+2]=vert2[i+2]+z;
363:            }
364:       }
365:  void main()
366:       {
367:       int x1=300,y1=100,x2=400,y2=200,x3=400,y3=150;
368:       int gd=DETECT,gm;
369:       int c2;
370:       float mat1[4][1],mat2[4][1],mat3[4][1];
371:       float new_pnt1[4][1],new_pnt2[4][1],new_pnt3[4][1];
372:       float dnew_pnt1[4][1],dnew_pnt2[4][1];
373:       float t_mat[4][4];
374:       initgraph(&gd,&gm,"");
375:  printf("Enter the no. of vertices .. ? ");
376:  scanf("%d",&n);
377:  printf(" Select the vertices >>");
378:  initmouse();
379:  showmouse();
380:  int z=1;
381:  for(i=0;i<n*3;i+=3)
382:       {
383:       getposOnClick(&x,&y);
384:       hidemouse();
385:       putpixel(x,y,10);
386:       showmouse();
387:       vert[i]=x;
388:       vert[i+1]=y;
389:       vert[i+2]=z;
390:       }
391:  draw_3d();
392:  int c;
393:       hidemouse();
394:  while(( c=getch())!=27)
395:       {
396:            setfillstyle(1,0);
397:            bar(0,0,640,480);
398:  if(c == '6')
399:            {
400:  move_to(1,0,0);
401:  draw_3d();
402:            }
403:  if(c == '4')
404:            {
405:  move_to(-1,0,0);
406:  draw_3d();
407:       }
408:  if(c == '8')
409:       {
410:  move_to(0,-1,0);
411:  draw_3d();
412:       }
413:  if(c == '2')
414:       {
415:  move_to(0,1,0);
416:  draw_3d();
417:       }
418:  if(c == '3')
419:       {
420:  rotate_with_z(1);
421:  draw_3d();
422:       }
423:  if(c == '1')
424:       {
425:  rotate_with_z(-1);
426:  draw_3d();
427:       }
428:  if(c == '9')
429:       {
430:  rotate_with_y(1);
431:  draw_3d();
432:       }
433:  if(c == '7')
434:       {
435:  rotate_with_y(-1);
436:  draw_3d();
437:       }
438:  if(c == '0')
439:       {
440:  rotate_with_x(-1);
441:  draw_3d();
442:       }
443:  if(c == '.')
444:       {
445:  rotate_with_x(1);
446:  draw_3d();
447:       }
448:  if(c == '+')
449:       {
450:  scale_to(1.01);
451:  draw_3d();
452:       }
453:  if(c == '-')
454:       {
455:  scale_to(.99);
456:  draw_3d();
457:       }
458:  }
459:  }

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