Created
September 23, 2022 12:04
-
-
Save swallez/4c76de8cf43587b2440560c047d54361 to your computer and use it in GitHub Desktop.
Cercles fractals en Rust avec la librairie macroquad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use macroquad::prelude::*; | |
#[macroquad::main("Cercles fractals")] | |
async fn main() { | |
loop { | |
clear_background(LIGHTGRAY); | |
circles(0, screen_width() / 2.0, screen_height()/2.0, 150.0, 0); | |
next_frame().await | |
} | |
} | |
// Facteur multiplicatif pour le rayon des cercles à chaque itération | |
const RADIUS_RATIO: f32 = 0.5; | |
fn circles(iteration: i32, mut x: f32, mut y: f32, mut r: f32, angle: i32) { | |
if iteration == 4 { | |
// Fini ! | |
return; | |
} | |
// A quelle distance de x, y va être le centre du cercle ? | |
let distance = r * (1.0 + RADIUS_RATIO); | |
// On s'assure que l'angle est positif | |
let angle = (angle+360) % 360; | |
// On ne se déplace que pour les itérations > 0 | |
if iteration > 0 { | |
if angle == 0 { | |
y = y - distance; | |
} else if angle == 90 { | |
x = x + distance; | |
} else if angle == 180 { | |
y = y + distance; | |
} else if angle == 270 { | |
x = x - distance; | |
} | |
} | |
r = r * RADIUS_RATIO; | |
draw_circle_lines(x, y, r, 1.0, BLACK); | |
// On itère, avec des cercles plus petits à gauche, en haut, et à droite, | |
// relativement à l'angle courant. | |
circles(iteration + 1, x, y, r, angle - 90); | |
circles(iteration + 1, x, y, r, angle); | |
circles(iteration + 1, x, y, r, angle + 90); | |
if iteration == 0 { | |
// A la première itération on dessine aussi en dessous | |
circles(iteration + 1, x, y, r, angle + 180); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment