Skip to content

Commit b99a6cc

Browse files
authored
[examples] Added shapes_recursive_tree (#5229)
* ADDED: example: shapes_recursive_tree * [examples] Added shapes_recursive_tree * [examples] shapes_recursive_tree: adjustments * Reduced tree depth from 12 to 10 * new shapes_recursive_tree.png * follow the conventions * follow the conventions 2
1 parent b067b0b commit b99a6cc

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

examples/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ SHAPES = \
559559
shapes/shapes_logo_raylib_anim \
560560
shapes/shapes_rectangle_advanced \
561561
shapes/shapes_rectangle_scaling \
562+
shapes/shapes_recursive_tree \
562563
shapes/shapes_ring_drawing \
563564
shapes/shapes_rounded_rectangle_drawing \
564565
shapes/shapes_splines_drawing \

examples/Makefile.Web

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ SHAPES = \
559559
shapes/shapes_logo_raylib_anim \
560560
shapes/shapes_rectangle_advanced \
561561
shapes/shapes_rectangle_scaling \
562+
shapes/shapes_recursive_tree \
562563
shapes/shapes_ring_drawing \
563564
shapes/shapes_rounded_rectangle_drawing \
564565
shapes/shapes_splines_drawing \
@@ -868,6 +869,9 @@ shapes/shapes_rectangle_advanced: shapes/shapes_rectangle_advanced.c
868869
shapes/shapes_rectangle_scaling: shapes/shapes_rectangle_scaling.c
869870
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
870871

872+
shapes/shapes_recursive_tree: shapes/shapes_recursive_tree.c
873+
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
874+
871875
shapes/shapes_ring_drawing: shapes/shapes_ring_drawing.c
872876
$(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM)
873877

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You may find it easier to use than other toolchains, especially when it comes to
1717
- `zig build [module]` to compile all examples for a module (e.g. `zig build core`)
1818
- `zig build [example]` to compile _and run_ a particular example (e.g. `zig build core_basic_window`)
1919

20-
## EXAMPLES COLLECTION [TOTAL: 172]
20+
## EXAMPLES COLLECTION [TOTAL: 173]
2121

2222
### category: core [40]
2323

@@ -66,7 +66,7 @@ Examples using raylib[core](../src/rcore.c) platform functionality like window c
6666
| [core_undo_redo](core/core_undo_redo.c) | <img src="core/core_undo_redo.png" alt="core_undo_redo" width="80"> | ⭐⭐⭐☆ | 5.5 | 5.6 | [Ramon Santamaria](https://github.com/raysan5) |
6767
| [core_input_actions](core/core_input_actions.c) | <img src="core/core_input_actions.png" alt="core_input_actions" width="80"> | ⭐⭐☆☆ | 5.5 | 5.6 | [Jett](https://github.com/JettMonstersGoBoom) |
6868

69-
### category: shapes [23]
69+
### category: shapes [24]
7070

7171
Examples using raylib shapes drawing functionality, provided by raylib [shapes](../src/rshapes.c) module.
7272

@@ -84,6 +84,7 @@ Examples using raylib shapes drawing functionality, provided by raylib [shapes](
8484
| [shapes_following_eyes](shapes/shapes_following_eyes.c) | <img src="shapes/shapes_following_eyes.png" alt="shapes_following_eyes" width="80"> | ⭐⭐☆☆ | 2.5 | 2.5 | [Ramon Santamaria](https://github.com/raysan5) |
8585
| [shapes_easings_ball](shapes/shapes_easings_ball.c) | <img src="shapes/shapes_easings_ball.png" alt="shapes_easings_ball" width="80"> | ⭐⭐☆☆ | 2.5 | 2.5 | [Ramon Santamaria](https://github.com/raysan5) |
8686
| [shapes_easings_box](shapes/shapes_easings_box.c) | <img src="shapes/shapes_easings_box.png" alt="shapes_easings_box" width="80"> | ⭐⭐☆☆ | 2.5 | 2.5 | [Ramon Santamaria](https://github.com/raysan5) |
87+
| [shapes_recursive_tree](shapes/shapes_recursive_tree.c) | <img src="shapes/shapes_recursive_tree.png" alt="shapes_recursive_tree" width="80"> | ⭐⭐⭐☆ | 5.6-dev | 5.6-dev| [Jopestpe](https://github.com/jopestpe) |
8788
| [shapes_easings_rectangles](shapes/shapes_easings_rectangles.c) | <img src="shapes/shapes_easings_rectangles.png" alt="shapes_easings_rectangles" width="80"> | ⭐⭐⭐☆ | 2.0 | 2.5 | [Ramon Santamaria](https://github.com/raysan5) |
8889
| [shapes_ring_drawing](shapes/shapes_ring_drawing.c) | <img src="shapes/shapes_ring_drawing.png" alt="shapes_ring_drawing" width="80"> | ⭐⭐⭐☆ | 2.5 | 2.5 | [Vlad Adrian](https://github.com/demizdor) |
8990
| [shapes_circle_sector_drawing](shapes/shapes_circle_sector_drawing.c) | <img src="shapes/shapes_circle_sector_drawing.png" alt="shapes_circle_sector_drawing" width="80"> | ⭐⭐⭐☆ | 2.5 | 2.5 | [Vlad Adrian](https://github.com/demizdor) |

examples/examples_list.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ shapes;shapes_following_eyes;★★☆☆;2.5;2.5;2013;2025;"Ramon Santamaria";@
6060
shapes;shapes_easings_ball;★★☆☆;2.5;2.5;2014;2025;"Ramon Santamaria";@raysan5
6161
shapes;shapes_easings_box;★★☆☆;2.5;2.5;2014;2025;"Ramon Santamaria";@raysan5
6262
shapes;shapes_easings_rectangles;★★★☆;2.0;2.5;2014;2025;"Ramon Santamaria";@raysan5
63+
shapes;shapes_recursive_tree;★★★☆;5.6-dev;5.6-dev;2025;2025;"Jopestpe";@jopestpe
6364
shapes;shapes_ring_drawing;★★★☆;2.5;2.5;2018;2025;"Vlad Adrian";@demizdor
6465
shapes;shapes_circle_sector_drawing;★★★☆;2.5;2.5;2018;2025;"Vlad Adrian";@demizdor
6566
shapes;shapes_rounded_rectangle_drawing;★★★☆;2.5;2.5;2018;2025;"Vlad Adrian";@demizdor
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*******************************************************************************************
2+
*
3+
* raylib [shapes] example - shapes 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) 2018-2025 Jopestpe (@jopestpe)
15+
*
16+
********************************************************************************************/
17+
18+
#include "raylib.h"
19+
#include <math.h>
20+
21+
#define RAYGUI_IMPLEMENTATION
22+
#include "raygui.h" // Required for GUI controls
23+
24+
//----------------------------------------------------------------------------------
25+
// Types and Structures Definition
26+
//----------------------------------------------------------------------------------
27+
typedef struct {
28+
Vector2 start;
29+
Vector2 end;
30+
float angle;
31+
float length;
32+
} Branch;
33+
34+
//----------------------------------------------------------------------------------
35+
// Module Functions Declaration
36+
//----------------------------------------------------------------------------------
37+
static Vector2 CalculateBranchEnd(Vector2 start, float angle, float length);
38+
39+
//------------------------------------------------------------------------------------
40+
// Program main entry point
41+
//------------------------------------------------------------------------------------
42+
int main(void)
43+
{
44+
// Initialization
45+
//--------------------------------------------------------------------------------------
46+
const int screenWidth = 800;
47+
const int screenHeight = 450;
48+
49+
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - shapes recursive tree");
50+
51+
Vector2 start = { (screenWidth/2.0f) - 125.0f, screenHeight };
52+
float angle = 40.0f;
53+
float thick = 1.0f;
54+
float treeDepth = 10.0f;
55+
float branchDecay = 0.66f;
56+
float length = 120.0f;
57+
bool bezier = false;
58+
59+
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
60+
//--------------------------------------------------------------------------------------
61+
62+
// Main game loop
63+
while (!WindowShouldClose()) // Detect window close button or ESC key
64+
{
65+
// Update
66+
//----------------------------------------------------------------------------------
67+
68+
float theta = angle*DEG2RAD;
69+
int maxBranches = (int)(powf(2, (int)(treeDepth)));
70+
Branch branches[1024] = { 0 };
71+
int count = 0;
72+
73+
Vector2 initialEnd = CalculateBranchEnd(start, 0.0f, length);
74+
branches[count++] = (Branch){start, initialEnd, 0.0f, length};
75+
76+
for (int i = 0; i < count; i++)
77+
{
78+
Branch branch = branches[i];
79+
if (branch.length < 2) continue;
80+
81+
float nextLength = branch.length*branchDecay;
82+
83+
if (count < maxBranches && nextLength >= 2)
84+
{
85+
Vector2 branchStart = branch.end;
86+
87+
Vector2 branchEnd1 = CalculateBranchEnd(branchStart, branch.angle + theta, nextLength);
88+
Vector2 branchEnd2 = CalculateBranchEnd(branchStart, branch.angle - theta, nextLength);
89+
90+
branches[count++] = (Branch){branchStart, branchEnd1, branch.angle + theta, nextLength};
91+
branches[count++] = (Branch){branchStart, branchEnd2, branch.angle - theta, nextLength};
92+
}
93+
}
94+
95+
// Draw
96+
//----------------------------------------------------------------------------------
97+
BeginDrawing();
98+
99+
ClearBackground(RAYWHITE);
100+
101+
for (int i = 0; i < count; i++)
102+
{
103+
Branch branch = branches[i];
104+
if (branch.length >= 2)
105+
{
106+
if (!bezier) DrawLineEx(branch.start, branch.end, thick, RED);
107+
else DrawLineBezier(branch.start, branch.end, thick, RED);
108+
}
109+
}
110+
111+
DrawLine(580, 0, 580, GetScreenHeight(), (Color){ 218, 218, 218, 255 });
112+
DrawRectangle(580, 0, GetScreenWidth(), GetScreenHeight(), (Color){ 232, 232, 232, 255 });
113+
114+
// Draw GUI controls
115+
//------------------------------------------------------------------------------
116+
GuiSliderBar((Rectangle){ 640, 40, 120, 20}, "Angle", TextFormat("%.0f", angle), &angle, 0, 180);
117+
GuiSliderBar((Rectangle){ 640, 70, 120, 20 }, "Length", TextFormat("%.0f", length), &length, 12.0f, 240.0f);
118+
GuiSliderBar((Rectangle){ 640, 100, 120, 20}, "Decay", TextFormat("%.2f", branchDecay), &branchDecay, 0.1f, 0.78f);
119+
GuiSliderBar((Rectangle){ 640, 130, 120, 20 }, "Depth", TextFormat("%.0f", treeDepth), &treeDepth, 1.0f, 10.f);
120+
GuiSliderBar((Rectangle){ 640, 160, 120, 20}, "Thick", TextFormat("%.0f", thick), &thick, 1, 8);
121+
GuiCheckBox((Rectangle){ 640, 190, 20, 20 }, "Bezier", &bezier);
122+
//------------------------------------------------------------------------------
123+
124+
DrawFPS(10, 10);
125+
126+
EndDrawing();
127+
//----------------------------------------------------------------------------------
128+
}
129+
130+
// De-Initialization
131+
//--------------------------------------------------------------------------------------
132+
CloseWindow(); // Close window and OpenGL context
133+
//--------------------------------------------------------------------------------------
134+
135+
return 0;
136+
}
137+
138+
static Vector2 CalculateBranchEnd(Vector2 start, float angle, float length)
139+
{
140+
return (Vector2){ start.x + length*sinf(angle), start.y - length*cosf(angle) };
141+
}
17.9 KB
Loading

0 commit comments

Comments
 (0)