-
-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #353 from CGUTA/master
Proposal for NTimes
- Loading branch information
Showing
8 changed files
with
366 additions
and
2 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
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
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.