Functions_-_scanlinefill_bresenhamline.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "raylib.h"
  2. #define MAP_WIDTH 100
  3. #define MAP_HEIGHT 100
  4. int map[MAP_HEIGHT][MAP_WIDTH];
  5. void scanlinefill(int x, int y,int value);
  6. void mapline(int x1, int y1, int x2, int y2,int value);
  7. int main(void)
  8. {
  9. // Initialization
  10. //--------------------------------------------------------------------------------------
  11. const int screenWidth = 800;
  12. const int screenHeight = 450;
  13. InitWindow(screenWidth, screenHeight, "raylib example.");
  14. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  15. //--------------------------------------------------------------------------------------
  16. mapline(0,0,MAP_WIDTH,MAP_HEIGHT,1);
  17. scanlinefill(50,48,1);
  18. // Main game loop
  19. while (!WindowShouldClose()) // Detect window close button or ESC key
  20. {
  21. // Update
  22. //----------------------------------------------------------------------------------
  23. //----------------------------------------------------------------------------------
  24. // Draw
  25. //----------------------------------------------------------------------------------
  26. BeginDrawing();
  27. ClearBackground(RAYWHITE);
  28. for(int y=0;y<MAP_HEIGHT;y++){
  29. for(int x=0;x<MAP_WIDTH;x++){
  30. if(map[y][x]==1){
  31. DrawRectangle(x*2,y*2,2,2,RED);
  32. }
  33. }}
  34. EndDrawing();
  35. //----------------------------------------------------------------------------------
  36. }
  37. // De-Initialization
  38. //--------------------------------------------------------------------------------------
  39. CloseWindow(); // Close window and OpenGL context
  40. //--------------------------------------------------------------------------------------
  41. return 0;
  42. }
  43. //
  44. // scan line fill(flood) the map array if the value is 0 with value.
  45. //
  46. //
  47. void scanlinefill(int x, int y,int value){
  48. if(x<0 || x>=MAP_WIDTH || y<0 || y>=MAP_HEIGHT)return;
  49. if(map[y][x]==value)return;
  50. int x2=x;
  51. while(x2>-1 && map[y][x2]==0){
  52. map[y][x2]=value;
  53. scanlinefill(x2,y-1,value);
  54. scanlinefill(x2,y+1,value);
  55. x2--;
  56. }
  57. x2=x+1;
  58. while(x2<MAP_WIDTH && map[y][x2]==0){
  59. map[y][x2]=value;
  60. scanlinefill(x2,y-1,value);
  61. scanlinefill(x2,y+1,value);
  62. x2++;
  63. }
  64. }
  65. //
  66. // Bresenham line inside map[][] array.
  67. //
  68. void mapline(int x1, int y1, int x2, int y2,int value){
  69. int dx;
  70. int dy;
  71. int sx;
  72. int sy;
  73. int e;
  74. dx = abs(x2 - x1);
  75. sx = -1;
  76. if(x1 < x2)sx = 1;
  77. dy = abs(y2 - y1);
  78. sy = -1;
  79. if(y1 < y2)sy = 1;
  80. if(dx < dy){
  81. e = dx / 2;
  82. }else{
  83. e = dy / 2;
  84. }
  85. bool exitloop=false;
  86. while(exitloop == false){
  87. //SetColor 255,255,255
  88. //DrawPoint x1,y1
  89. if(x1>-1 && x1<MAP_WIDTH && y1>-1 && y1<MAP_HEIGHT)map[x1][y1]=value;
  90. if(x1 == x2){
  91. if(y1 == y2){
  92. exitloop = true;
  93. }
  94. }
  95. if(dx > dy){
  96. x1 += sx;
  97. e -= dy;
  98. if (e < 0){
  99. e += dx;
  100. y1 += sy;
  101. }
  102. }else{
  103. y1 += sy;
  104. e -= dx;
  105. if(e < 0){
  106. e += dy;
  107. x1 += sx;
  108. }
  109. }
  110. }
  111. }