Guest User

Untitled

a guest
Jan 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. /*
  2. Title: 2D Transformation
  3. Description: C Program to draw any shape perform 2D Transformations like Translation, Scaling, Rotation Reflection & Shear on that shape.
  4. Author: Saideep Dicholkar
  5. */
  6.  
  7. #include<stdio.h>
  8. #include<conio.h>
  9. #include<graphics.h>
  10. #include<math.h>
  11. int x=330,y=250;
  12. int a[4][2]={{50,50},{100,50},{100,100},{50,100}};
  13. void draw(int x,int y)
  14. {
  15. setcolor(WHITE);
  16. line(x,y-250,x,y+250);
  17. line(x-330,y,x+330,y);
  18. }
  19. void stat(int x,int y)
  20. {
  21. line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
  22. line(x+a[1][0],y-a[1][1],x+a[2][0],y-a[2][1]);
  23. line(x+a[2][0],y-a[2][1],x+a[3][0],y-a[3][1]);
  24. line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
  25. }
  26. void translate(int x,int y)
  27. {
  28. int tx,ty,xt,yt;
  29. cleardevice();
  30. printf("\nEnter translation factor (tx,ty): ");
  31. scanf("%d%d",&tx,&ty);
  32. cleardevice();
  33. draw(x,y);
  34. setcolor(WHITE);
  35. stat(x,y);
  36. xt=x+tx;
  37. yt=y+ty;
  38. getch();
  39. setcolor(GREEN);
  40. stat(xt,yt);
  41. }
  42. void rotate(int x,int y)
  43. {
  44. double d,x2r,y2r,x3r,y3r,x4r,y4r;
  45. cleardevice();
  46. printf("\nEnter Angle of Rotation: ");
  47. scanf("%lf",&d);
  48. cleardevice();
  49. draw(x,y);
  50. setcolor(WHITE);
  51. stat(x,y);
  52. d=(d*3.14)/180;
  53. x2r=a[0][0]+(((a[1][0]-a[0][0])*cos(d))-((a[1][1]-a[0][1])*sin(d)));
  54. y2r=a[0][1]+(((a[1][0]-a[0][0])*sin(d))+((a[1][1]-a[0][1])*cos(d)));
  55. x3r=a[0][0]+(((a[2][0]-a[0][0])*cos(d))-((a[2][1]-a[0][1])*sin(d)));
  56. y3r=a[0][1]+(((a[2][0]-a[0][0])*sin(d))+((a[2][1]-a[0][1])*cos(d)));
  57. x4r=a[0][0]+(((a[3][0]-a[0][0])*cos(d))-((a[3][1]-a[0][1])*sin(d)));
  58. y4r=a[0][1]+(((a[3][0]-a[0][0])*sin(d))+((a[3][1]-a[0][1])*cos(d)));
  59. getch();
  60. setcolor(GREEN);
  61. line(x+a[0][0],y-a[0][1],x+x2r,y-y2r);
  62. line(x+x2r,y-y2r,x+x3r,y-y3r);
  63. line(x+x3r,y-y3r,x+x4r,y-y4r);
  64. line(x+x4r,y-y4r,x+a[0][0],y-a[0][1]);
  65. }
  66. void scale(int x,int y)
  67. {
  68. int sx,sy;
  69. cleardevice();
  70. printf("\nEnter scaling factor (sx,sy): ");
  71. scanf("%d%d",&sx,&sy);
  72. cleardevice();
  73. draw(x,y);
  74. setcolor(WHITE);
  75. stat(x,y);
  76. getch();
  77. setcolor(GREEN);
  78. line(x+a[0][0]*sx,y-a[0][1]*sy,x+a[1][0]*sx,y-a[1][1]*sy);
  79. line(x+a[1][0]*sx,y-a[1][1]*sy,x+a[2][0]*sx,y-a[2][1]*sy);
  80. line(x+a[2][0]*sx,y-a[2][1]*sy,x+a[3][0]*sx,y-a[3][1]*sy);
  81. line(x+a[3][0]*sx,y-a[3][1]*sy,x+a[0][0]*sx,y-a[0][1]*sy);
  82. }
  83. void reflect(int x,int y)
  84. {
  85. int x1,y1,ch;
  86. printf("\n1.About X-axis\n2.About Y-axis:\nEnter choice: ");
  87. scanf("%d",&ch);
  88. cleardevice();
  89. draw(x,y);
  90. setcolor(WHITE);
  91. stat(x,y);
  92. getch();
  93. x1=-1;
  94. y1=1;
  95. setcolor(GREEN);
  96. if(ch==2)
  97. {
  98. line(x+a[0][0]*x1,y-a[0][1]*y1,x+a[1][0]*x1,y-a[1][1]*y1);
  99. line(x+a[1][0]*x1,y-a[1][1]*y1,x+a[2][0]*x1,y-a[2][1]*y1);
  100. line(x+a[2][0]*x1,y-a[2][1]*y1,x+a[3][0]*x1,y-a[3][1]*y1);
  101. line(x+a[3][0]*x1,y-a[3][1]*y1,x+a[0][0]*x1,y-a[0][1]*y1);
  102. }
  103. else
  104. {
  105. line(x-a[0][0]*x1,y+a[0][1]*y1,x-a[1][0]*x1,y+a[1][1]*y1);
  106. line(x-a[1][0]*x1,y+a[1][1]*y1,x-a[2][0]*x1,y+a[2][1]*y1);
  107. line(x-a[2][0]*x1,y+a[2][1]*y1,x-a[3][0]*x1,y+a[3][1]*y1);
  108. line(x-a[3][0]*x1,y+a[3][1]*y1,x-a[0][0]*x1,y+a[0][1]*y1);
  109. }
  110. }
  111. void shear(int x,int y)
  112. {
  113. int shr,sh1,sh2,shear;
  114. cleardevice();
  115. printf("\n1: Shear w.r.t X-axis\n2: Shear w.r.t Y-axis");
  116. printf("\nEnter choice: ");
  117. scanf("%d",&shear);
  118. printf("\nEnter shear value: ");
  119. scanf("%d",&shr);
  120. cleardevice();
  121. draw(x,y);
  122. setcolor(WHITE);
  123. stat(x,y);
  124. getch();
  125. setcolor(GREEN);
  126. switch(shear)
  127. {
  128. case 1:
  129. {
  130. sh1=shr*a[2][1];
  131. sh2=shr*a[3][1];
  132. line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]);
  133. line(x+a[1][0],y-a[1][1],x+a[2][0]+sh1,y-a[2][1]);
  134. line(x+a[2][0]+sh1,y-a[2][1],x+a[3][0]+sh2,y-a[3][1]);
  135. line(x+a[3][0]+sh2,y-a[3][1],x+a[0][0],y-a[0][1]);
  136. break;
  137. }
  138. case 2:
  139. {
  140. sh1=shr*a[1][0];
  141. sh2=shr*a[2][0];
  142. line(x+a[0][0],y-a[0][1],x+a[1][0],y-a[1][1]-sh2);
  143. line(x+a[1][0],y-a[1][1]-sh2,x+a[2][0],y-a[2][1]-sh2);
  144. line(x+a[2][0],y-a[2][1]-sh2,x+a[3][0],y-a[3][1]);
  145. line(x+a[3][0],y-a[3][1],x+a[0][0],y-a[0][1]);
  146. break;
  147. }
  148. }
  149. }
  150. void main()
  151. {
  152. int ch;
  153. int gd=DETECT,gm;
  154. initgraph(&gd,&gm,"C:\\tc\\bgi");
  155. cleardevice();
  156. printf("\n1: TRANSLATION \n2: ROTATION \n3: SCALING \n4: REFLECTION \n5: SHEAR");
  157. printf("\nEnter choice: ");
  158. scanf("%d",&ch);
  159. switch(ch)
  160. {
  161. case 1: translate(x,y);
  162. break;
  163. case 2: rotate(x,y);
  164. break;
  165. case 3: scale(x,y);
  166. break;
  167. case 4: reflect(x,y);
  168. break;
  169. case 5: shear(x,y);
  170. break;
  171. default:printf("Wrong choice");
  172. break;
  173. }
  174. getch();
  175. closegraph();
  176. restorecrtmode();
  177. }
Add Comment
Please, Sign In to add comment