ch
This commit is contained in:
@@ -32,8 +32,9 @@ private:
|
|||||||
Vector2 start = {0};
|
Vector2 start = {0};
|
||||||
std::list<DrawArgs> drawCalls;
|
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 size_t getNumOfBranches(int dep);
|
||||||
inline Color getStartColor(DrawArgs &arg);
|
inline Color getStartColor(DrawArgs &arg);
|
||||||
inline Color getEndColor(int dep, Color &start);
|
inline Color getEndColor(int dep, Color &start);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
#include <rlgl.h>
|
||||||
|
|
||||||
#define ITER_PER_FRAME 5000
|
#define ITER_PER_FRAME 5000
|
||||||
|
|
||||||
@@ -56,7 +57,7 @@ bool Tree::tick()
|
|||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
while (!drawCalls.empty())
|
while (!drawCalls.empty())
|
||||||
{
|
{
|
||||||
drawBranch();
|
calculateBranch();
|
||||||
drawCalls.pop_front();
|
drawCalls.pop_front();
|
||||||
i++;
|
i++;
|
||||||
if (i >= ITER_PER_FRAME)
|
if (i >= ITER_PER_FRAME)
|
||||||
@@ -68,7 +69,7 @@ bool Tree::tick()
|
|||||||
|
|
||||||
// Private
|
// Private
|
||||||
|
|
||||||
void Tree::drawBranch()
|
void Tree::calculateBranch()
|
||||||
{
|
{
|
||||||
DrawArgs arg = drawCalls.front();
|
DrawArgs arg = drawCalls.front();
|
||||||
if (arg.dep == MAX_DEPTH)
|
if (arg.dep == MAX_DEPTH)
|
||||||
@@ -86,7 +87,7 @@ void Tree::drawBranch()
|
|||||||
|
|
||||||
Color colorStart = getStartColor(arg);
|
Color colorStart = getStartColor(arg);
|
||||||
Color colorEnd = getEndColor(arg.dep, colorStart);
|
Color colorEnd = getEndColor(arg.dep, colorStart);
|
||||||
|
// drawBranch(arg.start, end, colorStart, colorEnd, sizeStart, sizeEnd);
|
||||||
for (float i = 0; i < 1; i += fstep)
|
for (float i = 0; i < 1; i += fstep)
|
||||||
{
|
{
|
||||||
Vector2 point = Vector2Lerp(arg.start, end, i);
|
Vector2 point = Vector2Lerp(arg.start, end, i);
|
||||||
@@ -94,9 +95,6 @@ void Tree::drawBranch()
|
|||||||
int size = Lerp(sizeStart, sizeEnd, i);
|
int size = Lerp(sizeStart, sizeEnd, i);
|
||||||
DrawCircleV(point, size, color);
|
DrawCircleV(point, size, color);
|
||||||
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
|
// DrawTextureEx(texBunny, point,0, ((float)size) / texBunny.height, color);
|
||||||
|
|
||||||
// use
|
|
||||||
// DrawRectangleGradientEx
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// add more branches to draw
|
// add more branches to draw
|
||||||
@@ -200,3 +198,51 @@ inline float Tree::getAngleVar(DrawArgs &arg)
|
|||||||
|
|
||||||
return angleVar;
|
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();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user