65 lines
1.5 KiB
C++
65 lines
1.5 KiB
C++
#include <cmath>
|
|
#include "raylib.h"
|
|
|
|
#define MAX_DEPTH 10
|
|
|
|
Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, Color color)
|
|
{
|
|
angleDeg += 180.0f;
|
|
float angle = (angleDeg * PI) / 180.0f;
|
|
float nx = lenghth * std::sin(angle);
|
|
float ny = lenghth * std::cos(angle);
|
|
Vector2 end = {start.x + nx, start.y + ny};
|
|
DrawLineEx(start, end, 2.0f, color);
|
|
return end;
|
|
}
|
|
|
|
void draw_branch(Vector2 start, float angle, float len, int dep)
|
|
{
|
|
Vector2 next = draw_line(start, angle, len, PURPLE);
|
|
|
|
if (len < MAX_DEPTH)
|
|
return;
|
|
|
|
float next_len = 0.7f;
|
|
draw_branch(next, angle + 45, len * next_len, dep + 1);
|
|
draw_branch(next, angle - 45, len * next_len, dep + 1);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
const int screenWidth = 800;
|
|
const int screenHeight = 800;
|
|
|
|
InitWindow(screenWidth, screenHeight, "raylib");
|
|
|
|
SetTargetFPS(60);
|
|
|
|
const int textureWidth = screenWidth;
|
|
const int textureHeight = screenHeight;
|
|
|
|
RenderTexture2D target = LoadRenderTexture(textureWidth, textureHeight);
|
|
Vector2 start = {textureWidth / 2, textureHeight};
|
|
|
|
BeginTextureMode(target);
|
|
ClearBackground(WHITE);
|
|
draw_branch(start, 0, textureHeight / 4, 0);
|
|
EndTextureMode();
|
|
|
|
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
|
|
Rectangle dest = {0, 0, screenWidth, screenHeight};
|
|
Vector2 position = {0, 0};
|
|
Vector2 origin = {0.0f, 0.0f};
|
|
|
|
while (!WindowShouldClose())
|
|
{
|
|
BeginDrawing();
|
|
|
|
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE);
|
|
|
|
EndDrawing();
|
|
}
|
|
|
|
CloseWindow();
|
|
return 0;
|
|
} |