{"id":16588,"date":"2017-10-03T10:38:42","date_gmt":"2017-10-03T14:38:42","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?page_id=16588"},"modified":"2022-10-11T08:21:11","modified_gmt":"2022-10-11T12:21:11","slug":"lab-week-7","status":"publish","type":"page","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/lab-week-7\/","title":{"rendered":"Lab Week 7"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><p><\/p>\n\n\n<h2>Randomness &amp; Objects<\/h2>\n<h3>Learning Objectives<\/h3>\n<ul><li>Use the noise() function to generate a random signal (e.g. the stock market value over a period of time).<\/li>\n<li>Determine when the stock market is rising and falling and color code this clearly.<\/li>\n<li>Make the stock market plot scroll to show it evolving over time.<\/li>\n<li>Create an array of objects to simulate a fish tank.<\/li>\n<\/ul><h3>Participation<\/h3>\n<p>In this lab\/recitation, you will write some p5.js programs that use the techniques you&rsquo;ve learned in class so far. The goal here is not just to get the programs done as quickly as possible, but also to help your peers if they get stuck and to discuss alternate ways to solve the same problems. You will be put into breakout rooms in Zoom to work on your code and then discuss your answers with each other, or to help each other if you get stuck. Then we will return to discuss the results together as a group.<\/p>\n<p>For each problem, you will start with a copy of the uncompressed&nbsp;<a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2019\/08\/template-p5only.zip\">template-p5only.zip<\/a>&nbsp; in a folder named&nbsp;<strong>lab-07<\/strong>. Rename the folder as&nbsp;<strong><em>andrewID<\/em>-07-A<\/strong>,&nbsp;<strong><em>andrewID<\/em>-07-B<\/strong>, etc. as appropriate.<\/p>\n<h3>A. Simple Stock Market Tracker<\/h3>\n<p>Write a p5.js program that simulates a stock market tracker that show the stock market &ldquo;price&rdquo; as shown below. Note that you will be using the noise() function to generate the values, so you won&rsquo;t get the exact same picture, but you will get something that looks like this in principle.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-60399\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/simplestockticker-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/simplestockticker-300x300.png 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/simplestockticker.png 800w\" sizes=\"(max-width: 300px) 85vw, 300px\"><\/p>\n<p>You should program this for any general canvas size, but the canvas size in this exercise will be 400 x 400.<\/p>\n<p>Start by defining a global variable <strong>marketvalue<\/strong> for an array of market values, initially empty. Also define a global variable <strong>noiseParam<\/strong> initialized to 0 to use for the noise() function later and a global variable <strong>noiseStep<\/strong> initialized to 0.1 to use to update the noiseParam for each subsequent value you generate. See the Perlin examples from Lecture 15 for guidance.<\/p>\n<p>For your <strong>setup<\/strong> function, create the canvas and set the stroke weight to 5. Next, run a loop that creates a new market value using the noise() function and store it in the next available position in the array. You should generate width\/5 + 1 values. For a 400 x 400 canvas, you&rsquo;d generate 81 values. To generate each value inside your loop:<\/p>\n<ol><li>Generate a random value using the <strong>noise<\/strong> function with <strong>noiseParam<\/strong> as its argument. Store this in the variable <strong>n<\/strong>. Remember that <strong>n<\/strong> will be between 0 and 1.<\/li>\n<li>Use the <strong>map<\/strong> function to map <strong>n<\/strong> from the range 0 to 1 to the range 0 to height. Store this value in the variable <strong>value<\/strong>.<\/li>\n<li>Append <strong>value<\/strong> to your array <strong>marketvalue<\/strong> using the <strong>push<\/strong> function.<\/li>\n<li>Add <strong>noiseStep<\/strong> to <strong>noiseParam<\/strong> to prepare it for the next iteration.<\/li>\n<\/ol><p>For your <strong>draw<\/strong> function, set the background to blue and the stroke to white. Then write a for loop that draws a line between each pair of adjacent values in the <strong>marketvalue<\/strong> array. Each line will have a width of 5 pixels:<\/p>\n<p style=\"padding-left: 40px;\">The first line will start at x = 0, y = marketvalue[0]<br>and end at x = 5, y = marketvalue[1].<br>The next line will start at x = 5, y = marketvalue[1]<br>and end at x = 10, y = marketvalue[2].<br>The next line will start at x = 10, y = marketvalue[2]<br>and end at x = 15, y = marketvalue[3];<br>and so on&hellip;<br>The last line will start at x = 395, y = marketvalue[79]<br>and end at x = 400, y = marketvalue[80].<br>(Look for the pattern here!)<\/p>\n<p>(Once you have this working, experiment with noiseStep. What happens if you increase it to 0.5? What happens if you decrease it to 0.05? Why?)<\/p>\n<h3>B. Improved Stock Market Tracker<\/h3>\n<p>Once you have a working solution for problem A, copy this project into a new project for problem B.&nbsp;Modify the draw function in your p5.js program so that it shows lines going upward (or level) as green lines and lines going downward as red lines. Note that your canvas is upside-down (y increases downward), the values stored in your array will be treated opposite to what they are numerically. In other words, if the next value in the array goes up numerically, then your line plot will go down since y increases from top to bottom.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-60400\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/improvedstockticker-300x300.png\" alt=\"\" width=\"300\" height=\"300\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/improvedstockticker-300x300.png 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/improvedstockticker.png 800w\" sizes=\"(max-width: 300px) 85vw, 300px\"><\/p>\n<p>Once you have that working, try to make a moving stock market tracker! To do this, before you draw the market value lines in the <strong>draw<\/strong> function, remove the first value in the <strong>marketvalue<\/strong> array (hint: use <strong>shift<\/strong>) and then append a single new noise value on to the array like you did in <strong>setup<\/strong>.<\/p>\n<h3>C. Fishes as Objects<\/h3>\n<p>For this final exercise, you will recreate your aquarium by creating an array of fish objects. You will then draw them as you did before, but you will access the data for each fish using array and object notation. This exercise is very similar to the one done in lecture 16 for Many Boxes.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-medium wp-image-59612\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/manyfish-300x200.png\" alt=\"\" width=\"300\" height=\"200\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/manyfish-300x200.png 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/manyfish.png 600w\" sizes=\"(max-width: 300px) 85vw, 300px\"><\/p>\n<p>To get started, start a new sketch with the following code from our previous lab:<\/p>\n<p><a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2020\/10\/fishtank.js\">fishtank<\/a><\/p>\n<p>Start by replacing the original four arrays with just one array named <strong>fish<\/strong> to represent an array of fishes, initially empty.<\/p>\n<p>In the <strong>setup<\/strong> function, update the body of the loop so that you create a new object for the i<sup>th<\/sup> fish and then you set the x, y, dx and c fields for that fish to the indicated random values. (Look at the example for lecture for the general idea!)<\/p>\n<p>in the <strong>draw<\/strong> function, update the body of the loop so that you call the function <strong>draw_fish<\/strong> with the i<sup>th<\/sup> fish, fish[i]. You don&rsquo;t need to pass all of the values now (x,&nbsp; y, dx and c) since they are contained in the fish object! For the rest of the code, update the references to the x and dx values, remembering that these are stored in fish[i] now. So instead of writing x[i], you would write fish[i].x to get to its x value.<\/p>\n<p>In the <strong>draw_fish<\/strong> function, replace the four parameters with just one, f, representing a generic fish. Remember that the <strong>draw<\/strong> function will call this function with fish[i], a specific fish. For this function, we only need a general name to represent that fish, f. Then update the code in this function to access x, y, dx and c through object f using dot notation.<\/p>\n<p>Once you&rsquo;re done, it should run the same as in lab 6!<\/p>\n<h2>Handin<\/h2>\n<div class=\"entry-content\">\n<p>At the end of the lab, zip the&nbsp;<strong>lab-07<\/strong> folder (whatever you got done) and submit it to Autolab. Do not worry if you did not complete all of the programming problems but you should have made it through problems A and B in some form, and you should have reached problem C. If you did not complete all of the lab exercises, be sure to try to complete them in the next day or so since these will help you with the next deliverable!<\/p>\n<\/div><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Randomness &amp; Objects Learning Objectives Use the noise() function to generate a random signal (e.g. the stock market value over a period of time). Determine when the stock market is rising and falling and color code this clearly. Make the stock market plot scroll to show it evolving over time. Create an array of objects &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/lab-week-7\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lab Week 7&#8221;<\/span><\/a><\/p>\n","protected":false},"author":535,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/16588"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/users\/535"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/comments?post=16588"}],"version-history":[{"count":25,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/16588\/revisions"}],"predecessor-version":[{"id":61526,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/16588\/revisions\/61526"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=16588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}