-
-
Notifications
You must be signed in to change notification settings - Fork 97
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 #1053 from nature-of-code/dev/exercise-with-solution
Feat: Exercise with solutions
- Loading branch information
Showing
38 changed files
with
921 additions
and
3 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
11 changes: 11 additions & 0 deletions
11
content/examples/00_randomness/exercise_0_1_solution_skewed_random_walker/index.html
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,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script> | ||
<meta charset="utf-8" /> | ||
<link rel="stylesheet" type="text/css" href="style.css" /> | ||
</head> | ||
<body> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
Binary file added
BIN
+6.87 KB
...xamples/00_randomness/exercise_0_1_solution_skewed_random_walker/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions
37
content/examples/00_randomness/exercise_0_1_solution_skewed_random_walker/sketch.js
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,37 @@ | ||
// The Nature of Code, Exercise 0.1 | ||
// Solution by Rick Sidwell | ||
// http://natureofcode.com | ||
|
||
let walker; | ||
|
||
function setup() { | ||
createCanvas(640, 240); | ||
walker = new Walker(); | ||
background(255); | ||
} | ||
|
||
function draw() { | ||
walker.step(); | ||
walker.show(); | ||
} | ||
|
||
class Walker { | ||
constructor() { | ||
this.x = width / 2; | ||
this.y = height / 2; | ||
} | ||
|
||
show() { | ||
stroke(0); | ||
point(this.x, this.y); | ||
} | ||
|
||
// Give the walker a greater tendency to move down and left by making | ||
// the random range larger in that direction | ||
step() { | ||
let xstep = random(-2.75, 3); | ||
let ystep = random(-2.75, 3); | ||
this.x += xstep; | ||
this.y += ystep; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
content/examples/00_randomness/exercise_0_1_solution_skewed_random_walker/style.css
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,7 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} |
11 changes: 11 additions & 0 deletions
11
content/examples/00_randomness/exercise_0_3_solution_random_walk_towards_mouse/index.html
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,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.min.js"></script> | ||
<meta charset="utf-8" /> | ||
<link rel="stylesheet" type="text/css" href="style.css" /> | ||
</head> | ||
<body> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
Binary file added
BIN
+10.3 KB
...es/00_randomness/exercise_0_3_solution_random_walk_towards_mouse/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions
58
content/examples/00_randomness/exercise_0_3_solution_random_walk_towards_mouse/sketch.js
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,58 @@ | ||
// The Nature of Code Exercise 0.3 | ||
// solution by Rick Sidwell | ||
// http://natureofcode.com | ||
|
||
let walker; | ||
|
||
function setup() { | ||
createCanvas(640, 240); | ||
walker = new Walker(); | ||
background(255); | ||
} | ||
|
||
function draw() { | ||
walker.step(); | ||
walker.show(); | ||
} | ||
|
||
class Walker { | ||
constructor() { | ||
this.x = width / 2; | ||
this.y = height / 2; | ||
} | ||
|
||
show() { | ||
stroke(0); | ||
point(this.x, this.y); | ||
} | ||
|
||
step() { | ||
const r = random(); | ||
if (r < 0.5) { | ||
if (r < 0.25) { | ||
if (this.x < mouseX) { | ||
this.x++; | ||
} else { | ||
this.x--; | ||
} | ||
} else { | ||
if (this.y < mouseY) { | ||
this.y++; | ||
} else { | ||
this.y--; | ||
} | ||
} | ||
} else { | ||
const choice = floor(random(4)); | ||
if (choice == 0) { | ||
this.x++; | ||
} else if (choice == 1) { | ||
this.x--; | ||
} else if (choice == 2) { | ||
this.y++; | ||
} else { | ||
this.y--; | ||
} | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
content/examples/00_randomness/exercise_0_3_solution_random_walk_towards_mouse/style.css
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,7 @@ | ||
html, body { | ||
margin: 0; | ||
padding: 0; | ||
} | ||
canvas { | ||
display: block; | ||
} |
15 changes: 15 additions & 0 deletions
15
content/examples/00_randomness/exercise_0_4_solution_paint_splatter/index.html
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,15 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/p5.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.10.0/addons/p5.sound.min.js"></script> | ||
<link rel="stylesheet" type="text/css" href="style.css"> | ||
<meta charset="utf-8" /> | ||
|
||
</head> | ||
<body> | ||
<main> | ||
</main> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
Binary file added
BIN
+46.6 KB
content/examples/00_randomness/exercise_0_4_solution_paint_splatter/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 126 additions & 0 deletions
126
content/examples/00_randomness/exercise_0_4_solution_paint_splatter/sketch.js
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,126 @@ | ||
// The Nature of Code, Exercise 0.4 | ||
// Solution by Rick Sidwell | ||
// http://natureofcode.com | ||
|
||
// Controls | ||
let spreadSlider; | ||
let sizeSlider; | ||
let sizespSlider; | ||
let baseHueSlider; | ||
let huespSlider; | ||
let alphaSlider; | ||
|
||
function setup() { | ||
|
||
createCanvas(640, 240); | ||
colorMode(HSB); | ||
background(97); | ||
createControls(300); | ||
|
||
} | ||
|
||
function draw() { | ||
translate(width / 2, height / 2); | ||
scale(height / 2); | ||
|
||
x = randomGaussian(0, spreadSlider.value()); | ||
y = randomGaussian(0, spreadSlider.value()); | ||
size = randomGaussian(sizeSlider.value() / height, sizespSlider.value()); | ||
if (size <= 0) { | ||
size = 0.001; | ||
} | ||
|
||
let paintHue = randomGaussian(baseHueSlider.value(), huespSlider.value()); | ||
let paintSat = randomGaussian(80, 20); | ||
let paintBright = randomGaussian(80, 20); | ||
if (paintHue < 0) { | ||
paintHue += 360; | ||
} else if (paintHue >= 360) { | ||
paintHue -= 360; | ||
} | ||
if (paintSat > 100) { | ||
paintSat = 100; | ||
} | ||
if (paintBright > 100) { | ||
paintBright = 100; | ||
} | ||
|
||
noStroke(); | ||
fill(paintHue, paintSat, paintBright, alphaSlider.value()); | ||
ellipse(x, y, size, size); | ||
} | ||
|
||
function createControls(ypos) { | ||
let xpos = 0; | ||
|
||
cpTitle = createP("Paint Splatter Simulation"); | ||
cpTitle.position(xpos, ypos -50); | ||
cpTitle.style("font-size", "14pt"); | ||
cpTitle.style("font-weight", "bold"); | ||
xpos += 220; | ||
|
||
clearButton = createButton("Clear"); | ||
clearButton.position(xpos, ypos-50); | ||
clearButton.mousePressed(clearButtonClicked); | ||
|
||
|
||
xpos=0; | ||
spreadTitle = createP("Spread"); | ||
spreadTitle.position(xpos, ypos); | ||
xpos += 50; | ||
|
||
spreadSlider = createSlider(0, 0.75, 0.25, 0); | ||
spreadSlider.position(xpos, ypos); | ||
spreadSlider.size(80); | ||
xpos += 100; | ||
|
||
sizeTitle = createP("Size"); | ||
sizeTitle.position(xpos, ypos); | ||
xpos += 35; | ||
|
||
sizeSlider = createSlider(5, 50, 20, 0); | ||
sizeSlider.position(xpos, ypos); | ||
sizeSlider.size(80); | ||
xpos += 100; | ||
|
||
sizespTitle = createP("Size Spread"); | ||
sizespTitle.position(xpos, ypos); | ||
xpos += 80; | ||
|
||
sizespSlider = createSlider(0, 0.1, 0.01, 0); | ||
sizespSlider.position(xpos, ypos); | ||
sizespSlider.size(80); | ||
xpos += 100; | ||
|
||
xpos=0; | ||
baseHueTitle = createP("Base Hue"); | ||
baseHueTitle.position(xpos, ypos+30); | ||
xpos += 70; | ||
|
||
baseHueSlider = createSlider(0, 360, 250, 0); | ||
baseHueSlider.position(xpos, ypos+30); | ||
baseHueSlider.size(80); | ||
xpos += 100; | ||
|
||
huespTitle = createP("Hue Spread"); | ||
huespTitle.position(xpos, ypos+30); | ||
xpos += 80; | ||
|
||
huespSlider = createSlider(0, 100, 15, 0); | ||
huespSlider.position(xpos, ypos+30); | ||
huespSlider.size(80); | ||
xpos += 100; | ||
|
||
alphaTitle = createP("Transparency"); | ||
alphaTitle.position(xpos, ypos+30); | ||
xpos += 90; | ||
|
||
alphaSlider = createSlider(0, 1, 0.75, 0); | ||
alphaSlider.position(xpos, ypos+30); | ||
alphaSlider.size(80); | ||
xpos += 100; | ||
} | ||
|
||
function clearButtonClicked() { | ||
background(97); | ||
} |
Oops, something went wrong.