This commit is contained in:
2025-12-04 13:31:30 +01:00
parent 525936c6e7
commit 96f4f51e69
2 changed files with 56 additions and 9 deletions

View File

@@ -32,8 +32,9 @@ private:
Vector2 start = {0};
std::list<DrawArgs> drawCalls;
void drawBranch();
void calculateBranch();
void drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness);
inline size_t getNumOfBranches(int dep);
inline Color getStartColor(DrawArgs &arg);
inline Color getEndColor(int dep, Color &start);

View File

@@ -4,6 +4,7 @@
#include <raylib.h>
#include <raymath.h>
#include <rlgl.h>
#define ITER_PER_FRAME 5000
@@ -40,7 +41,7 @@ void Tree::init(int size)
start.x = size / 2;
start.y = size;
calculateLevels(size);
//texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
// texBunny = LoadTexture("dot.png"); // bug add deinit to unload texutre
}
void Tree::draw(Dna *dna)
@@ -56,7 +57,7 @@ bool Tree::tick()
size_t i = 0;
while (!drawCalls.empty())
{
drawBranch();
calculateBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
@@ -68,7 +69,7 @@ bool Tree::tick()
// Private
void Tree::drawBranch()
void Tree::calculateBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
@@ -86,17 +87,14 @@ void Tree::drawBranch()
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
int size = Lerp(sizeStart, sizeEnd, i);
DrawCircleV(point, size, color);
//DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
// use
// DrawRectangleGradientEx
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
}
// add more branches to draw
@@ -200,3 +198,51 @@ inline float Tree::getAngleVar(DrawArgs &arg)
return angleVar;
}
void Tree::drawBranch(Vector2 startPoint, Vector2 endPoint, Color startColor, Color endColor, float startThickness, float endThickness)
{
// Calculate the direction vector from startPoint to endPoint
Vector2 direction = {endPoint.x - startPoint.x, endPoint.y - startPoint.y};
// Normalize the direction vector
float length = sqrtf(direction.x * direction.x + direction.y * direction.y);
if (length == 0)
length = 1; // Avoid division by zero
Vector2 normalizedDir = {direction.x / length, direction.y / length};
// Calculate the perpendicular vector (rotate 90 degrees)
Vector2 perpendicular = {-normalizedDir.y, normalizedDir.x};
// Calculate the four vertices of the quadrilateral
Vector2 topLeft = {
startPoint.x + perpendicular.x * startThickness,
startPoint.y + perpendicular.y * startThickness};
Vector2 topRight = {
endPoint.x + perpendicular.x * endThickness,
endPoint.y + perpendicular.y * endThickness};
Vector2 bottomLeft = {
startPoint.x - perpendicular.x * startThickness,
startPoint.y - perpendicular.y * startThickness};
Vector2 bottomRight = {
endPoint.x - perpendicular.x * endThickness,
endPoint.y - perpendicular.y * endThickness};
// Draw the two triangles to form the quadrilateral
rlBegin(RL_TRIANGLES);
// First triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(topLeft.x, topLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
// Second triangle
rlColor4ub(startColor.r, startColor.g, startColor.b, startColor.a);
rlVertex2f(bottomLeft.x, bottomLeft.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(topRight.x, topRight.y);
rlColor4ub(endColor.r, endColor.g, endColor.b, endColor.a);
rlVertex2f(bottomRight.x, bottomRight.y);
rlEnd();
}