Example_-_Bresenham_oval_array.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "raylib.h"
  2. // width/height/centerx/centery
  3. static void midptellipse(int rx, int ry, int xc, int yc);
  4. static int map[200][200]={0};
  5. int main(void)
  6. {
  7. // Initialization
  8. //--------------------------------------------------------------------------------------
  9. const int screenWidth = 800;
  10. const int screenHeight = 450;
  11. InitWindow(screenWidth, screenHeight, "raylib example.");
  12. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  13. //--------------------------------------------------------------------------------------
  14. // width/height/centerx/centery
  15. midptellipse(10,7,30,30);
  16. // Main game loop
  17. while (!WindowShouldClose()) // Detect window close button or ESC key
  18. {
  19. // Update
  20. //----------------------------------------------------------------------------------
  21. midptellipse(GetRandomValue(5,10),GetRandomValue(5,10),GetRandomValue(5,100),GetRandomValue(5,100));
  22. //----------------------------------------------------------------------------------
  23. // Draw
  24. //----------------------------------------------------------------------------------
  25. BeginDrawing();
  26. ClearBackground(RAYWHITE);
  27. for(int y=0;y<100;y++){
  28. for(int x=0;x<100;x++){
  29. if(map[x][y]==1)DrawRectangle(x*3,y*3,3,3,BLACK);
  30. }
  31. }
  32. EndDrawing();
  33. //----------------------------------------------------------------------------------
  34. }
  35. // De-Initialization
  36. //--------------------------------------------------------------------------------------
  37. CloseWindow(); // Close window and OpenGL context
  38. //--------------------------------------------------------------------------------------
  39. return 0;
  40. }
  41. void midptellipse(int rx, int ry,
  42. int xc, int yc)
  43. {
  44. float dx, dy, d1, d2, x, y;
  45. x = 0;
  46. y = ry;
  47. // Initial decision parameter of region 1
  48. d1 = (ry * ry) - (rx * rx * ry) +
  49. (0.25 * rx * rx);
  50. dx = 2 * ry * ry * x;
  51. dy = 2 * rx * rx * y;
  52. // For region 1
  53. while (dx < dy)
  54. {
  55. // Print points based on 4-way symmetry
  56. int tx[4] = {x+xc,-x+xc,x+xc,-x+xc};
  57. int ty[4] = {y+yc,y+yc,-y+yc,-y+yc};
  58. for(int i=0;i<4;i++){
  59. if(tx[i]>=0 && tx[i]<100 && ty[i]>=0 && ty[i]<100)map[tx[i]][ty[i]] = 1;
  60. }
  61. // Checking and updating value of
  62. // decision parameter based on algorithm
  63. if (d1 < 0)
  64. {
  65. x++;
  66. dx = dx + (2 * ry * ry);
  67. d1 = d1 + dx + (ry * ry);
  68. }
  69. else
  70. {
  71. x++;
  72. y--;
  73. dx = dx + (2 * ry * ry);
  74. dy = dy - (2 * rx * rx);
  75. d1 = d1 + dx - dy + (ry * ry);
  76. }
  77. }
  78. // Decision parameter of region 2
  79. d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
  80. ((rx * rx) * ((y - 1) * (y - 1))) -
  81. (rx * rx * ry * ry);
  82. // Plotting points of region 2
  83. while (y >= 0)
  84. {
  85. int tx[4] = {x+xc,-x+xc,x+xc,-x+xc};
  86. int ty[4] = {y+yc,y+yc,-y+yc,-y+yc};
  87. for(int i=0;i<4;i++){
  88. if(tx[i]>=0 && tx[i]<100 && ty[i]>=0 && ty[i]<100)map[tx[i]][ty[i]] = 1;
  89. }
  90. // Checking and updating parameter
  91. // value based on algorithm
  92. if (d2 > 0)
  93. {
  94. y--;
  95. dy = dy - (2 * rx * rx);
  96. d2 = d2 + (rx * rx) - dy;
  97. }
  98. else
  99. {
  100. y--;
  101. x++;
  102. dx = dx + (2 * ry * ry);
  103. dy = dy - (2 * rx * rx);
  104. d2 = d2 + dx - dy + (rx * rx);
  105. }
  106. }
  107. }