Binary Trees
For an introduction to binary trees, see Data Structures: Introduction to Trees
Computing outside the canvas: p5.js and web pages.
The DOM stands for Document Object Model. The idea is that the entire web page is processed to create nested objects: Every heading, paragraph, table, image, and list, originally in HTML, is accessible as an object via p5.js and JavaScript programming.
To access “the DOM” from p5.js, you need the “all” template, which includes p5.dom.js
, an extension of the core p5.js functions that includes new functions to access DOM objects.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.12, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/addons/p5.dom.js"></script> <!-- Uncomment the lines below to include extra p5 libraries, or use template-all or template-all-min: <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/addons/p5.sound.js"></script> --> <script src="ex1.js" type="text/javascript"></script> </head> <body> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// ex1.js - put canvas in a variable and move it around (load ex1.html) var myCanvas; function setup() { myCanvas = createCanvas(80, 80); } function draw() { background(255); myCanvas.position(noise(millis() / 2000) * 200, noise(10 + millis() / 2000) * 200); fill("red"); rect(0, 0, 50, 50); } |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.12, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/addons/p5.dom.js"></script> <!-- Uncomment the lines below to include extra p5 libraries, or use template-all or template-all-min: <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.12/addons/p5.sound.js"></script> --> <script src="ex2.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// ex2.js - rotating box in a canvas over HTML text (load ex2.html) var myCanvas; function setup() { myCanvas = createCanvas(80, 80); } function draw() { background(255); myCanvas.position(noise(millis() / 2000) * 200, noise(10 + millis() / 2000) * 200); fill("red"); translate(40, 40); rotate(millis() / 1000); rect(-25, -25, 50, 50); pop(); } |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex3.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// ex3.js - a little animated banner (load ex3.html) // Note: a little animated gif would have much less baggage var myCanvas; function setup() { myCanvas = createCanvas(400, 10); myCanvas.parent('aPlaceForCanvas'); } function draw() { background(255); stroke("red"); strokeWeight(2); var offset = int(millis() / 100) % 15; for (var i = -15; i < width; i = i + 15) { line(i + offset, 0, i + height + offset, height); } } |
Example 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex4.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// ex4.js - mouse-tracking eyes (load ex4.html) var myCanvas; function setup() { myCanvas = createCanvas(200, 100); myCanvas.parent('aPlaceForCanvas'); } function draw() { background(255); for (i = 0; i < 2; i++) { // 2 eyes var ex = i * 100; // eye x offset fill(255); var x = 45 + ex; // eye center x var y = 50; // eye center y ellipse(x, y, 80, 60); var dx = mouseX - x; // dx, dy goes from eye to mouse var dy = mouseY - y; var d = dist(x, y, mouseX, mouseY); // distance to mouse d = max(30, d); // avoid divide by zero dx = dx / d; // now dx, dy goes from 0 to 1 in direction of mouse dy = dy / d; dx = dx * 25; // scale dx, dy roughly to size of eye dy = dy * 15; fill(0); // draw the pupil ellipse(x + dx, y + dy, 30, 30); } } |
Example 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex5.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div align="center" id="aPlaceForCanvas"></div> <ol> <li>Music was considered by Pythagoras to be a branch of mathematics closely related to Arithmetic</li> <li>Described by Plato in The Republic as the four arts: Arithmetic, Music, Geometry, Astronomy</li> <li>Later formalized as the Quadrivium</li> <li>Music theory has always had a musical aspect</li> <li>Today, music composing, printing, recording, synthesizers … all based on digital information processing</li> </ol> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// ex5.js - creating a DIV element (load ex5.html) var myCanvas; function setup() { myText = createDiv("<img src='http://fla.fg-a.com/aliens/flying-alien.gif'><b><h2>All your base are belong to us!</h2>"); } function draw() { myText.position(noise(millis() / 2000) * 400 - 200, noise(10 + millis() / 2000) * 600 - 200); } |
Example 6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html> <head> <meta charset="UTF-8"> <title>p5.js vers 0.5.2, Edit index.html to Change This Title</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex6.js" type="text/javascript"></script> </head> <body> Mankind has always been fascinated by mechanical simulations of human activity. A characteristic of Western thought is to understand our minds, bodies, and our world in mechanistic terms. Music especially has been viewed as a mathematical and mechanical process. As a result, musicians, scientists, and engineers have devised all sorts of mechanisms and systems for composing and performing music. I will review some or these activities their evolution over the last 2000 years, ending with some recent findings on how a human audience relates to humanoid robot performers and how music can be composed by computers. <br><br> <div id="aPlaceForText"></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// ex6.js - inserting computed text into HTML (load "ex6.html") var myCanvas; function setup() { myText = createDiv("Some Text"); myText.parent("aPlaceForText"); myText.style("font-size", "18pt"); } function draw() { var s = "The mouse is at location " + int(mouseX) + ", " + int(mouseY); myText.elt.innerHTML = s; } |
Example 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<html> <head> <meta charset="UTF-8"> <title>Example 7 - onmouseover</title> <script src="http://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.dom.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.2/addons/p5.sound.js"></script> <script src="ex7.js" type="text/javascript"></script> </head> <body> <big> <big> <big> <!-- other events are onchange, onmouseover, onclick, onkeypress --> <b onmouseover="apple()">Apple</b> <b onmouseover="duck()">Duck</b> <b onmouseover="car()">Car</b> </big> </big> </big> <br> <div id="picture-here"></div> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
// ex6.js - inserting computed text into HTML (load "ex6.html") // NOTE: to run this code, you'll probably need a local server AND // you will need to provide images: apple.jpg, duck.jpg, car.jpg var myCanvas; var appleImg; var duckImg; var carImg; var showImg; function preload() { appleImg = loadImage("apple.jpg") duckImg = loadImage("duck.jpg") carImg = loadImage("car.jpg") } function setup() { myCanvas = createCanvas(300, 300); myCanvas.parent("picture-here"); showImg = false; noLoop(); } function draw() { background(255); } function apple() { background(255); image(appleImg, 0, 0); } function duck() { background(255); image(duckImg, 0, 0); } function car() { background(255); image(carImg, 0, 0); } |
Summary
What do the previous examples teach us? Here are some important points and concepts:
createCanvas
returns a value. A p5.js canvas is an object. You can usecanvas.position(x, y)
to position it anywhere on the page.- In HTML, the
div
tag can be used as a place-holder for content created by p5.js. Use<div id=aPlaceForCanvas></div>
to mark the document location of your canvas and, in p5.js,myCanvas.parent('aPlaceForCanvas');
to make thediv
contain the canvas - You can create a free-floating
div
with p5.js’screateDiv(content_string)
function call. The content string is just HTML, so it can be text, images, tables, whatever. You can position thisdiv
withtheDiv.position(x, y)
or usingtheDiv.parent("aPlace")
where some tag in the HTML has the tagid=aPlace
. - Once you create a
div
withmyDiv = createDiv(content_string);
, you can change it usingmyDiv.elt.innerHTML = "any string of HTML";
3D Graphics
Includes most concepts of 2D drawing, but adds a lot more:
- the Z axis of course
- camera position and angle
- lighting
- surface color, texture, and how it reflects light (specular, diffuse)
- translation and rotation are 3D: you can rotate around any one of 3 axes.
See examples in p5.js documentation.
Another example:
sketch
var boxes;
var zlimit = -4000;
function randomColor() {
return color(random(255), random(255), random(255));
}
function setup() {
createCanvas(600, 400, WEBGL);
boxes = [];
for (var i = 0; i < 1000; i++) {
boxes.push({x: random(-100, 100),
y: random(-100, 100),
z: random(zlimit, 300),
color: randomColor()});
}
frameRate(5); // to reduce cpu load; looks better without this
}
function draw() {
background(230);
ambientLight(100, 100, 200);
pointLight(255, 100, 100, 255, 300, 0, 0);
pointLight(100, 255, 100, 255, 0, 300, 0);
orbitControl();
for (var i = 0; i < boxes.length; i++) {
var b = boxes[i];
push();
translate(-200, 0, b.z);
rotateZ(b.z * 0.005);
translate(b.x + 200, b.y, 0);
b.z -= 4;
ambientMaterial(b.color);
rotateX(b.z * 0.02);
box(15, 20, 25);
pop();
if (b.z < zlimit) {
b.z = 300;
}
}
}
Machine Learning
I gave an overview of machine learning. Some important concepts are:
- ML most commonly refers to supervised learning where we give a learner many examples of input/output, and the learner tries to learn a function that produces the correct output from new (previously unseen) input.
- Input and output are almost always vectors (arrays) of numbers. So we learn functions from numbers to numbers.
- A classifier learns to identify the class of an object given a set of numerical features. E.g. detecting digits 0 through 9 is a problem to classify features (pixel values) as class 0, class 1, class 2, etc.
- Machine learning programs usually start with a well-defined model of computation, e.g. a “neural network” and adjust parameters or weights to achieve the desired function
I used slides based on “Machine Learning is Fun” — it’s pretty good reading for a high-level overview of ML.
We also saw an application of real-time image classification used to control a drum machine.
The following are some “leftovers” — I think we covered Perlin noise earlier in the semester, so we probably won’t talk about these in class this week.
Perlin Noise Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
// noise.js - noise study var myCanvas; var loc = 0; function setup() { createCanvas(500, 300); } function draw() { background(250); stroke(0); strokeWeight(0.5); // draw the first graph of Perlin noise // the parameter to the noise function is the X coordinate scaled // by a scale factor from 0-1, thus we're accessing noise() using // ranges from 0-0 (mouse all the way left) to 0-width (mouse all // the way right) for (i = 0; i < width; i++) { line(i, 30 + 100 * noise(i * mouseX / width), i + 1, 30 + 100 * noise((i + 1) * mouseX / width)); } // the second graph is the same Perlin noise, but it is shifted by 100 pixels // by accessing the noise() function with a large enough offset, we access // different "random" values that are seemingly independent. for (i = 0; i < width; i++) { line(i, 180 + 100 * noise((i + 100) * mouseX / width), i + 1, 180 + 100 * noise((i + 101) * mouseX / width)); } // draw a cursor to show where we are accessing the function loc = (loc + 1) % width; stroke("red"); line(loc, 0, loc, height); noStroke(); // move blue balls according to the noise value a the red cursor fill("blue"); ellipse(30, 30 + 100 * noise(loc * mouseX / width), 30, 30); ellipse(30, 180 + 100 * noise((loc + 100) * mouseX / width), 30, 30); fill(0); textSize(20); text("Scale factor: " + nf(mouseX / width, 1, 2), 10, 30); } |
2D Perlin Noise Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// noise2.js - noise study var myCanvas; function setup() { createCanvas(600, 600); } var SCALE = 0.01; function draw() { background(250); fill("blue"); for (i = 0; i < 600; i += 30) { for (j = 0; j < 600; j += 30) { var d = noise(i * SCALE, j * SCALE) * 50; ellipse(i, j, d, d); } } } |