shapes_recursive_tree.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - recursive tree
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Jopestpe (@jopestpe)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Jopestpe (@jopestpe)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h>
  19. #define RAYGUI_IMPLEMENTATION
  20. #include "raygui.h" // Required for GUI controls
  21. //----------------------------------------------------------------------------------
  22. // Types and Structures Definition
  23. //----------------------------------------------------------------------------------
  24. typedef struct {
  25. Vector2 start;
  26. Vector2 end;
  27. float angle;
  28. float length;
  29. } Branch;
  30. //------------------------------------------------------------------------------------
  31. // Program main entry point
  32. //------------------------------------------------------------------------------------
  33. int main(void)
  34. {
  35. // Initialization
  36. //--------------------------------------------------------------------------------------
  37. const int screenWidth = 800;
  38. const int screenHeight = 450;
  39. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - recursive tree");
  40. Vector2 start = { (screenWidth/2.0f) - 125.0f, (float)screenHeight };
  41. float angle = 40.0f;
  42. float thick = 1.0f;
  43. float treeDepth = 10.0f;
  44. float branchDecay = 0.66f;
  45. float length = 120.0f;
  46. bool bezier = false;
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. float theta = angle*DEG2RAD;
  55. int maxBranches = (int)(powf(2, floorf(treeDepth)));
  56. Branch branches[1030] = { 0 };
  57. int count = 0;
  58. Vector2 initialEnd = { start.x + length*sinf(0.0f), start.y - length*cosf(0.0f) };
  59. branches[count++] = (Branch){start, initialEnd, 0.0f, length};
  60. for (int i = 0; i < count; i++)
  61. {
  62. Branch branch = branches[i];
  63. if (branch.length < 2) continue;
  64. float nextLength = branch.length*branchDecay;
  65. if (count < maxBranches && nextLength >= 2)
  66. {
  67. Vector2 branchStart = branch.end;
  68. float angle1 = branch.angle + theta;
  69. Vector2 branchEnd1 = { branchStart.x + nextLength*sinf(angle1), branchStart.y - nextLength*cosf(angle1) };
  70. branches[count++] = (Branch){branchStart, branchEnd1, angle1, nextLength};
  71. float angle2 = branch.angle - theta;
  72. Vector2 branchEnd2 = { branchStart.x + nextLength*sinf(angle2), branchStart.y - nextLength*cosf(angle2) };
  73. branches[count++] = (Branch){branchStart, branchEnd2, angle2, nextLength};
  74. }
  75. }
  76. //----------------------------------------------------------------------------------
  77. // Draw
  78. //----------------------------------------------------------------------------------
  79. BeginDrawing();
  80. ClearBackground(RAYWHITE);
  81. for (int i = 0; i < count; i++)
  82. {
  83. Branch branch = branches[i];
  84. if (branch.length >= 2)
  85. {
  86. if (bezier) DrawLineBezier(branch.start, branch.end, thick, RED);
  87. else DrawLineEx(branch.start, branch.end, thick, RED);
  88. }
  89. }
  90. DrawLine(580, 0, 580, GetScreenHeight(), (Color){ 218, 218, 218, 255 });
  91. DrawRectangle(580, 0, GetScreenWidth(), GetScreenHeight(), (Color){ 232, 232, 232, 255 });
  92. // Draw GUI controls
  93. //------------------------------------------------------------------------------
  94. GuiSliderBar((Rectangle){ 640, 40, 120, 20}, "Angle", TextFormat("%.0f", angle), &angle, 0, 180);
  95. GuiSliderBar((Rectangle){ 640, 70, 120, 20 }, "Length", TextFormat("%.0f", length), &length, 12.0f, 240.0f);
  96. GuiSliderBar((Rectangle){ 640, 100, 120, 20}, "Decay", TextFormat("%.2f", branchDecay), &branchDecay, 0.1f, 0.78f);
  97. GuiSliderBar((Rectangle){ 640, 130, 120, 20 }, "Depth", TextFormat("%.0f", treeDepth), &treeDepth, 1.0f, 10.0f);
  98. GuiSliderBar((Rectangle){ 640, 160, 120, 20}, "Thick", TextFormat("%.0f", thick), &thick, 1, 8);
  99. GuiCheckBox((Rectangle){ 640, 190, 20, 20 }, "Bezier", &bezier);
  100. //------------------------------------------------------------------------------
  101. DrawFPS(10, 10);
  102. EndDrawing();
  103. //----------------------------------------------------------------------------------
  104. }
  105. // De-Initialization
  106. //--------------------------------------------------------------------------------------
  107. CloseWindow(); // Close window and OpenGL context
  108. //--------------------------------------------------------------------------------------
  109. return 0;
  110. }