The examples in this directory are meant to help teach you about different aspects of the turtle crate. Each example shows a different way of using it to create a drawing.
Examples range from "beginner" to "advanced". This rating is subjective and
subject to change, so you may find that you're able to understand examples
in one of the higher categories even if you're still learning. The examples
listed here are just a few of the more prominent ones from the crate. See
the other files in the examples
directory for even more.
Source Code: square.rs
This program draws a square on the canvas by repeatedly calling the forward
and right
methods.
Source Code: circle.rs
This circle is drawn using many small lines and short turns. It's hard to tell just by looking at it, but it isn't actually perfectly round!
Source Code: dashed_circle.rs
This image is drawn using the pen_up
and pen_down
methods to periodically
lift the pen as the turtle moves around the circle. The code uses integer
division as well as the remainder operator to figure out when to lift and
lower the pen.
Source Code: three-triangles.rs
Draws three equilateral triangles and shows how the program for a single triangle can be reused to draw several of them.
Source Code: empty_star.rs
A five-pointed star with no lines in the middle. The background color is changed
from the default white and the pen size and pen color are changed as well. This
is done using the set_background_color
, set_pen_size
, and set_pen_color
methods respectively.
Source Code: squares.rs
This program repeatedly draws squares while rotating the turtle to produce a
square-petalled flower. The red and white squares are colored using the
set_fill_color
, begin_fill
and end_fill
methods.
Source Code: nestedcubes.rs
This example draws a hexagonal spiral with a fading color that alternates between blue and white. It creates an illusion of nested three-dimensional cubes isometrically projected onto the two-dimensional space of the canvas.
Source Code: coloredridge.rs
This example varies the pen size and color as the turtle moves. The size is set to the value of a gaussian function and random pen colors are used to ensure that you'll get a completely different image every time.
Source Code: heart.rs
A cute drawing of a heart. The arcs are more difficult to draw than you might expect!
Source Code: rust.rs
A (simplified) version of the logo for the Rust programming language.
Source Code: snowman.rs
A nice-looking snowman with all its common features.
Source Code: draw_turtle.rs
A turtle drawn by... a turtle!
Source Code: ferris.rs
Draws the officially unofficial mascot of the Rust language: Ferris in the "happy" version! It uses cubic Bézier curves extensively.
Source Code: dragon.rs
Draws a Heighway dragon curve. This self-similar fractal curve is generated by recursively unfolding a line by 90°. Step 1 is to draw a line segment. Step 2 is to duplicate the line segment from Step 1 and rotate it by 90° along one of its extremities to create a new extremity. You repeat the process using the new extremity and continue along to generate each line.
Source Code: sierpinski-triangle.rs
Draws a Sierpiński triangle The Sierpiński triangle is another recursively drawn self-similar shape. It consists of many nested equilateral triangles, each containing three, one in every corner.
Source Code: tree.rs
This is a tree-like fractal curve where the trunks or branches are squares rather than lines. The sizes are chosen to make two squares' vertices coincide.
Source Code: snowflake.rs
Draws a Von Koch snowflake. This uses yet another recursive algorithm: Step 1 is an equilateral triangle. Step 2 is cutting each of its sides in three and replacing their middle parts with an equilateral triangle with sides of the same length as the surrounding parts but without a base side. Step n + 1 is repeating the process for each side of step n. Note there is no "cutting" happening during the drawing, all the drawn sides are the same length, but the turning angles are computed recursively following the described algorithm.
Source Code: maze/main.rs
In this example, the turtle generates and draws a maze. Then, it goes through and solves the maze by moving through the passages until it finds the exit.