add examples
This commit is contained in:
parent
f43a71739e
commit
630b4fdc44
93
turtle-lib-macroquad/examples/dragon.rs
Normal file
93
turtle-lib-macroquad/examples/dragon.rs
Normal 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_macroquad::*;
|
||||
|
||||
#[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);
|
||||
}
|
||||
98
turtle-lib-macroquad/examples/sierpinski_triangle.rs
Normal file
98
turtle-lib-macroquad/examples/sierpinski_triangle.rs
Normal file
@ -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_macroquad::*;
|
||||
|
||||
/// 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);
|
||||
}
|
||||
126
turtle-lib-macroquad/examples/v1_0_0.rs
Normal file
126
turtle-lib-macroquad/examples/v1_0_0.rs
Normal 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_macroquad::*;
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user