xxxxxxxxxx
122
function setup() {
// remember to set the angle mode that rotating is more natural for us
angleMode(DEGREES);
// we'll stop animation, otherwise our toast will be all jittery
noLoop()
createCanvas(800, 800);
}
// some useful functions
function randomInteger (min, max) {
return Math.floor(min + ((max-min) * Math.random()))
}
function pick (inputArray) {
return inputArray[randomInteger(0,inputArray.length)]
}
function buildArray (n, fillFunction) {
let outputArray = [];
for (let i = 0; i < n; i++) {
outputArray.push(fillFunction(i))
}
return outputArray
}
class Point {
constructor(x,y) {
this.x = x;
this.y = y;
}
move(xDistance, yDistance) {
return new Point(this.x + xDistance, this.y + yDistance)
}
}
// some functions for ToastOrders
function howMuchToast (number) {
console.log("Today, I'll have " + number + " pieces of toast for breakfast!");
return number
}
class ToastOrder {
constructor (name, quantity, type) {
this.name = name;
this.quantity = howMuchToast(quantity);
this.type = type
}
}
function totalToast3 (toastArray) {
let total = 0;
toastArray.forEach(x => total = total + x.quantity);
return total
}
// renaming these to show they are different from the Toast objects created below
let kateToastOrder = new ToastOrder ('kate', 2, 'regular');
let ellaToastOrder = new ToastOrder ('ella', 3, 'regular');
let lynnToastOrder = new ToastOrder ('lynn', 1, 'light');
let toastOrders = [kateToastOrder, ellaToastOrder, lynnToastOrder];
let total = totalToast3 (toastOrders);
console.log(total)
// now to draw the toast using our Toast class from before
class Toast {
constructor(size, tColor, point) {
this.toastSize = size;
this.toastColor = tColor;
this.xPosition = point.x;
this.yPosition = point.y;
}
draw() {
fill(color(this.toastColor));
// let's randomly rotate the toast so that it's cute
// remember to translate before you rotate or the toast is going to be in a crazy place
translate(this.xPosition, this.yPosition)
// we'll use pick here so that we can rotate either right or left randomly
rotate(15 * pick([1,-1]) * Math.random())
rect(0,0, this.toastSize, this.toastSize);
resetMatrix()
}
}
// we make points for each order based on the objects above
let katePoints = buildArray (kateToastOrder.quantity, i => new Point(100 + (i*40), 110))
let kateToast = buildArray (kateToastOrder.quantity, i=> new Toast (20, 'peru', katePoints[i]))
let ellaPoints = buildArray (ellaToastOrder.quantity, i => new Point(100 + (i*40), 170))
let ellaToast = buildArray (ellaToastOrder.quantity, i=> new Toast (20, 'peru', ellaPoints[i]))
let lynnPoints = buildArray (lynnToastOrder.quantity, i => new Point(100 + (i*40), 230))
let lynnToast = buildArray (lynnToastOrder.quantity, i=> new Toast (20, 'khaki', lynnPoints[i]))
function draw() {
background(220);
textSize(40);
fill('black')
// we can use the total that we calculated above!
text('toast orders: ' + total, 30, 70);
textSize(20);
// these are the labels
text('kate:', 30, 130);
text('ella:', 30, 190);
text('lynn:', 30, 250);
// here we draw the toast!
kateToast.forEach(t => t.draw())
ellaToast.forEach(t => t.draw())
lynnToast.forEach(t => t.draw())
}