Bresenham_filledcircle.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Bresenham circle FILL algorithm. (centerx, centery, radius.)
  3. //
  4. // From the book - mathmatics for computer graphics and game programming.
  5. //
  6. #include "raylib.h"
  7. void circleBres(int xc, int yc, int r,Color col);
  8. void putpixelBres(int xc, int yc, int x, int y,Color col);
  9. int main(void)
  10. {
  11. // Initialization
  12. //--------------------------------------------------------------------------------------
  13. const int screenWidth = 800;
  14. const int screenHeight = 450;
  15. InitWindow(screenWidth, screenHeight, "raylib example.");
  16. SetTargetFPS(10); // Set our game to run at 60 frames-per-second
  17. //--------------------------------------------------------------------------------------
  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 i=0;i<20;i++){
  29. int x = GetRandomValue(0,screenWidth);
  30. int y = GetRandomValue(0,screenHeight);
  31. int r = GetRandomValue(10,100);
  32. circleBres(x,y,r,RED);
  33. }
  34. EndDrawing();
  35. //----------------------------------------------------------------------------------
  36. }
  37. // De-Initialization
  38. //--------------------------------------------------------------------------------------
  39. CloseWindow(); // Close window and OpenGL context
  40. //--------------------------------------------------------------------------------------
  41. return 0;
  42. }
  43. void circleBres(int xc, int yc, int r,Color col)
  44. {
  45. int x=0;
  46. int y=r;
  47. int p=1-r;
  48. while(x<=y){
  49. putpixelBres(xc,yc,x,y,col);
  50. if(p<=0){
  51. x=x+1;
  52. y=y;
  53. p=p+(x*2)+3;
  54. }else if(p>0)
  55. {
  56. x=x+1;
  57. y=y-1;
  58. p=p+(x*2)-(y*2)+5;
  59. }
  60. }
  61. }
  62. void putpixelBres(int xc, int yc, int x, int y,Color col)
  63. {
  64. DrawLine(xc+x, yc+y,xc-x, yc+y, col);
  65. DrawLine(xc-x, yc+y,xc-x, yc-y, col); //**
  66. DrawLine(xc+x, yc-y,xc+x, yc+y, col);
  67. DrawLine(xc+y, yc+x,xc-y, yc+x, col);
  68. DrawLine(xc+y, yc-x,xc-y, yc-x, col);
  69. }