{"id":310,"date":"2011-11-26T11:07:11","date_gmt":"2011-11-26T19:07:11","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=310"},"modified":"2011-11-26T11:07:11","modified_gmt":"2011-11-26T19:07:11","slug":"pointing-squares","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/11\/pointing-squares\/","title":{"rendered":"Pointing Squares"},"content":{"rendered":"<p><script type=\"application\/processing\">void setup()\n{\n  size(500,500);\n  smooth();\n  noCursor();\n  noLoop(); \/\/ Only draw when the mouse moves\n}\n\n\/\/ Since the graphics only change when the mouse moves, this keeps the CPU\n\/\/ from overworking\nvoid mouseMoved()\n{\n  redraw();\n}\n\nvoid draw() \n{\n  float leftMargin = 20;\n  float topMargin = 20;\n  float nbrCols = 20;\n  float nbrRows = 20;\n  float cellWidth = (width-leftMargin*2)\/nbrCols;\n  float cellHeight = (height-topMargin*2)\/nbrRows;\n\n  background(255);\n  strokeWeight(cellWidth\/2);\n  strokeCap(SQUARE);\n  for (int y = 0; y < nbrRows; ++y) {\n    for (int x = 0; x < nbrCols; ++x) {\n      float px = leftMargin + x*cellWidth + cellWidth\/2;\n      float py = topMargin + y*cellHeight + cellHeight\/2;\n      float angle = atan2(mouseY-py,mouseX-px);\n      pushMatrix();\n      translate(px, py);\n      rotate(angle);\n      line(0,0,cellWidth\/2,0);\n      popMatrix();\n    }\n  }\n}\n\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=pointing_squares\">source<\/a><\/div>\n<p>In this sketch, I&#8217;m using the atan2() function to determine the angle from each grid square to the mouse.  <\/p>\n<p>This angle is then used to rotate a thick short line so that each square is oriented towards the mouse.<\/p>\n<p>Wave the mouse over the sketch to see the effect of this.<\/p>\n<p>Here&#8217;s the main loop:<\/p>\n<pre>\r\n\r\n  for (int y = 0; y < nbrRows; ++y) {\r\n    for (int x = 0; x < nbrCols; ++x) {\r\n      float px = leftMargin + x*cellWidth + cellWidth\/2;\r\n      float py = topMargin + y*cellHeight + cellHeight\/2;\r\n      float angle = atan2(mouseY-py,mouseX-px);\r\n      pushMatrix();\r\n      translate(px, py);\r\n      rotate(angle);\r\n      line(0,0,cellWidth\/2,0);\r\n      popMatrix();\r\n    }\r\n  }\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>source In this sketch, I&#8217;m using the atan2() function to determine the angle from each grid square to the mouse. This angle is then used to rotate a thick short line so that each square is oriented towards the mouse. Wave the mouse over the sketch to see the effect of this. Here&#8217;s the main [&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-310","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/310","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=310"}],"version-history":[{"count":4,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":314,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/310\/revisions\/314"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}