Задача: Заливка замкнутой области
Исходник: Заливка замкнутой области, язык: C++ [code #135, hits: 12458]
автор: - [добавлен: 17.05.2006]
  1. #include <graphics.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. // this function initializes graphics mode
  6. // it will work only if you're using Borland C++ compiler & BGI drivers
  7. // if you're using another compiler you should overwrite body of this function
  8. void init_gr(void)
  9. {
  10. /* request autodetection */
  11. int gdriver = DETECT, gmode, errorcode;
  12. /* initialize graphics mode */
  13. initgraph(&gdriver, &gmode, "");
  14. /* read result of initialization */
  15. errorcode = graphresult();
  16. if (errorcode != grOk) /* an error occurred */
  17. {
  18. printf("Graphics error: %s\n", grapherrormsg(errorcode));
  19. printf("Press any key to halt:");
  20. getch();
  21. exit(1); /* return with error code */
  22. }
  23. }
  24. // this function shuts graphics mode down
  25. // it will work only if you're using Borland C++ compiler & BGI drivers
  26. // if you're using another compiler you should overwrite body of this function
  27. void end_gr(void)
  28. {
  29. closegraph();
  30. }
  31. // this function puts pixel on the screen in (x,y) position using color 'color'
  32. // it will work only if you're using Borland C++ compiler & BGI drivers
  33. // if you're using another compiler you should overwrite body of this function
  34. void PutPixel(int x, int y, int color)
  35. {
  36. putpixel(x,y,color);
  37. }
  38. // this function gets color of pixel in (x,y) position
  39. // it will work only if you're using Borland C++ compiler & BGI drivers
  40. // if you're using another compiler you should overwrite body of this function
  41. int GetPixel(int x, int y)
  42. {
  43. return getpixel(x,y);
  44. }
  45. // this function gets maximum horizontal coordinate
  46. // it will work only if you're using Borland C++ compiler & BGI drivers
  47. // if you're using another compiler you should overwrite body of this function
  48. int GetMaxX(void)
  49. {
  50. return getmaxx();
  51. }
  52. // this function gets maximum vertical coordinate
  53. // it will work only if you're using Borland C++ compiler & BGI drivers
  54. // if you're using another compiler you should overwrite body of this function
  55. int GetMaxY(void)
  56. {
  57. return getmaxy();
  58. }
  59. void FloodFill(int x, int y,int fill_color, int border_color)
  60. {
  61. int x_left,x_right,YY,i;
  62. int XMAX,YMAX;
  63. XMAX=GetMaxX();
  64. YMAX=GetMaxY();
  65. // Light as many pixels as possible on line y
  66. // and determine x_left and x_right
  67. x_left=x_right=x;
  68. while (GetPixel(x_left,y)!=border_color && x_left>0)
  69. {
  70. PutPixel(x_left,y,fill_color);
  71. x_left--;
  72. }
  73. x_right++;
  74. while (GetPixel(x_right,y)!=border_color && x_right<XMAX)
  75. {
  76. PutPixel(x_right,y,fill_color);
  77. x_right++;
  78. }
  79. // Recursive calls of FloodFill for two remote points
  80. x=(x_right+x_left)>>1; //shifting means division by 2
  81. for(i=-1;i<=1;i+=2)
  82. {
  83. YY=y;
  84. while (GetPixel(x,YY)!=border_color && YY<YMAX && YY>0) YY+=i;
  85. YY=(y+YY)>>1;
  86. if (GetPixel(x,YY)!=border_color && GetPixel(x,YY)!=fill_color) FloodFill(x,YY,fill_color,border_color);
  87. }
  88. // Recursive calls for all "dark" (not filled) pixels next
  89. // to the line y (with x values between x_left and x_right
  90. for(YY=y-1;YY<=y+1;YY+=2)
  91. {
  92. x=x_left+1;
  93. while (x<x_right && YY>0 && YY<YMAX)
  94. {
  95. if (GetPixel(x,YY)!=border_color && GetPixel(x,YY)!=fill_color) FloodFill(x,YY,fill_color,border_color);
  96. x++;
  97. }
  98. }
  99. }
  100. int main(void)
  101. {
  102. // initializing graphics mode
  103. init_gr();
  104. // drawing area to fill
  105. moveto(520,90); lineto(520,190); lineto(470,240);
  106. lineto(520,290); lineto(520,390);
  107. lineto(320,315); lineto(120,390);
  108. lineto(120,290); lineto(170,240);
  109. lineto(120,190); lineto(120, 90);
  110. lineto(270,90); lineto(270,120);
  111. lineto(170,120); lineto(170,150);
  112. lineto(470,150); lineto(470,120);
  113. lineto(370,120); lineto(370,90); lineto(520,90);
  114. moveto(370,240); lineto(320,290); lineto(270,240);
  115. lineto(320,190); lineto(370,240);
  116. /* example */
  117. FloodFill(121,91,1,15);
  118. FloodFill(320,240,9,15);
  119. FloodFill(100,20,3,15);
  120. /* clean up */
  121. getch();
  122. end_gr();
  123. return 0;
  124. }
//////////////////////////////////////////////////////////////////////////////
//
// Filling closed area using given color
// (c) Johna Smith, 1996
//
// Method description:
// 1) Fill all pixels on the current line
// 2) Call FloodFill recursively for two remote points to reduce
// the depth of the recursion
// 3) Call FloodFill recursively for all points on two nearest
// horizontal lines
// ! This program is NOT optimized for best performance !
// To do so don't use PutPixel & GetPixel function -
// change bytes in video memory directly.
//
//////////////////////////////////////////////////////////////////////////////

+добавить реализацию