bresenham_circle.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Bresenham circle 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);
  8. void putpixelBres(int xc, int yc, int x, int y);
  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(60); // 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. circleBres(100,100,100);
  29. EndDrawing();
  30. //----------------------------------------------------------------------------------
  31. }
  32. // De-Initialization
  33. //--------------------------------------------------------------------------------------
  34. CloseWindow(); // Close window and OpenGL context
  35. //--------------------------------------------------------------------------------------
  36. return 0;
  37. }
  38. void circleBres(int xc, int yc, int r)
  39. {
  40. int x=0;
  41. int y=r;
  42. int p=1-r;
  43. while(x<=y){
  44. putpixelBres(xc,yc,x,y);
  45. if(p<=0){
  46. x=x+1;
  47. y=y;
  48. p=p+(x*2)+3;
  49. }else if(p>0)
  50. {
  51. x=x+1;
  52. y=y-1;
  53. p=p+(x*2)-(y*2)+5;
  54. }
  55. }
  56. }
  57. void putpixelBres(int xc, int yc, int x, int y)
  58. {
  59. DrawPixel(xc+x, yc+y, RED);
  60. DrawPixel(xc-x, yc+y, RED);
  61. DrawPixel(xc+x, yc-y, RED);
  62. DrawPixel(xc-x, yc-y, RED);
  63. DrawPixel(xc+y, yc+x, RED);
  64. DrawPixel(xc-y, yc+x, RED);
  65. DrawPixel(xc+y, yc-x, RED);
  66. DrawPixel(xc-y, yc-x, RED);
  67. }