Skip to content

Commit 4256be5

Browse files
authored
Fix branch array size and remove extra function (#5281)
* Fix branch array size and remove extra function * Fix branch array size and remove extra function
1 parent 087aa1b commit 4256be5

1 file changed

Lines changed: 12 additions & 21 deletions

File tree

examples/shapes/shapes_recursive_tree.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ typedef struct {
3131
float length;
3232
} Branch;
3333

34-
//----------------------------------------------------------------------------------
35-
// Module Functions Declaration
36-
//----------------------------------------------------------------------------------
37-
static Vector2 CalculateBranchEnd(Vector2 start, float angle, float length);
38-
3934
//------------------------------------------------------------------------------------
4035
// Program main entry point
4136
//------------------------------------------------------------------------------------
@@ -64,13 +59,12 @@ int main(void)
6459
{
6560
// Update
6661
//----------------------------------------------------------------------------------
67-
6862
float theta = angle*DEG2RAD;
6963
int maxBranches = (int)(powf(2, floorf(treeDepth)));
70-
Branch branches[1024] = { 0 };
64+
Branch branches[1030] = { 0 };
7165
int count = 0;
7266

73-
Vector2 initialEnd = CalculateBranchEnd(start, 0.0f, length);
67+
Vector2 initialEnd = { start.x + length*sinf(0.0f), start.y - length*cosf(0.0f) };
7468
branches[count++] = (Branch){start, initialEnd, 0.0f, length};
7569

7670
for (int i = 0; i < count; i++)
@@ -84,14 +78,16 @@ int main(void)
8478
{
8579
Vector2 branchStart = branch.end;
8680

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};
81+
float angle1 = branch.angle + theta;
82+
Vector2 branchEnd1 = { branchStart.x + nextLength*sinf(angle1), branchStart.y - nextLength*cosf(angle1) };
83+
branches[count++] = (Branch){branchStart, branchEnd1, angle1, nextLength};
84+
85+
float angle2 = branch.angle - theta;
86+
Vector2 branchEnd2 = { branchStart.x + nextLength*sinf(angle2), branchStart.y - nextLength*cosf(angle2) };
87+
branches[count++] = (Branch){branchStart, branchEnd2, angle2, nextLength};
9288
}
9389
}
94-
90+
//----------------------------------------------------------------------------------
9591
// Draw
9692
//----------------------------------------------------------------------------------
9793
BeginDrawing();
@@ -103,8 +99,8 @@ int main(void)
10399
Branch branch = branches[i];
104100
if (branch.length >= 2)
105101
{
106-
if (!bezier) DrawLineEx(branch.start, branch.end, thick, RED);
107-
else DrawLineBezier(branch.start, branch.end, thick, RED);
102+
if (bezier) DrawLineBezier(branch.start, branch.end, thick, RED);
103+
else DrawLineEx(branch.start, branch.end, thick, RED);
108104
}
109105
}
110106

@@ -133,9 +129,4 @@ int main(void)
133129
//--------------------------------------------------------------------------------------
134130

135131
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) };
141132
}

0 commit comments

Comments
 (0)