{"id":200,"date":"2015-09-30T04:58:45","date_gmt":"2015-09-30T04:58:45","guid":{"rendered":"http:\/\/www.zoebachman.net\/itp\/?p=200"},"modified":"2015-09-30T04:58:45","modified_gmt":"2015-09-30T04:58:45","slug":"icm-wk-4-here-we-go-again","status":"publish","type":"post","link":"http:\/\/www.zoebachman.net\/itp\/?p=200","title":{"rendered":"ICM, Wk 4: Here We Go Again"},"content":{"rendered":"<p>This week, our assignment was to take\u00a0a previous drawing and try to use the new object and function formatting to clean it up.<\/p>\n<p>I thought back to my <a href=\"http:\/\/zoebachman.net\/itp\/ICM\/bachman_week1\">Sol LeWitt<\/a> drawing from the first week and I wondered how I could transform the individual square (the sketch is comprised of 400 of them) to create an object that can have one of four interiors\u00a0\u2013 either a line is drawn diagonally from bottom left to top right, or diagonally from bottom right to top left, has both lines, or is blank.<\/p>\n<p>I started by conceptually thinking of the code.<\/p>\n<p>The overall square is 200\u00d7200, including 400 squares with lines or no lines. I\u2019m thinking I\u2019d write an object that would have variables for the parameter<\/p>\n<p>var square: {<br \/>\nx: 200<br \/>\ny: 100<br \/>\nw: 10<br \/>\nh: 10<\/p>\n<p>(parameters for first square in the upper right hand corner).<\/p>\n<p>and then I\u2019d execute the object in draw() \u2026and add a conditional statement so that it redraws the squares every 10 pixels, until it reaches the edge of the large rectangle, and repeat through the next 19 lines. So, something along the lines of the nested loops that\u00a0we explored last week.<\/p>\n<p>The next part would be to figure out how to\u00a0randomize what is drawn in the interior of\u00a0each of those 400 squares, or have it be blank. Would I use that mysterious array thing I\u2019ve heard about?<\/p>\n<p>I put all of those thoughts in an email to Shiffman. He wrote back and made a few modifications as well as suggesting some hints\u2026<\/p>\n<blockquote><p>To answer your question, I think what I might start out doing is create an object for an individual cell of the full grid, i.e.<\/p>\n<p>var cell = {<br \/>\nx: 0,<br \/>\ny: 0.<br \/>\nw: 10,<br \/>\nh: 10,<br \/>\ndir: 0<br \/>\n}<\/p>\n<p>You could have a variable like \u201cdir\u201d which could have a value of 0 or 1 depending on the line direction. Then you could add a function like:<\/p>\n<p>var cell = {<br \/>\nx: 0,<br \/>\ny: 0.<br \/>\nw: 10,<br \/>\nh: 10,<br \/>\ndir: 0,<br \/>\ndisplay: function() {<br \/>\nrect(this.x,this.y,this.w,this.h);<br \/>\nif (this.dir == 0) {<br \/>\n\/\/ line one way<br \/>\n} else {<br \/>\n\/\/ line other way<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>Try making this object and then changing dir to false manually and see if it works!<\/p>\n<p>Then you could have dir set randomly:<\/p>\n<p>function setup() {<br \/>\ncell.dir = floor(random(2));<br \/>\n}<\/p>\n<p>floor() converts the random decimal number to a whole number so you get only 0 or 1.<\/p>\n<p>Try to see if you can get this to work and then the next step is to make an array of these objects! Which we will discuss in class this week or I can help you with another e-mail or in person.<\/p><\/blockquote>\n<p>Taking Shiffman\u2019s suggestions, I wrote out the code as suggested, then went in to make the edits. I did some simple math to figure out the relationship between the lines and their corresponding rectangles:<\/p>\n<p>For rect(x, y, w, h) a line that goes from the [bottom right to top left] (R2L) would look like line1(x, y, x + 10, y + 10), and a line that went from the [bottom left to top right](L2R) would look like line2(x + 10, y, x, y + 10).<\/p>\n<p>Then in the draw() function I added a background and wrote cell.dispaly(); and voila! (I\u2019m starting to realize I use this in almost every blog post) \u2013 I have a square with a line!<\/p>\n<p>adding this line of code: cell.dir = floor(random(2)) to the setup() function got me a square with a line in it that would change every time!<\/p>\n<div id=\"attachment_214\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.00.59.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-214\" class=\"size-medium wp-image-214\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.00.59-300x188.png\" alt=\"My single square with a randomized line!\" width=\"300\" height=\"188\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.00.59-300x188.png 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.00.59-1024x640.png 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.00.59-1200x750.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-214\" class=\"wp-caption-text\">My single square with a randomized line!<\/p><\/div>\n<p>********<\/p>\n<p>So now, I wanted my squares\/cells in a grid\u202610 pixels apart, not exceeding 200 in width and height. I went back to the code my partner Xiqiao did last week to look at how he made his grid\u2026I added in his variables and the for loops and got this:<\/p>\n<div id=\"attachment_215\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.40.16.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-215\" class=\"size-medium wp-image-215\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.40.16-300x188.png\" alt=\"nested loop square grid!\" width=\"300\" height=\"188\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.40.16-300x188.png 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.40.16-1024x640.png 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-28-19.40.16-1200x750.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-215\" class=\"wp-caption-text\">nested loop square grid!<\/p><\/div>\n<p>I started to play around with it to try and get it to do what I wanted \u2013 basically, have the squares repeat 20 across and 20 down, starting from 200, 100. I don\u2019t want to bore you, but suffice to say I tried out a couple variations on conditional statements and loops. I thought an array might have something to do with it, so I (metaphorically) cracked open Getting Started with p5.js and went to the chapter on arrays\u2026and was pretty lost. I tried adding some code, but wasn\u2019t exactly sure what I was doing.<\/p>\n<p>I also tried to play around with creating constructor functions, which I read about in p5 but we hadn\u2019t gotten into yet. The more I looked at Getting Started, the more I wondered about the best way to create <strong>one thing\u00a0<\/strong>and then get it to repeat in a specific pattern.<\/p>\n<p>**********<\/p>\n<p>Tuesday morning I met with Shiffman to go over the code. He explained arrays more in depth and I finally realized that in order to get the repetition I wanted I would need a variable. We played around with parameters \u2013 how many squares, how far apart they would be, etc. We also converted the\u00a0original cell object into part of an array. We were also now able to add the floor() function to this portion of code, since that information is now in setup() (mostly a note to self: p5 doesn\u2019t recognize floor() if it comes before setup()).<\/p>\n<p>Now with a better understanding of arrays, Shiffman tasked me with trying to get a horizontal and a vertical line of cells \u2013 which I was able to do! Separately, that is. They also had been further apart when we tried out the code and I was able to line them up one after the other.<\/p>\n<div id=\"attachment_213\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.45.29.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-213\" class=\"size-medium wp-image-213\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.45.29-300x188.png\" alt=\"Horizontal line with randomized interior lines\" width=\"300\" height=\"188\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.45.29-300x188.png 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.45.29-1024x640.png 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.45.29-1200x750.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-213\" class=\"wp-caption-text\">Horizontal line with randomized interior lines<\/p><\/div>\n<p>https:\/\/gist.github.com\/zoebachman\/98d408dcffa415903b6b<\/p>\n<div id=\"attachment_212\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.44.05.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-212\" class=\"size-medium wp-image-212\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.44.05-300x188.png\" alt=\"Vertical line with randomized interior lines\" width=\"300\" height=\"188\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.44.05-300x188.png 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.44.05-1024x640.png 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.44.05-1200x750.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-212\" class=\"wp-caption-text\">Vertical line with randomized interior lines<\/p><\/div>\n<p><script src=\"https:\/\/gist.github.com\/zoebachman\/8da51414b765b72bf426.js\"><\/script>I thought I\u2019d try and see what would happen if I had i*10\u00a0simultaneously\u00a0in x and y\u2026This, however, it gave me a diagonal line:<\/p>\n<div id=\"attachment_211\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.47.21.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-211\" class=\"wp-image-211 size-medium\" src=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.47.21-300x188.png\" alt=\"Diagonal line with randomized interior lines\" width=\"300\" height=\"188\" srcset=\"http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.47.21-300x188.png 300w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.47.21-1024x640.png 1024w, http:\/\/www.zoebachman.net\/itp\/wp-content\/uploads\/2015\/09\/Screenshot-2015-09-29-23.47.21-1200x750.png 1200w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-211\" class=\"wp-caption-text\">Diagonal line with randomized interior lines. I knew it couldn\u2019t have been that easy.<\/p><\/div>\n<p><script src=\"https:\/\/gist.github.com\/zoebachman\/8e5ca7e86022a39f1e37.js\"><\/script><\/p>\n<p>I attempted to do another conditional statement, but with a new variable (j) and then repeating the array\/object block again but switching all the i to j.<\/p>\n<p><script src=\"https:\/\/gist.github.com\/zoebachman\/294d1798337af609501e.js\"><\/script><\/p>\n<p>I got <strong>30: Uncaught RefrenceError: floor is not defined<\/strong>. Can I not use floor more than once?<\/p>\n<p>It was clear that this additional array\/variable did not make for happy code. So, since it\u2019s 11:59, I\u2019ll stop my tinkering and clean up this blog post. The hope is that next week I\u2019ll have a fully functioning sketch!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, our assignment was to take\u00a0a previous drawing and try to use the new object and function formatting to clean it up. I thought back to my Sol LeWitt drawing from the first week and I wondered how I &hellip; <a href=\"http:\/\/www.zoebachman.net\/itp\/?p=200\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[53,55,54,11,12],"class_list":["post-200","post","type-post","status-publish","format-standard","hentry","category-icm","tag-array","tag-function","tag-object","tag-p5","tag-sol-lewitt"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/200","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":4,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":217,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/217"}],"wp:attachment":[{"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zoebachman.net\/itp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}