{"id":14168,"date":"2017-09-19T12:46:55","date_gmt":"2017-09-19T16:46:55","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/?page_id=14168"},"modified":"2022-09-27T08:31:12","modified_gmt":"2022-09-27T12:31:12","slug":"lab-week-5","status":"publish","type":"page","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/lab-week-5\/","title":{"rendered":"Lab Week 5"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><h2>Color Grids and Gradients<\/h2>\n<h3>Learning Objectives<\/h3>\n<ul><li>Practice using nested loops.<\/li>\n<li>Practice making image properties (location, color) depend on loop variables.<\/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-05<\/strong>. Rename the folder as&nbsp;<strong><em>andrewID<\/em>-05-A<\/strong>,&nbsp;<strong><em>andrewID<\/em>-05-B<\/strong>, etc. as appropriate.<\/p>\n<h3>A. One-dimensional Color Gradient<\/h3>\n<p>Write a p5.js sketch in the folder <em><strong>andrewID<\/strong><\/em><strong>-05-A<\/strong> that draws this picture using a <strong>for<\/strong> loop. Canvas is 600&times;400, circle width (and height, it&rsquo;s a circle after all) is 100. The color is close to black at the top and close to red at the bottom, but you can see in this picture that the color is not full black at the top, in this case because the center of the circle is at 50, not 0. For this exercise, you will write a single loop that sets the fill color with some amount of red, and have green and blue set to 0. The amount of red will depend on the iteration. For example, for the four circles below, you might set the red component to 10, 90, 170, 250.&nbsp; The amount of change in red should be constant (e.g. in this sequence, each value is 80 more than the previous value).<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-15238\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2017\/09\/colorcolumn.png\" alt=\"increasing red from top to bottom\" width=\"1202\" height=\"804\"><\/p>\n<h3>B. Nested Loops<\/h3>\n<p>Make a copy of your answer to exercise A into a new folder <em><strong>andrewID<\/strong><\/em><strong>-05-B<\/strong>. Add an inner loop to your previous program to create this image (or something close to it):<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-14169\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2017\/09\/colorgrid.png\" alt=\"\" width=\"600\" height=\"400\"><\/p>\n<p>Use the following code as a starting point to create the image above:<\/p>\n<pre class=\"lang:js decode:true\" title=\"Color Gradient Starter Code\">function setup() {\n    createCanvas(600, 400);\n    noStroke();\n}\n\nfunction draw() {\n    background(0);\n    drawGrid();\n    noLoop(); \n}\nfunction drawGrid() {\n  for (var y = 50; y &lt; height + 50; y += 100) {\n        for (var x = 50; x &lt; width + 50; x += 100) {\n            \/\/color gradient needs to be implemented\n            ellipse(x, y, 100, 100);\n        }\n    }\n}<\/pre>\n<p>In the image, the RED component of the RGB color increases vertically from about 0 to about 255 (e.g. 10 to 250 in steps of 80). The GREEN component increases horizontally from about 0 to about 255. (For example, you might increase the green component from left to right from 10 to 250 as well, but the increase for each column would not be 80; what would it be?).<\/p>\n<p>Modify the &ldquo;starter&rdquo; program to produce the image above. Use only multiplication, division and\/or addition to compute color values from x and y.<\/p>\n<h2>C. Generalizing gradient<\/h2>\n<p>Make a copy of your answer to exercise B into a new folder <em><strong>andrewID<\/strong><\/em><strong>-05-C<\/strong>. Modify your program to make the following starting image (circle diameter and spacing is 10). Challenge: Define a global variable (var size = 10; ) and see if you can remove all &ldquo;magic numbers&rdquo; (i.e. numbers that are tied to a specific spacing only) so that changing <strong>size<\/strong> will change the circle size and spacing.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-14170\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-content\/uploads\/2017\/09\/finegrid.png\" alt=\"\" width=\"603\" height=\"401\"><\/p>\n<p>Once you have this programmed, replace &ldquo;noLoop();&rdquo; with &ldquo;size = mouseX \/ 10 + 10;&rdquo; and see if it works! If you&rsquo;ve removed all of the &ldquo;magic numbers&rdquo;, then the grid of circles will scale larger or smaller as your mouse moves left and right with the color gradient adjusting to the number of circles drawn.<\/p>\n<h2>D. Challenge Problem: using maps<\/h2>\n<p><b>Highly recommended if you are comfortable with creating &ldquo;mappings&rdquo; or transformations from loop variable values to coordinates, colors, and sizes by scaling (multiplication) and shifting (addition\/subtraction).<\/b><\/p>\n<p>Make a copy of your answer to <strong>exercise C<\/strong> into a new folder <em><strong>andrewID<\/strong><\/em><strong>-05-D<\/strong>. Rewrite the nested loop program (the original color grid) using the <strong>map<\/strong> function to help with choosing the correct color value.<\/p>\n<p>ALSO: You can try to write a separate function to draw something other than a circle (but the same general size) and then have draw call this to draw each shape in the appropriate color.<\/p>\n<p><span style=\"color: inherit; font-size: 28px; font-weight: 900;\">Handin<\/span><\/p>\n<div class=\"entry-content\">\n<p>At the end of the lab, zip the&nbsp;<strong>lab-05<\/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, and you should have some attempt at problem C.<\/p>\n<\/div><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Color Grids and Gradients Learning Objectives Practice using nested loops. Practice making image properties (location, color) depend on loop variables. Participation 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, &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/lab-week-5\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lab Week 5&#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\/14168"}],"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=14168"}],"version-history":[{"count":15,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/14168\/revisions"}],"predecessor-version":[{"id":73179,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/pages\/14168\/revisions\/73179"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2022\/wp-json\/wp\/v2\/media?parent=14168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}