{"id":304,"date":"2011-11-21T20:26:40","date_gmt":"2011-11-22T04:26:40","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=304"},"modified":"2011-11-21T20:26:40","modified_gmt":"2011-11-22T04:26:40","slug":"incremental-grid","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/11\/incremental-grid\/","title":{"rendered":"Incremental Grid"},"content":{"rendered":"<p><script type=\"application\/processing\">\/\/ Incremental Grid\n\nvoid setup()\n{\n  size(500,500);\n  noStroke();\n}\n\nvoid draw()\n{\n  int gridWidth = 25;\n  int cellWidth = width\/gridWidth;\n  int n = 0;\n  scale(cellWidth);\n  for (int y = 0; y < gridWidth; ++y) {\n    for (int x = 0; x < gridWidth; ++x) {\n      fill( map(sin( millis()*.00001*n ),-1,1,0,255) );\n      rect( x, y, 1, 1 );\n      ++n;\n    }\n  }\n}\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=incremental_grid\">source<\/a><\/div>\n<p>Here&#8217;s two nifty tricks:<\/p>\n<p>1) Using sin waves with incrementing frequencies to pulse each cell of a grid.  This is a square grid version of the animation in the <a href=\"http:\/\/wheelof.com\/whitney\/\">Whitney Music Box<\/a>.<\/p>\n<pre>\r\n      fill( map(sin( millis()*.00001*n ),-1,1,0,255) );\r\n<\/pre>\n<p><i>n<\/i> is a number which increases for each cell.  .00001 is used to control the speed, and millis() provides the impetus for the motion.  Since sin() goes from -1 to 1, I used map() to make it go from 0 to 255.<\/p>\n<p>It&#8217;s tempting to use frameCount for animations like this, but I usually use millis() because it makes the speed of the animation predictable, regardless of whatever framerate I&#8217;m managing to achieve.<\/p>\n<p>2) Each cell is drawn as if it is 1 pixel wide.  I use scale() to grow the whole thing to the desired size.<\/p>\n<pre>\r\n  scale(cellWidth);\r\n  for (int y = 0; y < gridWidth; ++y) {\r\n    for (int x = 0; x < gridWidth; ++x) {\r\n      fill( map(sin( millis()*.00001*n ),-1,1,0,255) );\r\n      rect( x, y, 1, 1 );\r\n      ++n;\r\n    }\r\n  }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>source Here&#8217;s two nifty tricks: 1) Using sin waves with incrementing frequencies to pulse each cell of a grid. This is a square grid version of the animation in the Whitney Music Box. fill( map(sin( millis()*.00001*n ),-1,1,0,255) ); n is a number which increases for each cell. .00001 is used to control the speed, and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":5,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions\/309"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}