Example_-_BruteForceCircleFill2.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Draw a Filled circle using brute force.
  3. //
  4. #include "raylib.h"
  5. void bruteforcecircle(int xc, int yc, int r,Color col);
  6. int main(void)
  7. {
  8. // Initialization
  9. //--------------------------------------------------------------------------------------
  10. const int screenWidth = 800;
  11. const int screenHeight = 450;
  12. InitWindow(screenWidth, screenHeight, "raylib example.");
  13. SetTargetFPS(10); // Set our game to run at 60 frames-per-second
  14. //--------------------------------------------------------------------------------------
  15. // Main game loop
  16. while (!WindowShouldClose()) // Detect window close button or ESC key
  17. {
  18. // Update
  19. //----------------------------------------------------------------------------------
  20. //----------------------------------------------------------------------------------
  21. // Draw
  22. //----------------------------------------------------------------------------------
  23. BeginDrawing();
  24. ClearBackground(RAYWHITE);
  25. for(int i=0;i<20;i++){
  26. int x = GetRandomValue(0,screenWidth);
  27. int y = GetRandomValue(0,screenHeight);
  28. int r = GetRandomValue(10,100);
  29. bruteforcecircle(x,y,r,RED);
  30. }
  31. EndDrawing();
  32. //----------------------------------------------------------------------------------
  33. }
  34. // De-Initialization
  35. //--------------------------------------------------------------------------------------
  36. CloseWindow(); // Close window and OpenGL context
  37. //--------------------------------------------------------------------------------------
  38. return 0;
  39. }
  40. void bruteforcecircle(int xc, int yc, int radius,Color col) {
  41. // for(int y=-radius; y<=radius; y++){
  42. // for(int x=-radius; x<=radius; x++){
  43. // if(x*x+y*y <= radius*radius)
  44. // DrawPixel(xc+x, yc+y,col);
  45. // }}
  46. // Single Loop version ; Said to be 2x speed than double loop method.
  47. int r2 = radius * radius;
  48. int area = r2 << 2;
  49. int rr = radius << 1;
  50. for (int i = 0; i < area; i++)
  51. {
  52. int tx = (i % rr) - radius;
  53. int ty = (i / rr) - radius;
  54. if (tx * tx + ty * ty <= r2)
  55. DrawPixel(xc + tx, yc + ty, col);
  56. }
  57. }