```rust
// Movement
plan.forward(100);
plan.backward(50);
// Rotation
plan.left(90); // degrees
plan.right(45);
// Circular arcs
plan.circle_left(50.0, 180.0, 36); // radius, angle (degrees), segments
plan.circle_right(50.0, 180.0, 36); // draws arc to the right
// Pen control
plan.pen_up();
plan.pen_down();
// Appearance
plan.set_color(RED);
plan.set_pen_width(5.0);
plan.hide();
plan.show();
// Turtle shape
plan.shape(ShapeType::Triangle);
plan.shape(ShapeType::Turtle); // Default classic turtle shape
plan.shape(ShapeType::Circle);
plan.shape(ShapeType::Square);
plan.shape(ShapeType::Arrow);
// Custom shape
let custom = TurtleShape::new(
vec![vec2(10.0, 0.0), vec2(-5.0, 5.0), vec2(-5.0, -5.0)],
true // filled
);
plan.set_shape(custom);
// Chaining
plan.forward(100).right(90).forward(50);
```
50 lines
1.1 KiB
Rust
50 lines
1.1 KiB
Rust
//! Koch snowflake fractal example
|
|
|
|
use macroquad::prelude::*;
|
|
use turtle_lib_macroquad::*;
|
|
|
|
fn koch(depth: u32, plan: &mut TurtlePlan, distance: f32) {
|
|
if depth == 0 {
|
|
plan.forward(distance);
|
|
} else {
|
|
let new_distance = distance / 3.0;
|
|
koch(depth - 1, plan, new_distance);
|
|
plan.left(60.0);
|
|
koch(depth - 1, plan, new_distance);
|
|
plan.right(120.0);
|
|
koch(depth - 1, plan, new_distance);
|
|
plan.left(60.0);
|
|
koch(depth - 1, plan, new_distance);
|
|
}
|
|
}
|
|
|
|
#[macroquad::main("Koch Snowflake")]
|
|
async fn main() {
|
|
let mut plan = create_turtle();
|
|
|
|
// Position turtle
|
|
plan.set_speed(10);
|
|
plan.pen_up();
|
|
plan.backward(150.0);
|
|
|
|
plan.pen_down();
|
|
|
|
// Draw Koch snowflake (triangle of Koch curves)
|
|
for _ in 0..3 {
|
|
koch(4, &mut plan, 300.0);
|
|
plan.right(120.0);
|
|
}
|
|
|
|
plan.hide(); // Hide turtle when done
|
|
|
|
// Create app with animation
|
|
let mut app = TurtleApp::new().with_commands(plan.build(), 1000.0);
|
|
|
|
loop {
|
|
clear_background(WHITE);
|
|
app.update();
|
|
app.render();
|
|
next_frame().await
|
|
}
|
|
}
|