Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal for NTimes #353

Merged
merged 8 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,16 @@ path = "examples/nature_of_code/chp_07_cellular_automata/7_03_game_of_life_oop.r
[[example]]
name = "7_04_exercise_wolfram_ca_scrolling"
path = "examples/nature_of_code/chp_07_cellular_automata/7_04_exercise_wolfram_ca_scrolling.rs"
# --------------- Chapter 8 Fractals
[[example]]
name = "8_1_recursion"
path = "examples/nature_of_code/chp_08_fractals/8_1_recursion.rs"
[[example]]
name = "8_2_recursion"
path = "examples/nature_of_code/chp_08_fractals/8_2_recursion.rs"
[[example]]
name = "8_3_recursion"
path = "examples/nature_of_code/chp_08_fractals/8_3_recursion.rs"
[[example]]
name = "8_4_cantor_set"
path = "examples/nature_of_code/chp_08_fractals/8_4_cantor_set.rs"
3 changes: 2 additions & 1 deletion examples/loop_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ fn key_pressed(app: &App, _model: &mut Model, _key: Key) {
match app.loop_mode() {
LoopMode::Wait { .. } => app.set_loop_mode(LoopMode::refresh_sync()),
LoopMode::RefreshSync { .. } => app.set_loop_mode(LoopMode::rate_fps(60.0)),
LoopMode::Rate { .. } => app.set_loop_mode(LoopMode::wait(3)),
LoopMode::Rate { .. } => app.set_loop_mode(LoopMode::loop_once()),
LoopMode::NTimes { .. } => app.set_loop_mode(LoopMode::wait(3)),
}
println!("Loop mode switched to: {:?}", app.loop_mode());
let title = format!("`LoopMode` Demonstration - `{:?}`", app.loop_mode());
Expand Down
50 changes: 50 additions & 0 deletions examples/nature_of_code/chp_08_fractals/8_1_recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
//
// Example 8-1: Simple Recursion
use nannou::prelude::*;

fn main() {
nannou::app(model).run();
}

struct Model;

fn model(app: &App) -> Model {
app.set_loop_mode(LoopMode::loop_once());
let _window = app
.new_window()
.with_dimensions(640, 360)
.view(view)
.build()
.unwrap();
Model
}

fn view(app: &App, _model: &Model, frame: Frame) -> Frame {
// Begin drawing
let draw = app.draw();
draw.background().color(WHITE);

draw_circle(&draw, 0.0, 0.0, app.window_rect().w());

// Write the result of our drawing to the window's OpenGL frame.
draw.to_frame(app, &frame).unwrap();

// Return the drawn frame.
frame
}

fn draw_circle(draw: &app::Draw, x: f32, y: f32, mut r: f32) {
draw.ellipse()
.x_y(x, y)
.radius(r)
.hsv(map_range(r, 2.0, 360.0, 0.0, 1.0), 0.75, 1.0);
// Exit condition, stop when radius is too small
if r > 2.0 {
r *= 0.75;
// Call the function insie the function! (recursion!)
draw_circle(&draw, x, y, r);
}
}
52 changes: 52 additions & 0 deletions examples/nature_of_code/chp_08_fractals/8_2_recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
//
// Example 8-2: Simple Recursion
use nannou::prelude::*;

fn main() {
nannou::app(model).run();;
}

struct Model;

fn model(app: &App) -> Model {
app.set_loop_mode(LoopMode::loop_once());
let _window = app
.new_window()
.with_dimensions(640, 360)
.view(view)
.build()
.unwrap();
Model
}

fn view(app: &App, _model: &Model, frame: Frame) -> Frame {
// Begin drawing
let draw = app.draw();
draw.background().color(WHITE);

draw_circle(&draw, 0.0, 0.0, 400.0);

// Write the result of our drawing to the window's OpenGL frame.
draw.to_frame(app, &frame).unwrap();

// Return the drawn frame.
frame
}

// Recursive function
fn draw_circle(draw: &app::Draw, x: f32, y: f32, r: f32) {
draw.ellipse()
.x_y(x, y)
.radius(r)
.hsv(map_range(r, 2.0, 360.0, 0.0, 1.0), 0.75, 1.0);

if r > 2.0 {
// Now we draw two more circles, one to the left
// and one to the right
draw_circle(&draw, x + r / 2.0, y, r / 2.0);
draw_circle(&draw, x - r / 2.0, y, r / 2.0);
}
}
53 changes: 53 additions & 0 deletions examples/nature_of_code/chp_08_fractals/8_3_recursion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
//
// Example 8-3: Simple Recursion
use nannou::prelude::*;

fn main() {
nannou::app(model).run();
}

struct Model;

fn model(app: &App) -> Model {
app.set_loop_mode(LoopMode::loop_once());
let _window = app
.new_window()
.with_dimensions(640, 360)
.view(view)
.build()
.unwrap();
Model
}

fn view(app: &App, _model: &Model, frame: Frame) -> Frame {
// Begin drawing
let draw = app.draw();
draw.background().color(WHITE);

draw_circle(&draw, 0.0, 0.0, 400.0);

// Write the result of our drawing to the window's OpenGL frame.
draw.to_frame(app, &frame).unwrap();

// Return the drawn frame.
frame
}

// Recursive function
fn draw_circle(draw: &app::Draw, x: f32, y: f32, r: f32) {
draw.ellipse()
.x_y(x, y)
.radius(r)
.hsv(map_range(r, 2.0, 360.0, 0.0, 1.0), 0.75, 1.0);

if r > 8.0 {
// Four circles! left right, up and down
draw_circle(&draw, x + r / 2.0, y, r / 2.0);
draw_circle(&draw, x - r / 2.0, y, r / 2.0);
draw_circle(&draw, x, y + r / 2.0, r / 2.0);
draw_circle(&draw, x, y - r / 2.0, r / 2.0);
}
}
60 changes: 60 additions & 0 deletions examples/nature_of_code/chp_08_fractals/8_4_cantor_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
//
// Example 8-4: Cantor Set
// Renders a simple fractal, the Cantor Set
use nannou::prelude::*;

fn main() {
nannou::app(model).run();
}

struct Model;

fn model(app: &App) -> Model {
app.set_loop_mode(LoopMode::loop_once());
let _window = app
.new_window()
.with_dimensions(800, 200)
.view(view)
.build()
.unwrap();
Model
}

fn view(app: &App, _model: &Model, frame: Frame) -> Frame {
// Begin drawing
let draw = app.draw();
draw.background().color(WHITE);

let win = app.window_rect();
cantor(&draw, 0.0, win.top(), 730.0);

// Write the result of our drawing to the window's OpenGL frame.
draw.to_frame(app, &frame).unwrap();

// Return the drawn frame.
frame
}

// Recursive function
fn cantor(draw: &app::Draw, x: f32, mut y: f32, len: f32) {
let h = 30.0;

// recursive exit condition
if len >= 1.0 {
// Draw line (as rectangle to make it easier to see)
draw.rect()
.x_y(x, y - h / 6.0)
.w_h(len, h / 3.0)
.color(BLACK);

// Go down to next y position
y -= h;
// Draw 2 more lines 1/3rd the length (without the middle section)
let length = len / 3.0;
cantor(&draw, x - length, y, length);
cantor(&draw, (x + len * 2.0 / 3.0) - length, y, length);
}
}
Loading