Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
p5js-week-07 [2022/06/11 07:36] renickp5js-week-07 [2023/06/26 00:48] (current) reina.chen
Line 56: Line 56:
 Let's review the really big ideas first! Let's review the really big ideas first!
  
-  - encapsulation +  - **encapsulation** 
-  - generalization+  - **generalization**
  
-When we make a function which contains a procedure, we are using encapsulation.+When we make a function which contains a procedure, we are using <color black/pink>encapsulation</color>.
  
 <code>  <code> 
Line 71: Line 71:
 </code> </code>
  
-We studied procedures and encapsulation here: +When we add arguments to our function so that the function can be used in more cases, we are using <color black/pink>generalization</color>.
- +
-When we add arguments to our function so that the function can be used in more cases, we are using generalization.+
  
 <code>  <code> 
Line 85: Line 83:
 </code> </code>
  
-We learned about generalization here:  +We are also using <color black/pink>generalization</color> when we structure our data into a class and use a constructor with arguments.
- +
-We are also using generalization when we structure our data into a class and use a constructor with arguments.+
  
 <code> <code>
Line 109: Line 105:
  
 ==== assigning variables ==== ==== assigning variables ====
 +
 +{{:two-pieces-of-toast.png?600|}}
 +
 +(photo from https://www.flickr.com/photos/91261194@N06/51708375114)
  
 Remember, variables are names that we can give to pieces of data, like this: Remember, variables are names that we can give to pieces of data, like this:
Line 130: Line 130:
 ====making functions, including using arrow functions==== ====making functions, including using arrow functions====
  
-We make functions starting with the "function" keyword, followed by the function name, then the arguments, and finally the body of the function inside of curly braces.+We make functions starting with the <color black/yellow>"function"</color> keyword, followed by the function name, then the arguments, and finally the body of the function inside of curly braces.
  
-Remember, the arguments are the input to our function:+Remember, the <color black/pink>arguments</color> are the input to our function:
  
 <code> <code>
Line 141: Line 141:
 </code> </code>
  
-Don't forget the return to get some output from the function!+Don't forget the <color black/yellow>return</color> to get some output from the function!
  
-We can also make functions in the arrow function style. One way is to assign it to a variable:+We can also make functions in the <color black/pink>arrow</color> function style. One way is to assign it to a variable:
  
 <code> <code>
Line 167: Line 167:
 howMuchToast3(numberOfPiecesOfToast) howMuchToast3(numberOfPiecesOfToast)
 </code> </code>
 +
 +All of these functions can tell us how much toast we're having for breakfast.
 +
 +{{:beautiful-breakfast_toast.jpg?600|}}
 +
 +(photo from https://pxhere.com/en/photo/872146)
  
 ====various operators==== ====various operators====
  
-We've practice several operators like all of the ones for arithmetic (+, -, *, / ), and the remainder operator (%) and increment operator (++) as well.+We've practice several operators like all of the ones for arithmetic (+, -, *, / ), and the <color black/pink>remainder operator</color> (%) and <color black/pink>increment operator</color> (++) as well.
  
 We can use them to do math anywhere, like this: We can use them to do math anywhere, like this:
Line 180: Line 186:
 ====using booleans (true and false)==== ====using booleans (true and false)====
  
-One operator that we learned about when we studied for-loops is the  > (greater than) operator. There are also the < (less than) operator, the >= (greater than or equal) operator and the <= (less than or equal) operator. There's also the == (equal) operator. Try these:+One operator that we learned about when we studied for-loops is the  <color black/pink>> (greater than) operator</color>. There are also the <color black/pink>< (less than) operator</color>, the <color black/pink>>= (greater than or equal) operator</color> and the <color black/pink><= (less than or equal) operator</color>. There's also the <color black/pink>== (equal) operator</color>. Try these:
  
 <code> <code>
Line 190: Line 196:
 ====structuring data using arrays, and using push, pop, and unshift to add and remove items from those arrays==== ====structuring data using arrays, and using push, pop, and unshift to add and remove items from those arrays====
  
-We can collect data and put it into structure. One very important structure is arrays. We use push and unshift to add data to arrays. Let's do that for everyone's breakfast orders:+We can collect data and put it into structure. One very important structure is <color black/pink>arrays</color>. We use <color black/pink>push</color> and <color black/pink>unshift</color> to add data to arrays. Let's do that for everyone's breakfast orders:
  
 <code> <code>
Line 206: Line 212:
 </code> </code>
  
-Don't forget that you can get items from an array using the indexes. Each position in an array has a number, starting with 0.+Don't forget that you can get items from an array using the <color black/pink>indexes</color>. Each position in an array has a number, starting with 0.
  
 <code> <code>
Line 216: Line 222:
 ====for-loops for repeating actions==== ====for-loops for repeating actions====
  
-We have learned how to make for-loops to do things repeatedly. A for-loop has a first part in parentheses that describe the conditions for the for-loop: what number to start at (a counter), how long to run, and what to do with that counter. A for-loop has a second part in curly braces that says what you want to do on each loop.+We have learned how to make for-loops to do things repeatedly. A <color black/pink>for-loop</color> has a first part in parentheses that describe the conditions for the for-loop: what number to start at (a <color black/pink>counter</color>), how long to run, and what to do with that counter. A for-loop has a second part in curly braces that says what you want to do on each loop.
  
 We can use one with the things we've made above. Let's find out how much toast we have to make to fill everyone's order. We'll use the for-loop to get the value at each index with the counter i. We can use one with the things we've made above. Let's find out how much toast we have to make to fill everyone's order. We'll use the for-loop to get the value at each index with the counter i.
Line 238: Line 244:
 ====using forEach instead of a for-loop==== ====using forEach instead of a for-loop====
  
-We can rewrite totalToast using forEach instead. Remember that forEach is a method to be used on arrays. Instead of the code inside the for-loop above, we can use an arrow function.+We can rewrite totalToast using <color black/pink>forEach</color> instead. Remember that forEach is a method to be used on arrays. Instead of the code inside the for-loop above, we can use an arrow function.
  
 <code> <code>
Line 250: Line 256:
 ====structuring data using objects==== ====structuring data using objects====
  
-We have another way to structure toast, not just as arrays. That is objects. Let's make an object for each toast order.+We have another way to structure toast, not just as arrays. That is <color black/pink>objects</color>. Let's make an object for each toast order.
  
-There are properties on the left, each of which has a value on the right. Remember to separate the properties with commas. +There are <color black/pink>properties</color> on the left, each of which has a value on the right. Remember to separate the properties with commas. 
  
 <code> <code>
Line 264: Line 270:
 ====making classes so that making objects is easier and making those objects more useful==== ====making classes so that making objects is easier and making those objects more useful====
  
-Since we want to make a lot of objects like this, we can make a class. A class is like a factory for objects.+Since we want to make a lot of objects like this, we can make a class. A <color black/pink>class</color> is like a factory for objects.
  
 <code> <code>
Line 276: Line 282:
 </code> </code>
  
-When we want to use the class, we call the constructor with the "new" keyword. We can make all of the objects and then put them in an array like this:+When we want to use the class, we call the constructor with the <color black/yellow>"new"</color> keyword. We can make all of the objects and then put them in an array like this:
  
 <code> <code>
Line 314: Line 320:
 </code> </code>
  
-**And that is a quick review of all of the JavaScript that we've learned!**+**And that is a quick review of all of the JavaScript that we've learned!** Now that we have a class for ToastOrders, we're ready to open a restaurant! 
 + 
 +{{:menu-toast.png?600|}} 
 + 
 +(photo from https://nypl.getarchive.net/media/daily-breakfast-held-by-mansion-at-buffalo-ny-coffee-shop-0a9467)
  
 ===== p5js review ===== ===== p5js review =====
 +
 +Now that we've reviewed how to code __the data of toast__, let's review how to program __a drawing of that toast__.
  
 Here's an example in p5js that uses **everything above plus a lot of the things that we've studied before about p5js and some tools we've made**. See if you can find each of these things in the code below. Here's an example in p5js that uses **everything above plus a lot of the things that we've studied before about p5js and some tools we've made**. See if you can find each of these things in the code below.
Line 345: Line 357:
 <iframe src="https://editor.p5js.org/renick/sketches/q_0zBGiPk" width=99% height=800></iframe> <iframe src="https://editor.p5js.org/renick/sketches/q_0zBGiPk" width=99% height=800></iframe>
 </HTML> </HTML>
- 
  
 ===== Now for something new: a shape function ===== ===== Now for something new: a shape function =====
 +
 +Let's get back to making random shapes, which we might used to make weird slices of toast.
  
 Now we have a Point class. Using that abstraction, let's make another abstraction, a function called shapeFromPoints, that takes an array of Points and draws a shape. Now we have a Point class. Using that abstraction, let's make another abstraction, a function called shapeFromPoints, that takes an array of Points and draws a shape.
Line 373: Line 386:
 You can see after you run this a few times that the shapes created aren't always great when the Points are random. You can think about some ways to solve this. Still, this will be easy to use if you specify the Points. You can see after you run this a few times that the shapes created aren't always great when the Points are random. You can think about some ways to solve this. Still, this will be easy to use if you specify the Points.
  
-This sometimes makes what is called a self-intersecting polygon+This sometimes makes what is called a __self-intersecting polygon__
  
 https://en.wikipedia.org/wiki/Polygon#Convexity_and_non-convexity https://en.wikipedia.org/wiki/Polygon#Convexity_and_non-convexity
Line 383: Line 396:
 ===== algorithms ===== ===== algorithms =====
  
-We want either convex or concave polygons. We can write a set of rules to give us the output we want. We know one word for this: __procedure__. Another word we can use to call such sets of rules is __"algorithm"__+We want either convex or concave polygons. We can write a set of rules to give us the output we want. We know one word for this: <color black/pink>procedure</color>. Another word we can use to call such sets of rules is <color black/pink>"algorithm"</color>
  
 https://simple.wikipedia.org/wiki/Algorithm https://simple.wikipedia.org/wiki/Algorithm
Line 399: Line 412:
 ===== turning our algorithm into an abstraction ===== ===== turning our algorithm into an abstraction =====
  
-We want an abstraction, a function that implements this algorithm, which means to actually put the algorithm into a function so that we can run it.+We want an <color black/pink>abstraction</color>, a function that implements this algorithm, which means to actually put the algorithm into a function so that we can run it.
  
 In this case, we need some components. We'll need to: In this case, we need some components. We'll need to:
Line 445: Line 458:
 </code> </code>
  
-Now we can put these together to make a function that give us a list of points to be used to make a non-intersecting polygon. Notice that it uses the map method for arrays. We studied that before here: +Now we can put these together to make a function that give us a list of points to be used to make a __non-intersecting polygon__. Notice that it uses the map method for arrays. 
- +
-https://renickbell.net/middleschool/doku.php?id=core:foreach +
- +
-https://renickbell.net/middleschool/doku.php?id=core:map&s[]=%2Amap%2A +
- +
-You can learn more about map here: +
- +
-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map+
  
 <code> <code>
Line 465: Line 470:
 pointsForConvexShape(new Point(200,200),6,50,100,30,330) pointsForConvexShape(new Point(200,200),6,50,100,30,330)
 </code> </code>
 +
 +===== map =====
 +
 +Like forEach, <color black/pink>map</color> is a method for arrays. The big difference is that map applies a function to each item in the array and then returns an array filled with the result of those function calls. Like forEach, it's common to use an arrow function with map. We often use it with a one-argument function. There are other possibilities which we might look at later.
 +
 +For example, if we have an array of numbers, we can do some math with those numbers and get a new array.
 +
 +<code>
 +let numberOfSlicesOfToast = [2,2,3,2,1];
 +
 +let lotsMoreToast = numberOfSlicesOfToast.map(t => t *10)
 +</code>
 +
 +That's a lot of toast, but it was pretty easy to get those numbers by using map. 
 +
 +You can learn more about map here:
 +
 +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
  
 ===== a full example ===== ===== a full example =====
Line 475: Line 498:
  
 ===== making your own class for your drawings ===== ===== making your own class for your drawings =====
 +
 +{{:avacado-toast.png?600|}}
 +
 +(photo from https://www.flickr.com/photos/160866001@N07/44019575945)
 +
 +Now you're ready to get back to your customized personal toast, uh, drawings...
  
 We've been working on these drawings: We've been working on these drawings:
  • p5js-week-07.1654958217.txt.gz
  • Last modified: 2 years ago
  • by renick