{"id":7534,"date":"2016-11-28T04:43:42","date_gmt":"2016-11-28T09:43:42","guid":{"rendered":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/?page_id=7534"},"modified":"2021-11-23T11:51:31","modified_gmt":"2021-11-23T16:51:31","slug":"week-13-lab","status":"publish","type":"page","link":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/week-13-lab\/","title":{"rendered":"Lab Week 13"},"content":{"rendered":"<!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD HTML 4.0 Transitional\/\/EN\" \"http:\/\/www.w3.org\/TR\/REC-html40\/loose.dtd\">\n<html><body><h3>Learning Objectives<\/h3>\n<ul><li>Implement a recursive function to draw a Sierpinski triangle.<\/li>\n<li>Draw a set of nine cubes in three-dimensional space as a 3 X 3 X 3 set of cubes.<\/li>\n<li>Draw a Rubik&rsquo;s Cube in three-dimensional space by creating 9 cubes using six faces each of different colors.<\/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\/f2021\/wp-content\/uploads\/2019\/08\/template-p5only.zip\">template-p5only.zip<\/a>&nbsp; in a folder named&nbsp;<strong>lab-13<\/strong>. Rename the folder as&nbsp;<strong><em>andrewID<\/em>-13-A<\/strong>,&nbsp;<strong><em>andrewID<\/em>-13-B<\/strong>, etc. as appropriate.<\/p>\n\n\n<h3>A. Sierpinski Triangle<\/h3>\n\n\n\n<p><img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-63986\" style=\"width: 400px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski.png\" alt=\"\"><\/p>\n\n\n\n<p>This is an example of a fractal, an image that is self-similar. When you look at parts of the image, you see the original image. <\/p>\n\n\n\n<p>One of the most famous fractals is the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.mathsisfun.com\/sierpinski-triangle.html\" target=\"_blank\">Sierpinski Triangle<\/a> which is a triangle that is divided up into three triangles which are divided up into three triangles each, which are divided up into&hellip; you get the idea.<\/p>\n\n\n\n<p>To draw this sketch, we start with a large triangle of blue. We then find the midpoints of each of the three sides and draw a triangle between these points in the background color. Finally, we repeat the midpoint process with each of the three remaining blue triangles that are formed when we draw the triangle in the background color. Each of these triangles is &ldquo;split&rdquo; into three smaller triangles. This repeats until we reach the number of levels of repetition.<\/p>\n\n\n\n<p><img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-64003\" style=\"width: 100px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski0.png\" alt=\"\">\n&nbsp;\n<img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-64002\" style=\"width: 100px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski1.png\" alt=\"\">\n&nbsp;\n<img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-64001\" style=\"width: 100px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski2.png\" alt=\"\">\n&nbsp;\n<img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-64000\" style=\"width: 100px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski3.png\" alt=\"\">\n&nbsp;\n<img loading=\"lazy\" width=\"800\" height=\"800\" class=\"wp-image-63999\" style=\"width: 100px;\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/12\/sierpinski4.png\" alt=\"\"><\/p>\n\n\n\n<p>Complete the function <strong>splitIntoThree<\/strong> based on the comments to guide you.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">var numLevels = 4;\n\nfunction setup() {\n    createCanvas(400, 400);\n    frameRate(10);\n    noStroke();\n}\n\nfunction draw() {\n    background(220);\n    fill(0, 0, 255);\n    triangle(200, 50, 350, 350, 50, 350);\n    splitIntoThree(numLevels, 200, 50, 350, 350, 50, 350);\n    noLoop();\n}\n\nfunction splitIntoThree(levels, x0, y0, x1, y1, x2, y2) {\n    \/\/ base case: if there are no more levels left to draw, we're done:\n\n\n    fill(220);   \/\/ background color\n    var x01 = midpt(x0, x1);   \/\/ midpoint of x between x0 and x1\n    var y01 = midpt(y0, y1);   \/\/ etc.\n    var x12 = midpt(x1, x2);\n    var y12 = midpt(y1, y2);\n    var x20 = midpt(x2, x0);\n    var y20 = midpt(y2, y0);\n\n    \/\/ draw a triangle using the midpoints:\n\n\n    \/\/ split each of the remaining blue triangles with one less level\n    \/\/ (hint: you should have three recursive calls here,\n    \/\/  each call will have a list of x and y points for one of the\n    \/\/  remaining blue triangles):\n\n\n\n\n}\n\nfunction midpt(a, b) {\n    return (a + b)\/2;\n}\n\n<\/pre>\n\n\n\n<h3>B. Cube of Cubes<\/h3>\n\n\n\n<p>Write a program that creates a 3 X 3 X 3 set of cubes. each of size 100 X 100 X 100 in three dimensional space. Remember that you need to use the <strong>WEBGL<\/strong> renderer, and that the camera is initially facing in the -z direction, with x increasing toward the right and y increased downward and the origin (0,0,0) is initially in the center of the canvas. Use the <strong>orbitControl<\/strong> function in the draw function to allow you to use the mouse to move around the set of cubes once you&rsquo;re done to check your work. Here is a picture of the expected result, once the mouse is used to move around in 3D space a little.<\/p>\n\n\n\n<figure class=\"wp-block-image size-medium\"><img loading=\"lazy\" width=\"300\" height=\"300\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-300x300.jpg\" alt=\"\" class=\"wp-image-69189\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-300x300.jpg 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-1022x1024.jpg 1022w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-150x150.jpg 150w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-768x769.jpg 768w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes-1200x1202.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/cubeofcubes.jpg 1208w\" sizes=\"(max-width: 300px) 85vw, 300px\"><\/figure><p>You should use the <strong>box<\/strong> function to create each cube, remembering that this function will draw the shape centered at the origin, so to draw all nine boxes, you will need to use transformations to move the origin. <\/p>\n\n\n\n<p><strong>LOOK FOR PATTERNS HERE!<\/strong> Where is the center of each of the 9 cubes? Do you see the pattern? You should be able to use a set of three nested loops to solve this problem, and if so, you will only need to write a call to the <strong>box<\/strong> function once.<\/p>\n\n\n\n<h3>C. Rubik&rsquo;s Cube<\/h3>\n\n\n\n<p>Make a copy of the code for the previous problem and modify the copy so that the 3 X 3 X 3 cube you draw looks like a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Rubik%27s_Cube\" target=\"_blank\" rel=\"noreferrer noopener\">Rubik&rsquo;s Cube<\/a> in a solved state (ie. each side is all of one color). <\/p>\n\n\n\n<figure class=\"wp-block-image size-thumbnail\"><img loading=\"lazy\" width=\"150\" height=\"150\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-150x150.jpg\" alt=\"\" class=\"wp-image-69190\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-150x150.jpg 150w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-300x300.jpg 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-1022x1024.jpg 1022w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-768x769.jpg 768w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1-1200x1202.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view1.jpg 1208w\" sizes=\"(max-width: 150px) 85vw, 150px\"><\/figure><figure class=\"wp-block-image size-thumbnail\"><img loading=\"lazy\" width=\"150\" height=\"150\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-150x150.jpg\" alt=\"\" class=\"wp-image-69191\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-150x150.jpg 150w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-300x300.jpg 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-1022x1024.jpg 1022w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-768x769.jpg 768w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2-1200x1202.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view2.jpg 1208w\" sizes=\"(max-width: 150px) 85vw, 150px\"><\/figure><figure class=\"wp-block-image size-thumbnail\"><img loading=\"lazy\" width=\"150\" height=\"150\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-150x150.jpg\" alt=\"\" class=\"wp-image-69192\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-150x150.jpg 150w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-300x300.jpg 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-1022x1024.jpg 1022w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-768x769.jpg 768w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3-1200x1202.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view3.jpg 1208w\" sizes=\"(max-width: 150px) 85vw, 150px\"><\/figure><figure class=\"wp-block-image size-thumbnail\"><img loading=\"lazy\" width=\"150\" height=\"150\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-150x150.jpg\" alt=\"\" class=\"wp-image-69193\" srcset=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-150x150.jpg 150w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-300x300.jpg 300w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-1022x1024.jpg 1022w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-768x769.jpg 768w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4-1200x1202.jpg 1200w, https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2021\/11\/rubik-view4.jpg 1208w\" sizes=\"(max-width: 150px) 85vw, 150px\"><\/figure><p>First, add a global variable <strong>size<\/strong> that represents the size of the box so that you can use other values other than 100. Update your program to use size instead of 100.<\/p>\n\n\n\n<p>Now, think about drawing the 9 boxes in color. This is a bit more challenging than it might seem. When you draw each of 9 boxes, you need to color each side a different color,<\/p>\n\n\n\n<p><em>Front: white<br>Back: yellow<br>Left: red<br>Right: orange<br>Top: blue<br>Bottom: green<\/em><\/p>\n\n\n\n<p>but the <strong>box<\/strong> function won&rsquo;t let you do this.<\/p>\n\n\n\n<p>Instead, replace the <strong>box<\/strong> call in your <strong>draw<\/strong> function with a call to a separate function <strong>drawColorBox<\/strong> that you will write. In this helper function, you should draw a box centered at the origin that has 6 faces (but you won&rsquo;t use the <strong>box<\/strong> function anymore). Draw a wire view of a box on a piece of paper and determine the coordinates of the 8 corners. <em>HINT: Let sz2 be half the <strong>size<\/strong> variable; your coordinates should all be in terms of sz2.<\/em> To draw each face, you will <strong>fill<\/strong> with the appropriate color, and then use <strong>beginShape<\/strong>, <strong>vertex<\/strong> (4 times) with the appropriate coordinates and <strong>endShape<\/strong> to create a square. (You can&rsquo;t use the <strong>square<\/strong> function in 3D space since it doesn&rsquo;t have a z coordinate parameter.) You will need to draw 6 squares (faces) to make the box. <\/p>\n\n\n\n<p>Here&rsquo;s code for the front face to get you started:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fill(\"white\");\nbeginShape();\nvertex(-sz2, -sz2, sz2);\nvertex(-sz2,  sz2, sz2);\nvertex( sz2,  sz2, sz2);\nvertex( sz2, -sz2, sz2);\nendShape();<\/code><\/pre>\n\n\n\n<p>Your <strong>draw<\/strong> function will then call this <strong>drawColorBox<\/strong> function 9 times to create the 9 boxes. When you&rsquo;re done, you should be able to see something that looks like a Rubik&rsquo;s Cube. <\/p>\n\n\n\n<p>Change the size to 50 for a smaller cube. Use the mouse to look around the Rubik&rsquo;s Cube to make sure you correctly programmed all sides. If your mouse has a scroll wheel, scroll in and see what happens when you get too close.<\/p>\n\n\n\n<h3>D. Recursive Tree (Optional challenge)<\/h3>\n\n\n\n<p>Try to draw this picture recursively:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"800\" height=\"800\" src=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-content\/uploads\/2020\/11\/recursivetree.png\" alt=\"\" class=\"wp-image-63984\"><\/figure><h3>Handin<\/h3>\n\n\n\n<p>At the end of the lab, zip the&nbsp;<strong>lab-<\/strong>13&nbsp;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. Problem D is not required, but it is given for those who want a challenge.<\/p><\/body><\/html>\n","protected":false},"excerpt":{"rendered":"<p>Learning Objectives Implement a recursive function to draw a Sierpinski triangle. Draw a set of nine cubes in three-dimensional space as a 3 X 3 X 3 set of cubes. Draw a Rubik&rsquo;s Cube in three-dimensional space by creating 9 cubes using six faces each of different colors. Participation In this lab\/recitation, you will write &hellip; <a href=\"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/week-13-lab\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Lab Week 13&#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\/f2021\/wp-json\/wp\/v2\/pages\/7534"}],"collection":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/users\/535"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/comments?post=7534"}],"version-history":[{"count":33,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages\/7534\/revisions"}],"predecessor-version":[{"id":69206,"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/pages\/7534\/revisions\/69206"}],"wp:attachment":[{"href":"https:\/\/courses.ideate.cmu.edu\/15-104\/f2021\/wp-json\/wp\/v2\/media?parent=7534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}