Skip to content

Instantly share code, notes, and snippets.

@deniska
Created September 17, 2024 09:04
Show Gist options
  • Save deniska/7298106bb9dab62db4b5e3a19e9093b2 to your computer and use it in GitHub Desktop.
Save deniska/7298106bb9dab62db4b5e3a19e9093b2 to your computer and use it in GitHub Desktop.
#include "raylib.h"
#include <math.h>
const int WIDTH = 1800;
const int HEIGHT = 900;
const float MIN_ANGLE = 0.0f;
const float MAX_ANGLE = M_PI/1.5;
void DrawTree(float, float, float, float);
float da;
float a_off;
int main(void) {
InitWindow(WIDTH, HEIGHT, "Fractal tree");
SetTargetFPS(60);
da = M_PI / 4;
a_off = 0.0f;
Vector2 pos;
while (!WindowShouldClose()) {
pos = GetMousePosition();
da = (MAX_ANGLE - MIN_ANGLE) * (pos.y / HEIGHT) + MIN_ANGLE;
a_off = (M_PI / 2) * ((pos.x - WIDTH/2) / WIDTH);
BeginDrawing();
ClearBackground(BLACK);
DrawTree(WIDTH/2, HEIGHT, HEIGHT/3, M_PI/2.0);
EndDrawing();
}
CloseWindow();
return 0;
}
void DrawTree(float x, float y, float len, float a) {
float tx = x + cosf(a) * len;
float ty = y - sinf(a) * len;
Color c = WHITE;
if (len < 6.0f) {
c = GREEN;
}
DrawLine((int)x, (int)y, (int)tx, (int)ty, c);
if (len < 2.0f) {
return;
}
DrawTree(tx, ty, len*0.67, a - da - a_off);
DrawTree(tx, ty, len*0.67, a + da - a_off);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment