-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example drawing a dragon curve (#29)
* add example drawing a dragon curve * add comments describing the dragon curve example
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
//! Draw a dragon curve, more specifically a highway dragon. | ||
//! (https://en.wikipedia.org/wiki/Dragon_curve) | ||
//! | ||
//! As can be seen in the above wikipedia article, the highway 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). | ||
extern crate turtle; | ||
|
||
use turtle::Turtle; | ||
use turtle::Color; | ||
|
||
fn main() { | ||
let mut turtle = Turtle::new(); | ||
|
||
turtle.set_background_color("#112244"); | ||
|
||
turtle.pen_up(); | ||
turtle.backward(160.0); | ||
turtle.right(90.); | ||
turtle.forward(110.); | ||
turtle.pen_down(); | ||
turtle.set_speed(10); | ||
|
||
dragon(&mut turtle, -90., 11, 0., 255.); | ||
|
||
turtle.hide(); | ||
} | ||
|
||
/// Draw the dragon curve by simulating folding a strip of paper | ||
/// | ||
/// `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 Turtle, | ||
fold_direction: f64, | ||
num_folds: usize, | ||
color_start: f64, | ||
color_end: f64, | ||
) { | ||
let color_mid = (color_start + color_end) * 0.5; | ||
if num_folds == 0 { | ||
// mapping a color number 0-255 to an rgb gradient. | ||
turtle.set_pen_color(Color { | ||
red: (color_mid - 128.).abs() * 2., | ||
green: color_mid, | ||
blue: 160., | ||
alpha: 1., | ||
}); | ||
return turtle.forward(10.); | ||
} | ||
|
||
// draw a left to right strip (which has a left turn in the middle) | ||
dragon(turtle, -90., 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., num_folds - 1, color_mid, color_end); | ||
} |