Facebook
From unknownman, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 127
  1. Theory :
  2. https://pastebin.pl/view/d9b1d994
  3.  
  4. // EXP 1 To implement DDA Algorithm for drawing a line segment between two given end
  5.  
  6. #include<stdio.h>
  7. #include<graphics.h>
  8. #include<math.h>
  9. void main()
  10. {
  11. float x,y,x1,y1,x2,y2,dx,dy,length;
  12. int i,gd,gm; clrscr();
  13. /* Read two end points of line --
  14. -------------------------------- */
  15. printf("Enter the value of x1
  16. :\t"); scanf("%f",&x1);
  17. printf("Enter the value of y1
  18. :\t"); scanf("%f",&y1);
  19. printf("Enter the value of x2
  20. :\t"); scanf("%f",&x2);
  21. printf("Enter the value of y2
  22. :\t"); scanf("%f",&y2);
  23. /* Initialise graphics mode ----------------------------------
  24. */
  25. detectgraph(&gd,&gm);
  26. initgraph(&gd,&gm,"\\tc\\BGI");
  27. dx=abs(x2-x1); dy=abs(y2-y1);
  28. if (dx >= dy)
  29. { length = dx; }
  30. else
  31. { length = dy; } dx =
  32. (x2-x1)/length; dy =
  33. (y2-y1)/length; x =
  34. x1 + 0.5; /* Factor
  35. 0.5 is added to
  36. round the values */
  37. y = y1 + 0.5; /*
  38. Factor 0.5 is added
  39. to round the values
  40. */ putpixel (x, y,
  41. 15); i = 1; /*
  42. Initialise loop
  43. counter */ while(i
  44. <= length)
  45. {
  46. x = x + dx;
  47. y = y + dy;putpixel (x, y, 15); i = i + 1; delay(100); /*Delay is purposely
  48. inserted to see observe the line drawing
  49. process*/
  50. }
  51. getch();
  52. closegraph();
  53. }
  54.  
  55. // To implement Bresenham’s line drawing algorithm for drawing a line segment between
  56. two given endpoints A (x1, y2) and B(x2, y2).
  57.  
  58. #include<stdio.h>
  59. #include<graphics.h>
  60. #include<math.h>
  61. #include<conio.h>
  62. void main()
  63. {
  64. float x,y,x1,y1,x2,y2,dx,dy,e;
  65. int i,gd,gm; clrscr();
  66. /* Read two end points of line --
  67. -------------------------------- */
  68. printf("Enter the value of x1
  69. :\t"); scanf("%f",&x1);
  70. printf("Enter the value of y1
  71. :\t"); scanf("%f",&y1);
  72. printf("Enter the value of x2
  73. :\t"); scanf("%f",&x2);
  74. printf("Enter the value of y2
  75. :\t"); scanf("%f",&y2);
  76. /* Initialise graphics mode ----------------------------------
  77. */
  78. detectgraph(&gd,&gm);
  79. initgraph(&gd,&gm,"c:\\tc\\BGI");
  80. dx=abs(x2-x1); dy=abs(y2-y1);
  81. /* Initialise starting point
  82. -----------------------------*/
  83. x = x1; y = y1; putpixel (x,
  84. y, 15) ;
  85. /* Initialise decision variable
  86. -------------------------------- */ e
  87. = 2 * dy-dx;
  88. i = 1; /* Initialise loop counter */ do
  89. { while(e >= 0)
  90. {
  91. y = y + 1; e =
  92. e - 2 * dx; }
  93. x = x + 1; e = e + 2
  94. * dy; i = i + 1; putpixel (x,
  95.  
  96. y, 15); }while( i <= dx);
  97. getch(); closegraph();
  98. }
  99.  
  100.  
  101. // To implement area filling algorithm.
  102.  
  103. #include<stdio.h>
  104. #include<graphics.h>
  105. #include<conio.h>
  106. void flood(int, int, int, int);
  107. void main()
  108. { int gd,gm;
  109. /* Initialise graphics mode---------------------------------- */
  110. detectgraph(&gd,&gm);
  111. initgraph(&gd,&gm,"\\tc\\bgi");
  112. rectangle(50,50,100,100)
  113. ; flood(55,55,4,15);
  114. getch();
  115. closegraph();
  116. }
  117. void flood(int seed_x,int seed_y,int foreground_col,int background_col)
  118. { if(getpixel(seed_x,seed_y)!= background_col && getpixel(seed_x,seed_y)!=
  119. foreground_col)
  120. { putpixel(seed_x,seed_y,foreground_col);
  121. flood(seed_x+1,seed_y,foreground_col,background_col);
  122. flood(seed_x-1,seed_y,foreground_col,background_col);
  123. flood(seed_x,seed_y+1,foreground_col,background_col);
  124. flood(seed_x,seed_y-1,foreground_col,background_col);
  125. flood(seed_x+1,seed_y+1,foreground_col,background_col);
  126. flood(seed_x-1,seed_y-1,foreground_col,background_col);
  127. flood(seed_x+1,seed_y-1,foreground_col,background_col);
  128. flood(seed_x-1,seed_y+1,foreground_col,background_col); }
  129. }
  130.  
  131. //To implement Bit Map method for given character generation
  132.  
  133. #include<stdio.h>
  134. #include<conio.h>
  135. #include<graphics.h>
  136. void main()
  137. { int gd=DETECT,gm,i,j; int
  138. a[20][20]=
  139. {{0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0},
  140. {0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0},
  141. {0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1},
  142. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
  143. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
  144. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0},
  145. {1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0},
  146. {0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0},
  147. {0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0},
  148. {0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0}};
  149. initgraph(&gd,&gm,"c:\\tc\\bgi");
  150. for(i=0;i<19;i++)
  151. { for(j=0;j<19;j++)
  152. {
  153. if(a[i][j]==1)
  154.  putpixel(100+j,200+i,WHITE);
  155. }
  156. }
  157. getch();
  158. }
  159.  
  160.  
  161. //: To apply the basic 2D transformations such as translation, Scaling, Rotation for a
  162. given 2D object
  163.  
  164. include<graphics.h>
  165. #include<conio.h>
  166. #include<math.h>
  167. #include<stdio.h>
  168. int main()
  169. {
  170. int i, x, y, tx, ty, sx, sy, angle=10, xmax, ymax, xmid, ymid, op;
  171. int gd,gm; float p1[10]={50,50,
  172.  100,50,
  173.  100,100,
  174.  50,100,
  175.  50,50,};
  176. int pi[10]; float
  177. b[3][3]={1,0,0,
  178.  0,1,0,
  179.  0,0,1}; int c[1][1]; float
  180. a[1][1]; printf("\nSelect the transformation
  181. : "); printf("\n1 : Traslation"); printf("\n2 :
  182. Rotation"); printf("\n3 : Scaling");
  183. printf("\n4 : Rotation about arbitrary
  184. point"); printf( "\nEnter the option : ");
  185. scanf("%d",&op); switch(op)
  186. {
  187. case 1:
  188. printf("\nEnter x traslation : ");
  189. scanf("%d",&tx);
  190. printf("\nEnter y traslation : ");
  191. scanf("%d",&ty);
  192. b[0][0] = 1;
  193. b[0][1] = 0;
  194. b[0][2] = 0;
  195. b[1][0] = 0;
  196. b[1][1] = 1;
  197. b[1][2] = 0;
  198. b[2][0] = tx;
  199. b[2][1] = ty;
  200. b[2][2] = 1;
  201. break;
  202. case 2:
  203. printf("\nEnter Rotation angle : ");
  204. scanf("%d",&angle);
  205. b[0][0] =cos(angle*3.142/180);
  206. b[0][1] =sin(angle*3.142/180);
  207. b[0][2] = 0;
  208. b[1][0] =-sin(angle*3.142/180);
  209. b[1][1] = cos(angle*3.142/180);
  210. b[1][2] = 0;
  211. b[2][0] = 0;
  212. b[2][1] = 0;
  213. b[2][2] = 1;
  214. break;
  215. case 3:
  216. printf("\nEnter x scaling : ");
  217. scanf("%d",&sx);
  218. printf("\nEnter y scaling : ");
  219. scanf("%d",&sy);
  220. b[0][0] = sx;
  221. b[0][1] = 0;
  222. b[0][2] = 0;
  223. b[1][0] = 0;
  224. b[1][1] = sy;
  225. b[1][2] = 0;
  226. b[2][0] = 0;
  227. b[2][1] = 0;
  228. b[2][2] = 1;
  229. break;
  230.  
  231. case 4:
  232. printf("\nEnter x coordinate of arbitrary point :
  233. "); scanf("%d",&x); printf("\nEnter y coordinate
  234. of arbitrary point : "); scanf("%d",&y);
  235. printf("\nEnter Rotation angle : ");
  236. scanf("%d",&angle);
  237. tx = x;
  238. ty = y;
  239. b[0][0]
  240. =cos(an
  241. gle*3.1
  242. 42/180)
  243. ; b[0][1]
  244. =sin(an
  245. gle*3.1
  246. 42/180)
  247. ; b[0][2]
  248. = 0;
  249. b[1][0] =-sin(angle*3.142/180);
  250. b[1][1] = cos(angle*3.142/180);
  251. b[1][2] = 0;
  252. b[2][0] = -tx* cos(angle*3.142/180) + ty*sin(angle*3.142/180)+tx;
  253. b[2][1] = -tx* sin(angle*3.142/180) - ty*cos(angle*3.142/180)+ty;
  254. b[2][2] = 1;
  255. } detectgraph(&gd,&gm);
  256. initgraph(&gd,&gm,"\\tc\\bgi"); // Initialize
  257. graphics
  258. xmax = getmaxx(); // Get maximum x
  259. coordinate
  260. ymax = getmaxy(); // Get maximum y
  261. coordinate
  262. xmid = xmax/2; // Get the center x
  263. coordinate
  264.  
  265. ymid = ymax/2;
  266. setcolor(1);
  267. // Get the center y
  268. coordinate
  269. line(xmid,0,xmid,ymax); // Draw y coordinate
  270. line(0, ymid, xmax, ymid); // Draw x coordinate
  271. setcolor(4); for
  272. (i=0; i<8;i=i+2)
  273. { line(p1[i]+xmid,ymid-p1[i+1],xmid+p1[i+2],ymid-p1[i+3]);
  274. }
  275. for(i=0;i<9;i=i+2)
  276. {
  277. a[0][0]=p1[i]; a[0][1]=p1[i+1]; c[0][0] =
  278. a[0][0]*b[0][0]+a[0][1]*b[1][0]+b[2][0]; c[0][1] =
  279. a[0][0]*b[0][1]+a[0][1]*b[1][1]+b[2][1];
  280. pi[i]=c[0][0]; pi[i+1]=c[0][1];
  281. }
  282. setcolor(15); for
  283. (i=0; i<8;i=i+2)
  284. { line(xmid+pi[i],ymid-pi[i+1],xmid+pi[i+2],ymid-pi[i+3]);
  285. }
  286. getch();
  287. closegraph();
  288. return 0;
  289. }
  290.  
  291. // To implement Bezier curve.
  292.  
  293. #include <stdio.h>
  294. #include <graphics.h>
  295. #include <conio.h>
  296. #include<stdio.h>
  297. #include<process.h>
  298. int gd,gm,maxx,maxy;
  299. float xxx[4][2];
  300. /* Function to draw line from relative position specified in array xxx-----------------------*/
  301. void line1(float x2,float y2)
  302. { line(xxx[0][0],xxx[0][1],x2,y2);
  303. xxx[0][0]=x2; xxx[0][1]=y2;
  304. }
  305. /* Bezier function-------------------- */
  306. void bezier(float xb,float yb,float xc,float yc,float xd,float yd,int n)
  307. { float xab,yab,xbc,ybc,xcd,ycd;
  308. float xabc,yabc,xbcd,ybcd;
  309. float xabcd,yabcd;
  310. if (n==0)
  311. {
  312. line1(xb,yb);
  313. line1(xc,yc);
  314. line1(xd,yd); }
  315. else
  316. { xab = (xxx[0][0]+xb)/2; yab =
  317. (xxx[0][1]+yb)/2; xbc = (xb+xc)/2; ybc =
  318. (yb+yc)/2; xcd = (xc+xd)/2; ycd =
  319. (yc+yd)/2; xabc = (xab+xbc)/2; yabc =
  320. (yab+ybc)/2; xbcd = (xbc+xcd)/2; ybcd =
  321. (ybc+ycd)/2; xabcd = (xabc+xbcd)/2;
  322. yabcd = (yabc+ybcd)/2; n=n-1;
  323. bezier(xab,yab,xabc,yabc,xabcd,yabcd,n);
  324. bezier(xbcd,ybcd,xcd,ycd,xd,yd,n);
  325. }
  326. }
  327. /* Function to initialise graphics----------------------------------- */ void
  328. igraph()
  329. { detectgraph(&gd,&gm);
  330. if(gd<0)
  331. { puts("CANNOT DETECT A GRAPHICS CARD");
  332. exit(1);
  333. }
  334. initgraph(&gd,&gm,"\\tc\\bgi");
  335. } void
  336. main()
  337. {
  338. int i;
  339. float temp1,temp2;
  340. igraph();
  341. /*Read two end points and two control points of the curve*/
  342. for(i=0;i<4;i++)
  343. {
  344. printf("Enter (x,y) coordinates of point%d : ",i+1);
  345. scanf("%f,%f",&temp1,&temp2);
  346. xxx[i][0] = temp1; xxx[i][1]
  347. = temp2;
  348. }
  349. bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8); getch();
  350. closegraph();
  351. }
  352.