letscheat1234

Graphics Extra

Nov 9th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. //Polygo using bresenham line
  2. #include<stdio.h>
  3. #include<graphics.h>
  4. #include<math.h>
  5. #define ROUND(a)((int)(a+0.5))
  6. struct point
  7. {
  8. int x,y;
  9. }pol[10];
  10. int n;
  11. void DDA(int,int,int,int);
  12. void Bresenham(int,int,int,int);
  13. void readpoly()
  14. {
  15. int i;
  16. for(i=0;i<n;i++)
  17. {
  18. printf("\nEnter the %d coordinates ",i+1);
  19. scanf("%d%d",&pol[i].x,&pol[i].y);
  20. }
  21. }
  22. void drawpolygonDDA()
  23. {
  24. int i;
  25. for(i=0;i<n;i++)
  26. DDA(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y);
  27. }
  28. void drawpolygonBres()
  29. {
  30. int i;
  31. for(i=0;i<n;i++)
  32. {
  33. delay(500);
  34. Bresenham(pol[i].x,pol[i].y,pol[(i+1)%n].x,pol[(i+1)%n].y);
  35. }
  36. }
  37. int main()
  38. {
  39. int xa,ya,xb,yb,choice,gd=DETECT,gm=VGAMAX;
  40. printf("\nEnter the no of sides ");
  41. scanf("%d",&n);
  42. readpoly();
  43. initgraph(&gd,&gm,NULL);
  44. printf("Press any key to draw traingle using Bresenham line
  45. drawing algortihm over the triangle drawn using
  46. DDA line drawing algorithm");
  47. drawpolygonDDA();
  48. getch();
  49. drawpolygonBres();
  50. while (!kbhit());
  51. closegraph();
  52. return 0;
  53. }
  54. void DDA(int xa,int ya,int xb,int yb)
  55. {
  56. int dx,dy,steps,k;
  57. dx=xb-xa;
  58. dy=yb-ya;
  59. float x=xa,y=ya,xinc,yinc;
  60. if(abs(dx)>abs(dy))
  61. steps=abs(dx);
  62. else
  63. steps=abs(dy);
  64. xinc=dx/(float)steps;
  65. yinc=dy/(float)steps;
  66. putpixel(ROUND(x),ROUND(y),5);
  67. for(k=0;k<steps;k++)
  68. {
  69. x+=xinc;
  70. y+=yinc;
  71. putpixel(ROUND(x),ROUND(y),5);
  72. }
  73. }
  74. void Bresenham(int xa,int ya,int xb,int yb)
  75. {
  76. int i,dx=xb-xa,dy=yb-ya,p,twod,twodd,s;
  77. float m=2;
  78. if(dx!=0)
  79. m=(float)dy/(float)dx;
  80. if(m>=-1&&m<=1)
  81. {
  82. if(xa>xb)
  83. {
  84. Bresenham(xb,yb,xa,ya);
  85. return;
  86. }
  87. p=2*dx-dy;
  88. if(dy<0)
  89. {
  90. s=-1;
  91. dy=-dy;
  92. }
  93. else
  94. s=1;
  95. putpixel(xa,ya,10);
  96. for(i=0;i<dy;i++)
  97. {
  98. while(p>0)
  99. {
  100. xa++;
  101. p-=2*dy;
  102. putpixel(xa,ya,10);
  103. }
  104. ya+=s;
  105. p+=2*dx;
  106. putpixel(xa,ya,10);
  107. }
  108. }
  109. else
  110. {
  111. if(ya>yb)
  112. {
  113. Bresenham(xb,yb,xa,ya);
  114. return;
  115. }
  116. p=2*dy-dx;
  117. if(dx<0)
  118. {
  119. s=-1;
  120. dx=-dx;
  121. }
  122. else
  123. s=1;
  124. putpixel(xa,ya,10);
  125. for(i=0;i<dx;i++)
  126. {
  127. while(p>0)
  128. {
  129. ya++;
  130. p-=2*dx;
  131. putpixel(xa,ya,10);
  132. }
  133. xa+=s;
  134. p+=2*dy;
  135. putpixel(xa,ya,10);
  136. }
  137. }
  138. }
  139.  
  140. //Different shapes using flood fill
  141. # include <iostream.h>
  142. # include <graphics.h>
  143. # include <conio.h>
  144. # include <math.h>
  145.  
  146. void show_screen( );
  147.  
  148. void Flood_fill(constint,constint,constint,constint);
  149.  
  150. void Circle(constint,constint,constint);
  151. void Triangle(constint,constint,constint,constint,constint,constint);
  152. void Rectangle(constint,constint,constint,constint);
  153. void Polygon(constint,constint []);
  154. void Line(constint,constint,constint,constint);
  155.  
  156. int main( )
  157. {
  158. int driver=VGA;
  159. int mode=VGAHI;
  160.  
  161. initgraph(&driver,&mode,"..\\Bgi");
  162.  
  163. show_screen( );
  164.  
  165. setcolor(15);
  166. Circle(175,175,40);
  167.  
  168. Flood_fill(175,175,10,0);
  169.  
  170. setcolor(15);
  171. settextstyle(0,0,1);
  172. outtextxy(150,225,"Circle");
  173.  
  174. setcolor(15);
  175. Rectangle(375,145,475,205);
  176.  
  177. Flood_fill(400,175,9,0);
  178.  
  179. setcolor(15);
  180. settextstyle(0,0,1);
  181. outtextxy(390,215,"Rectangle");
  182.  
  183. setcolor(15);
  184. Triangle(135,360,215,360,175,290);
  185.  
  186. Flood_fill(175,325,8,0);
  187.  
  188. setcolor(15);
  189. settextstyle(0,0,1);
  190. outtextxy(145,370,"Triangle");
  191.  
  192. int polygon_points[14]={ 365,325, 400,290, 450,290, 485,325,
  193. 450,360, 400,360, 365,325 };
  194.  
  195. setcolor(15);
  196. Polygon(7,polygon_points);
  197.  
  198. Flood_fill(425,325,12,0);
  199.  
  200. setcolor(15);
  201. settextstyle(0,0,1);
  202. outtextxy(395,370,"Polygon");
  203.  
  204. getch( );
  205. return 0;
  206. }
  207.  
  208. /*************************************************************************///--------------------------- Flood_fill( ) ---------------------------///*************************************************************************/void Flood_fill(constint x,constint y,
  209. constint fill_color,constint old_color)
  210. {
  211. if(getpixel(x,y)==old_color)
  212. {
  213. putpixel(x,y,fill_color);
  214.  
  215. Flood_fill((x+1),y,fill_color,old_color);
  216. Flood_fill((x-1),y,fill_color,old_color);
  217. Flood_fill(x,(y+1),fill_color,old_color);
  218. Flood_fill(x,(y-1),fill_color,old_color);
  219. }
  220. }
  221.  
  222. /*************************************************************************///------------------------------ Circle( ) ----------------------------///*************************************************************************/void Circle(constint h,constint k,constint r)
  223. {
  224. int color=getcolor( );
  225.  
  226. int x=0;
  227. int y=r;
  228. int p=(1-r);
  229.  
  230. do
  231. {
  232. putpixel((h+x),(k+y),color);
  233. putpixel((h+y),(k+x),color);
  234. putpixel((h+y),(k-x),color);
  235. putpixel((h+x),(k-y),color);
  236. putpixel((h-x),(k-y),color);
  237. putpixel((h-y),(k-x),color);
  238. putpixel((h-y),(k+x),color);
  239. putpixel((h-x),(k+y),color);
  240.  
  241. x++;
  242.  
  243. if(p<0)
  244. p+=((2*x)+1);
  245.  
  246. else
  247. {
  248. y--;
  249. p+=((2*(x-y))+1);
  250. }
  251. }
  252. while(x<=y);
  253. }
  254.  
  255. /*************************************************************************///---------------------------- Triangle( ) ----------------------------///*************************************************************************/void Triangle(constint x_1,constint y_1,constint x_2,constint y_2,
  256. constint x_3,constint y_3)
  257. {
  258. Line(x_1,y_1,x_2,y_2);
  259. Line(x_2,y_2,x_3,y_3);
  260. Line(x_3,y_3,x_1,y_1);
  261. }
  262.  
  263. /*************************************************************************///--------------------------- Rectangle( ) ----------------------------///*************************************************************************/void Rectangle(constint x_1,constint y_1,constint x_2,constint y_2)
  264. {
  265. Line(x_1,y_1,x_2,y_1);
  266. Line(x_2,y_1,x_2,y_2);
  267. Line(x_2,y_2,x_1,y_2);
  268. Line(x_1,y_2,x_1,y_1);
  269. }
  270.  
  271. /*************************************************************************///----------------------------- Polygon( ) ----------------------------///*************************************************************************/void Polygon(constint n,constint coordinates[])
  272. {
  273. if(n>=2)
  274. {
  275. Line(coordinates[0],coordinates[1],
  276. coordinates[2],coordinates[3]);
  277.  
  278. for(int count=1;count<(n-1);count++)
  279. Line(coordinates[(count*2)],coordinates[((count*2)+1)],
  280. coordinates[((count+1)*2)],
  281. coordinates[(((count+1)*2)+1)]);
  282. }
  283. }
  284.  
  285. /*************************************************************************///-------------------------------- Line( ) ----------------------------///*************************************************************************/void Line(constint x_1,constint y_1,constint x_2,constint y_2)
  286. {
  287. int color=getcolor( );
  288.  
  289. int x1=x_1;
  290. int y1=y_1;
  291.  
  292. int x2=x_2;
  293. int y2=y_2;
  294.  
  295. if(x_1>x_2)
  296. {
  297. x1=x_2;
  298. y1=y_2;
  299.  
  300. x2=x_1;
  301. y2=y_1;
  302. }
  303.  
  304. int dx=abs(x2-x1);
  305. int dy=abs(y2-y1);
  306. int inc_dec=((y2>=y1)?1:-1);
  307.  
  308. if(dx>dy)
  309. {
  310. int two_dy=(2*dy);
  311. int two_dy_dx=(2*(dy-dx));
  312. int p=((2*dy)-dx);
  313.  
  314. int x=x1;
  315. int y=y1;
  316.  
  317. putpixel(x,y,color);
  318.  
  319. while(x<x2)
  320. {
  321. x++;
  322.  
  323. if(p<0)
  324. p+=two_dy;
  325.  
  326. else
  327. {
  328. y+=inc_dec;
  329. p+=two_dy_dx;
  330. }
  331.  
  332. putpixel(x,y,color);
  333. }
  334. }
  335.  
  336. else
  337. {
  338. int two_dx=(2*dx);
  339. int two_dx_dy=(2*(dx-dy));
  340. int p=((2*dx)-dy);
  341.  
  342. int x=x1;
  343. int y=y1;
  344.  
  345. putpixel(x,y,color);
  346.  
  347. while(y!=y2)
  348. {
  349. y+=inc_dec;
  350.  
  351. if(p<0)
  352. p+=two_dx;
  353.  
  354. else
  355. {
  356. x++;
  357. p+=two_dx_dy;
  358. }
  359.  
  360. putpixel(x,y,color);
  361. }
  362. }
  363. }
  364.  
  365. /*************************************************************************///-------------------------- show_screen( ) ---------------------------///*************************************************************************/void show_screen( )
  366. {
  367. setfillstyle(1,1);
  368. bar(230,26,405,38);
  369.  
  370. settextstyle(0,0,1);
  371. setcolor(15);
  372. outtextxy(5,5,"******************************************************************************");
  373. outtextxy(5,17,"*-**************************************************************************-*");
  374. outtextxy(5,29,"*-------------------------- --------------------------*");
  375. outtextxy(5,41,"*-**************************************************************************-*");
  376. outtextxy(5,53,"*-**************************************************************************-*");
  377.  
  378. setcolor(11);
  379. outtextxy(237,29,"Flood Fill Algorithm");
  380.  
  381. setcolor(15);
  382.  
  383. for(int count=0;count<=30;count++)
  384. outtextxy(5,(65+(count*12)),"*-* *-*");
  385.  
  386. outtextxy(5,438,"*-**************************************************************************-*");
  387. outtextxy(5,450,"*------------------------- -------------------------*");
  388. outtextxy(5,462,"******************************************************************************");
  389.  
  390. setcolor(12);
  391. outtextxy(229,450,"Press any Key to exit.");
  392. }
Add Comment
Please, Sign In to add comment