remove the bevy based turtle and rename turtle-lib-macroquad to turtle-lib

This commit is contained in:
2025-10-12 20:31:05 +02:00
parent fe2beb01ed
commit 08a1802bd2
46 changed files with 100 additions and 510 deletions
+97
View File
@@ -0,0 +1,97 @@
//! Cheese example - demonstrates multi-contour fills with holes
//!
//! This example creates a cheese-like shape by:
//! 1. Drawing the outer square boundary
//! 2. Lifting the pen (pen_up) to close that contour
//! 3. Drawing circular and triangular holes with pen_up/pen_down
//!
//! Lyon's EvenOdd fill rule automatically creates holes where contours overlap!
use macroquad::prelude::*;
use turtle_lib::*;
#[macroquad::main("Cheese with Holes")]
async fn main() {
let mut turtle = create_turtle();
// Set fill color to yellow (cheese color!)
turtle.set_fill_color(YELLOW);
turtle.set_pen_color(ORANGE);
turtle.set_pen_width(3.0);
println!("=== Starting cheese fill ===");
turtle.begin_fill();
// Draw outer boundary (large square)
println!("Drawing outer square boundary...");
for _ in 0..4 {
turtle.forward(400.0);
turtle.right(90.0);
}
// Close outer contour and start drawing holes
println!("Closing outer contour with pen_up");
turtle.pen_up();
// Draw triangular hole in the middle
println!("Drawing triangular hole...");
turtle.go_to(vec2(200.0, 120.0));
turtle.pen_down(); // Start new contour for hole
for _ in 0..3 {
turtle.forward(160.0);
turtle.right(120.0);
}
println!("Closing triangle contour with pen_up");
turtle.pen_up(); // Close triangle hole contour
// Draw circular hole (top-left) using circle_left
println!("Drawing circular hole (top-left) with circle_left...");
turtle.go_to(vec2(100.0, 100.0));
turtle.pen_down(); // Start new contour for hole
turtle.circle_left(30.0, 360.0, 36); // radius=30, full circle, 36 steps
println!("Closing circle contour with pen_up");
turtle.pen_up(); // Close circle hole contour
// Draw circular hole (bottom-right) using circle_right
println!("Drawing circular hole (bottom-right) with circle_right...");
turtle.go_to(vec2(280.0, 280.0));
turtle.pen_down(); // Start new contour for hole
turtle.circle_right(40.0, 360.0, 36); // radius=40, full circle, 36 steps
println!("Closing circle contour with pen_up");
turtle.pen_up(); // Close circle hole contour
// End fill - Lyon will automatically create holes!
println!("Calling end_fill - Lyon should create holes now!");
turtle.end_fill();
// Set animation speed
turtle.set_speed(300);
println!("Building and executing turtle plan...");
// Execute the plan
let mut app = TurtleApp::new().with_commands(turtle.build());
loop {
clear_background(Color::new(0.95, 0.95, 0.98, 1.0));
app.update();
app.render();
// Instructions
draw_text(
"Cheese with Holes - pen_up/pen_down creates multiple contours!",
10.0,
20.0,
18.0,
BLACK,
);
draw_text("Press ESC or Q to quit", 10.0, 40.0, 16.0, DARKGRAY);
if is_key_pressed(KeyCode::Escape) || is_key_pressed(KeyCode::Q) {
break;
}
next_frame().await;
}
}
+66
View File
@@ -0,0 +1,66 @@
//! Cheese example using the turtle_main macro
//!
//! This is a simplified version of cheese.rs that demonstrates how the
//! turtle_main macro reduces boilerplate code.
use turtle_lib::*;
#[turtle_main("Cheese with Holes - Using Macro")]
fn draw_cheese(turtle: &mut TurtlePlan) {
// Set fill color to yellow (cheese color!)
turtle.set_pen_color(ORANGE);
turtle.set_pen_width(3.0);
turtle.set_fill_color(YELLOW);
println!("=== Starting cheese fill ===");
turtle.begin_fill();
// Draw outer boundary (large square)
println!("Drawing outer square boundary...");
for _ in 0..4 {
turtle.forward(400.0);
turtle.right(90.0);
}
// Close outer contour and start drawing holes
println!("Closing outer contour with pen_up");
turtle.pen_up();
// Draw triangular hole in the middle
println!("Drawing triangular hole...");
turtle.go_to(vec2(200.0, 120.0));
turtle.pen_down(); // Start new contour for hole
for _ in 0..3 {
turtle.forward(160.0);
turtle.right(120.0);
}
println!("Closing triangle contour with pen_up");
turtle.pen_up(); // Close triangle hole contour
// Draw circular hole (top-left) using circle_left
println!("Drawing circular hole (top-left) with circle_left...");
turtle.go_to(vec2(100.0, 100.0));
turtle.pen_down(); // Start new contour for hole
turtle.circle_left(30.0, 360.0, 36); // radius=30, full circle, 36 steps
println!("Closing circle contour with pen_up");
turtle.pen_up(); // Close circle hole contour
// Draw circular hole (bottom-right) using circle_right
println!("Drawing circular hole (bottom-right) with circle_right...");
turtle.go_to(vec2(280.0, 280.0));
turtle.pen_down(); // Start new contour for hole
turtle.circle_right(40.0, 360.0, 36); // radius=40, full circle, 36 steps
println!("Closing circle contour with pen_up");
turtle.pen_up(); // Close circle hole contour
// End fill - Lyon will automatically create holes!
println!("Calling end_fill - Lyon should create holes now!");
turtle.end_fill();
// Set animation speed
turtle.set_speed(300);
println!("Building and executing turtle plan...");
}
+29
View File
@@ -0,0 +1,29 @@
//! Test circle_left and circle_right commands
use turtle_lib::*;
#[turtle_main("Circle Test")]
fn draw(turtle: &mut TurtlePlan) {
turtle.shape(ShapeType::Turtle);
// Draw some circles
turtle.set_pen_color(RED);
turtle.set_pen_width(0.5);
turtle.left(90.0);
turtle.set_speed(999);
turtle.circle_left(100.0, 540.0, 72); // partial circle to the left
turtle.forward(150.0);
turtle.set_speed(100);
turtle.set_pen_color(BLUE);
turtle.circle_right(50.0, 270.0, 72); // partial circle to the right
// Set animation speed
turtle.set_speed(20);
turtle.forward(150.0);
turtle.circle_left(50.0, 180.0, 12);
turtle.circle_right(50.0, 180.0, 12);
turtle.set_speed(700);
turtle.set_pen_color(GREEN);
turtle.circle_left(50.0, 180.0, 36); // Half circle to the left
}
+93
View File
@@ -0,0 +1,93 @@
//! Draw a dragon curve, more specifically a Heighway dragon.
//!
//! (https://en.wikipedia.org/wiki/Dragon_curve)
//!
//! As can be seen in the above Wikipedia article, the Heighway dragon can be
//! constructed by repeatedly folding a strip of paper and looking at the
//! directions of the folds/turns.
//!
//! Starting with a strip going left to right (l2r):
//!
//! start|--->---l2r--->---|end
//!
//! you might fold it like this:
//!
//! end|---<---r2l---<---\
//! start|->---l2r--->---/
//!
//! Getting a l2r strip, followed by a left turn, followed by a r2l strip.
//!
//! Folding a right to left strip:
//!
//! end|---<---r2l---<---|start
//!
//! In the same way:
//!
//! start|-->---l2r--->---\
//! end|----<---r2l---<---/
//!
//! Would give you a l2r, followed by a right turn, followed by a r2l strip.
//!
//! As you can see, the only difference between the two is the direction of
//! the turn in the middle.
//!
//! This folding of paper is simulated by recursively calling the dragon(..)
//! function, passing the direction of the turn for this fold as an angle
//! (+90 for a right turn, -90 for a left turn).
use turtle_lib::*;
#[turtle_main("Dragon Curve")]
fn draw_dragon(turtle: &mut TurtlePlan) {
// Fast drawing
turtle.set_speed(1200);
// Start position
turtle.pen_up();
turtle.backward(160.0);
turtle.right(90.0);
turtle.forward(110.0);
turtle.pen_down();
turtle.set_pen_width(6.);
// Draw the dragon curve with 13 folds
dragon(turtle, -90.0, 13, 0.0, 255.0);
// Hide turtle when done
turtle.hide();
}
/// Draw the dragon curve by simulating folding a strip of paper
///
/// Arguments:
/// `fold_direction`: The direction of the fold, +90 for a right, -90 for a
/// left turn.
/// `num_folds`: The number of times to fold the 'strip of paper'.
/// `color_start`/`color_end`: The color at the start/end of this subsection
/// of the curve as a number 0-255.
fn dragon(
turtle: &mut TurtlePlan,
fold_direction: f32,
num_folds: usize,
color_start: f32,
color_end: f32,
) {
let color_mid = (color_start + color_end) * 0.5;
if num_folds == 0 {
// Mapping a color number 0-255 to an RGB gradient
let red = ((color_mid - 128.0).abs() * 2.0).floor();
let green = color_mid;
let blue = 160.0;
turtle.set_pen_color(Color::new(red / 255.0, green / 255.0, blue / 255.0, 1.0));
turtle.forward(10.0);
return;
}
// Draw a left to right strip (which has a left turn in the middle)
dragon(turtle, -90.0, num_folds - 1, color_start, color_mid);
turtle.right(fold_direction);
// Draw a right to left strip (which has a right turn in the middle)
dragon(turtle, 90.0, num_folds - 1, color_mid, color_end);
}
+159
View File
@@ -0,0 +1,159 @@
//! Advanced fill example with multiple holes and complex shapes
//!
//! This example uses manual setup to demonstrate custom window size and UI elements.
use macroquad::{miniquad::window::set_window_size, prelude::*};
use turtle_lib::*;
#[macroquad::main("Advanced Fill Demo")]
async fn main() {
set_window_size(2000, 1900);
let mut t = create_turtle();
// Example 1: Star shape (concave polygon)
t.pen_up();
t.go_to(vec2(-200.0, 100.0));
t.pen_down();
t.set_heading(0.0);
t.set_fill_color(GOLD);
t.set_pen_color(ORANGE);
t.set_pen_width(2.0);
t.set_speed(500);
t.begin_fill();
// Draw 5-pointed star
for _ in 0..5 {
t.forward(100.0);
t.right(144.0);
}
t.end_fill();
// Example 2: Swiss cheese (polygon with multiple holes)
t.pen_up();
t.go_to(vec2(100.0, 100.0));
t.pen_down();
t.set_heading(0.0);
t.set_fill_color(YELLOW);
t.set_pen_color(ORANGE);
t.begin_fill();
// Outer square
for _ in 0..4 {
t.forward(150.0);
t.right(90.0);
}
// First hole (circle)
t.pen_up();
t.go_to(vec2(140.0, 130.0));
t.pen_down();
t.circle_right(150.0, 360.0, 36);
// Second hole (circle)
t.pen_up();
t.go_to(vec2(200.0, 170.0));
t.pen_down();
t.circle_right(10.0, 360.0, 36);
// Third hole (triangle)
t.pen_up();
t.go_to(vec2(160.0, 200.0));
t.pen_down();
t.circle_right(15.0, 360.0, 3);
// Fourth hole (square)
t.pen_up();
t.go_to(vec2(190.0, 200.0));
t.pen_down();
t.circle_right(15.0, 360.0, 4);
// fifth hole (pentagon)
t.pen_up();
t.go_to(vec2(230.0, 200.0));
t.pen_down();
t.circle_right(15.0, 360.0, 5);
t.end_fill();
// Example 3: Donut (circle with circular hole)
t.pen_up();
t.go_to(vec2(-100.0, -100.0));
t.pen_down();
t.set_heading(0.0);
t.set_fill_color(Color::new(0.8, 0.4, 0.2, 1.0));
t.set_pen_color(Color::new(0.6, 0.3, 0.1, 1.0));
t.begin_fill();
// Outer circle
for _ in 0..72 {
t.forward(3.0);
t.right(5.0);
}
// Move to inner circle
t.pen_up();
t.go_to(vec2(-75.0, -90.0));
t.pen_down();
// Inner circle (hole)
for _ in 0..72 {
t.forward(1.5);
t.right(5.0);
}
t.end_fill();
// Set animation speed
t.set_speed(500);
let mut app = TurtleApp::new().with_commands(t.build());
let target_fps = 1.0; // 1 frame per second for debugging
let frame_time = 1.0 / target_fps;
let mut last_frame_time = macroquad::time::get_time();
loop {
// Frame rate limiting
let current_time = macroquad::time::get_time();
let delta = current_time - last_frame_time;
if delta < frame_time {
// std::thread::sleep(std::time::Duration::from_secs_f64(frame_time - delta));
}
last_frame_time = macroquad::time::get_time();
clear_background(Color::new(0.95, 0.95, 0.98, 1.0));
app.update();
app.render();
// Instructions
draw_text(
"Advanced Fill Demo: Star, Swiss Cheese, Donut",
10.0,
20.0,
20.0,
BLACK,
);
draw_text(
"Features: concave polygons, multiple holes, pen_up during fill",
10.0,
40.0,
16.0,
DARKGRAY,
);
draw_text(
"Mouse: drag to pan, scroll to zoom",
10.0,
60.0,
16.0,
DARKGRAY,
);
next_frame().await
}
}
+14
View File
@@ -0,0 +1,14 @@
//! Minimal turtle example - just 10 lines!
//!
//! This is the simplest possible turtle program using the macro.
use turtle_lib::*;
#[turtle_main("Hello Turtle")]
fn hello() {
turtle.set_pen_color(BLUE);
for _ in 0..4 {
turtle.forward(100.0);
turtle.right(90.0);
}
}
+37
View File
@@ -0,0 +1,37 @@
//! Koch snowflake fractal example
use turtle_lib::*;
fn koch(depth: u32, turtle: &mut TurtlePlan, distance: f32) {
if depth == 0 {
turtle.forward(distance);
} else {
let new_distance = distance / 3.0;
koch(depth - 1, turtle, new_distance);
turtle.left(60.0);
koch(depth - 1, turtle, new_distance);
turtle.right(120.0);
koch(depth - 1, turtle, new_distance);
turtle.left(60.0);
koch(depth - 1, turtle, new_distance);
}
}
#[turtle_main("Koch Snowflake")]
fn draw(turtle: &mut TurtlePlan) {
// Position turtle
turtle.set_speed(1001);
turtle.pen_up();
turtle.backward(150.0);
turtle.pen_down();
// Draw Koch snowflake (triangle of Koch curves)
for _ in 0..3 {
koch(4, turtle, 300.0);
turtle.right(120.0);
turtle.set_speed(1200);
}
turtle.hide(); // Hide turtle when done
}
+100
View File
@@ -0,0 +1,100 @@
//! Example demonstrating how to enable logging/tracing output from the turtle library
//!
//! This example shows how to use `tracing-subscriber` to see debug output from the library.
//! You can control the log level using the `RUST_LOG` environment variable:
//!
//! ```bash
//! # Show all debug output from turtle-lib
//! RUST_LOG=turtle_lib=debug cargo run --example logging_example
//!
//! # Show only warnings and errors
//! RUST_LOG=turtle_lib=warn cargo run --example logging_example
//!
//! # Show trace-level output (very verbose, includes all vertices)
//! RUST_LOG=turtle_lib=trace cargo run --example logging_example
//!
//! # Show debug output from specific modules
//! RUST_LOG=turtle_lib::tessellation=debug cargo run --example logging_example
//! RUST_LOG=turtle_lib::execution=debug cargo run --example logging_example
//! ```
//!
//! Note: This example uses manual setup to demonstrate custom initialization logic.
use macroquad::prelude::*;
use turtle_lib::*;
#[macroquad::main("Turtle Logging Example")]
async fn main() {
// Initialize tracing subscriber to see debug output
// This will respect the RUST_LOG environment variable
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// Default to showing info-level logs if RUST_LOG is not set
tracing_subscriber::EnvFilter::new("turtle_lib=info")
}),
)
.with_target(true) // Show which module the log came from
.with_thread_ids(false)
.with_line_number(true) // Show line numbers
.with_file(false)
.init();
tracing::info!("Starting turtle graphics example with logging enabled");
tracing::info!(
"Try running with: RUST_LOG=turtle_lib=debug cargo run --example logging_example"
);
// Create a turtle plan with fill operations to see detailed logging
let mut t = create_turtle();
t.set_speed(900);
// Draw a yin-yang symbol with fills (generates lots of debug output)
t.circle_left(90.0, 180.0, 36);
t.begin_fill();
t.circle_left(90.0, 180.0, 36);
t.circle_left(45.0, 180.0, 26);
t.circle_right(45.0, 180.0, 26);
t.pen_up();
t.right(90.0);
t.forward(37.0);
t.left(90.0);
t.pen_down();
t.circle_right(8.0, 360.0, 12);
t.pen_up();
t.right(90.0);
t.forward(90.0);
t.left(90.0);
t.pen_down();
t.circle_right(8.0, 360.0, 12);
t.end_fill();
tracing::info!("Turtle plan created, starting animation");
// Set animation speed
t.set_speed(100); // Slow animation to see the logs in real-time
// Create turtle app
let mut app = TurtleApp::new().with_commands(t.build());
// Main loop
loop {
clear_background(WHITE);
// Update and render - this is where you'll see debug logs
app.update();
app.render();
// Exit when animation is complete
if app.is_complete() {
tracing::info!("Animation complete, press any key to exit");
if is_key_pressed(KeyCode::Space) {
break;
}
}
next_frame().await
}
tracing::info!("Example finished");
}
+17
View File
@@ -0,0 +1,17 @@
//! Simple demo of the turtle_main macro
//!
//! This example shows how the turtle_main macro simplifies turtle programs
//! by automatically handling window setup, turtle creation, and the render loop.
use turtle_lib::*;
#[turtle_main("Macro Demo - Simple Square")]
fn draw_square(turtle: &mut TurtlePlan) {
turtle.set_pen_color(BLUE);
turtle.set_pen_width(3.0);
for _ in 0..4 {
turtle.forward(150.0);
turtle.right(90.0);
}
}
+17
View File
@@ -0,0 +1,17 @@
//! Demo of the turtle_main macro with inline code
//!
//! This example shows that you can write your turtle code directly
//! in the function body without taking a turtle parameter.
use turtle_lib::*;
#[turtle_main("Macro Demo - Inline Spiral")]
fn draw_spiral() {
turtle.set_pen_color(RED);
turtle.set_pen_width(2.0);
for i in 0..36 {
turtle.forward(i as f32 * 3.0);
turtle.right(25.0);
}
}
+57
View File
@@ -0,0 +1,57 @@
//! Nikolaus example - draws a house-like figure
use turtle_lib::*;
fn nikolausquadrat(turtle: &mut TurtlePlan, groesse: f32) {
turtle.forward(groesse);
turtle.left(90.0);
turtle.forward(groesse);
turtle.left(90.0);
turtle.forward(groesse);
turtle.left(90.0);
turtle.forward(groesse);
turtle.left(90.0);
}
fn nikolausdiag(turtle: &mut TurtlePlan, groesse: f32) {
let quadrat = groesse * groesse;
let diag = (quadrat + quadrat).sqrt();
turtle.left(45.0);
turtle.forward(diag);
turtle.left(45.0);
nikolausdach2(turtle, groesse);
turtle.left(45.0);
turtle.forward(diag);
turtle.left(45.0);
}
fn nikolausdach2(turtle: &mut TurtlePlan, groesse: f32) {
let quadrat = groesse * groesse;
let diag = (quadrat + quadrat).sqrt();
turtle.left(45.0);
turtle.forward(diag / 2.0);
turtle.left(90.0);
turtle.forward(diag / 2.0);
turtle.left(45.0);
}
fn nikolaus(turtle: &mut TurtlePlan, groesse: f32) {
nikolausquadrat(turtle, groesse);
nikolausdiag(turtle, groesse);
}
#[turtle_main("Nikolaus")]
fn draw(turtle: &mut TurtlePlan) {
turtle.shape(ShapeType::Turtle);
// Position the turtle (pen up, move, pen down)
turtle.pen_up();
turtle.backward(80.0);
turtle.left(90.0);
turtle.forward(50.0);
turtle.right(90.0);
turtle.pen_down();
nikolaus(turtle, 100.0);
}
+32
View File
@@ -0,0 +1,32 @@
//! Example demonstrating different turtle shapes
use turtle_lib::*;
#[turtle_main("Turtle Shapes")]
fn draw(turtle: &mut TurtlePlan) {
// Start with triangle (default)
turtle.forward(100.0);
turtle.right(90.0);
// Change to turtle shape
turtle.shape(ShapeType::Turtle);
turtle.forward(100.0);
turtle.right(90.0);
// Change to circle
turtle.shape(ShapeType::Circle);
turtle.forward(100.0);
turtle.right(90.0);
// Change to square
turtle.shape(ShapeType::Square);
turtle.forward(100.0);
turtle.right(90.0);
// Change to arrow
turtle.shape(ShapeType::Arrow);
turtle.forward(100.0);
// Set animation speed
turtle.set_speed(50);
}
@@ -0,0 +1,98 @@
//! Draws a Sierpiński triangle with automatic positioning and sizing.
//!
//! The Sierpiński triangle is a fairly simple self-similar fractal geometric shape: it consists of
//! many nested equilateral triangles. More formally, such a triangle is itself three triangles of
//! one level below and a size divided by two. Level zero means a simple equilateral triangle. The
//! drawing procedure is as follows, for a given level and size:
//!
//! * If level is 0
//! * Draw an equilateral triangle of the given size.
//! * otherwise
//! * Draw the half-sized level - 1 triangle at the bottom left.
//! * Go the start of the bottom-right slot.
//! * Draw a half-sized level - 1 triangle.
//! * Go to the start of the top slot.
//! * Draw a half-sized level - 1 triangle.
//!
//! That is relatively easy to implement, as long as you follow these steps and let recursion do
//! the rest. Another little bonus this example provides is the ability to customize the drawing
//! size: the triangle will stay correctly sized and positioned automatically.
use macroquad::window::{screen_height, screen_width};
use turtle_lib::*;
/// The number of levels to draw following the recursive procedure.
const LEVELS: u8 = 9;
/// Triangle size (adjust to fit nicely in window)
const TRIANGLE_SIZE: f32 = 300.0;
#[turtle_main("Sierpiński Triangle")]
fn draw_sierpinski(turtle: &mut TurtlePlan) {
turtle.set_speed(1500); // Fast drawing
turtle.set_pen_width(0.2);
// Auto-sized procedure
sierpinski_triangle_auto(turtle, LEVELS);
// Hide turtle when done drawing in order to fully reveal the result
turtle.hide();
}
/// Recursive function drawing a Sierpiński triangle.
///
/// It will do it with the given `turtle` and start at its current position and heading. `level`
/// is the depth of the drawing to be done, zero meaning a simple triangle. `size` is the length
/// of the outermost triangle's sides.
fn sierpinski_triangle(turtle: &mut TurtlePlan, level: u8, size: f32) {
// When level 0 is reached, just draw an equilateral triangle.
if level == 0 {
turtle.pen_down();
for _ in 0..3 {
turtle.forward(size);
turtle.left(120.0);
}
turtle.pen_up();
} else {
// Parameters for subsequent calls are the same.
let next_level = level - 1;
let next_size = size / 2.0;
// Bottom-left triangle.
sierpinski_triangle(turtle, next_level, next_size);
turtle.forward(next_size);
// Bottom-right triangle.
sierpinski_triangle(turtle, next_level, next_size);
turtle.left(120.0);
turtle.forward(next_size);
turtle.right(120.0);
// Top triangle.
sierpinski_triangle(turtle, next_level, next_size);
// Go back to the start.
turtle.right(120.0);
turtle.forward(next_size);
turtle.left(120.0);
}
}
/// Draws a Sierpiński triangle with automatic size and start point.
///
/// `level` is still required, it can't be computed automatically. However, given the used
/// canvas size, it will compute the appropriate size and start point so the triangle gets
/// centered and occupies as much drawing space as possible while staying in bounds.
fn sierpinski_triangle_auto(turtle: &mut TurtlePlan, level: u8) {
let size = TRIANGLE_SIZE;
turtle.pen_up();
turtle.go_to((-screen_width() / 2.0 + 20.0, screen_height() / 2.0 - 20.0));
turtle.set_heading(0.0); // 0 = East (pointing right)
// The drawing itself.
sierpinski_triangle(turtle, level, size);
}
+21
View File
@@ -0,0 +1,21 @@
//! Star pattern example demonstrating complex turtle patterns
use turtle_lib::*;
#[turtle_main("Star Pattern")]
fn draw(turtle: &mut TurtlePlan) {
turtle.shape(ShapeType::Turtle);
turtle.set_speed(1500);
turtle.set_pen_width(0.5);
// Draw a 5-pointed star pattern repeatedly
for _i in 0..50000 {
turtle.forward(200.0);
turtle.circle_left(10.0, 72.0, 1000);
turtle.circle_right(5.0, 360.0, 1000);
turtle.circle_left(10.0, 72.0, 1000);
}
// Set animation speed
turtle.set_speed(300);
}
+126
View File
@@ -0,0 +1,126 @@
//! Celebrates the 1.0.0 release of the original sunjay/turtle library.
//!
//! This example draws "1.0.0" with decorative background lines and filled shapes.
//! Ported from the original sunjay/turtle example.
use turtle_lib::*;
#[turtle_main("Version 1.0.0")]
fn draw_version(turtle: &mut TurtlePlan) {
turtle.set_pen_width(10.0);
turtle.set_speed(999); // instant
turtle.pen_up();
turtle.go_to(vec2(350.0, 178.0));
turtle.pen_down();
bg_lines(turtle);
turtle.pen_up();
turtle.go_to(vec2(-270.0, -200.0));
turtle.set_heading(90.0);
turtle.pen_down();
turtle.set_speed(100); // normal
turtle.set_pen_color(BLUE);
// Cyan with alpha - using RGB values for Color::from("#00E5FF")
turtle.set_fill_color([0.0, 0.898, 1.0, 0.75]);
one(turtle);
turtle.set_speed(200); // faster
turtle.pen_up();
turtle.left(90.0);
turtle.backward(50.0);
turtle.pen_down();
small_circle(turtle);
turtle.pen_up();
turtle.backward(150.0);
turtle.pen_down();
zero(turtle);
turtle.pen_up();
turtle.backward(150.0);
turtle.pen_down();
small_circle(turtle);
turtle.pen_up();
turtle.backward(150.0);
turtle.pen_down();
zero(turtle);
}
fn bg_lines(turtle: &mut TurtlePlan) {
// Light green color for background lines (#76FF03)
turtle.set_pen_color([0.463, 1.0, 0.012, 1.0].into());
turtle.set_heading(165.0);
turtle.forward(280.0);
turtle.left(147.0);
turtle.forward(347.0);
turtle.right(158.0);
turtle.forward(547.0);
turtle.left(138.0);
turtle.forward(539.0);
turtle.right(168.0);
turtle.forward(477.0);
turtle.left(154.0);
turtle.forward(377.0);
turtle.right(158.0);
turtle.forward(329.0);
}
fn small_circle(turtle: &mut TurtlePlan) {
turtle.begin_fill();
for _ in 0..90 {
turtle.forward(1.0);
turtle.right(4.0);
}
turtle.end_fill();
}
fn one(turtle: &mut TurtlePlan) {
turtle.begin_fill();
for _ in 0..2 {
turtle.forward(420.0);
turtle.left(90.0);
turtle.forward(50.0);
turtle.left(90.0);
}
turtle.end_fill();
}
fn zero(turtle: &mut TurtlePlan) {
turtle.begin_fill();
for _ in 0..2 {
arc_right(turtle);
arc_forward(turtle);
}
turtle.end_fill();
}
fn arc_right(turtle: &mut TurtlePlan) {
// Draw an arc that moves right faster than it moves forward
for i in 0..90 {
turtle.forward(3.0);
turtle.right((90.0 - i as f32) / 45.0);
}
}
fn arc_forward(turtle: &mut TurtlePlan) {
// Draw an arc that moves forward faster than it moves right
for i in 0..90 {
turtle.forward(3.0);
turtle.right(i as f32 / 45.0);
}
}
+30
View File
@@ -0,0 +1,30 @@
//! Yin-Yang symbol example demonstrating multi-contour fills
use turtle_lib::*;
#[turtle_main("Yin-Yang")]
fn draw(turtle: &mut TurtlePlan) {
turtle.set_speed(900);
turtle.circle_left(90.0, 180.0, 36);
turtle.begin_fill();
turtle.circle_left(90.0, 180.0, 36);
turtle.circle_left(45.0, 180.0, 26);
turtle.circle_right(45.0, 180.0, 26);
turtle.pen_up();
turtle.right(90.0);
turtle.forward(37.0);
turtle.left(90.0);
turtle.pen_down();
turtle.circle_right(8.0, 360.0, 12);
turtle.pen_up();
turtle.right(90.0);
turtle.forward(90.0);
turtle.left(90.0);
turtle.pen_down();
turtle.circle_right(8.0, 360.0, 12);
turtle.end_fill();
// Set animation speed
turtle.set_speed(1000);
}