{"id":44,"date":"2011-02-19T20:30:30","date_gmt":"2011-02-19T20:30:30","guid":{"rendered":"http:\/\/classes.visitsteve.com\/hacking\/?p=44"},"modified":"2011-02-19T20:30:30","modified_gmt":"2011-02-19T20:30:30","slug":"robot-as-a-function","status":"publish","type":"post","link":"https:\/\/classes.visitsteve.com\/hacking\/robot-as-a-function\/","title":{"rendered":"Robot as a function"},"content":{"rendered":"<p>Exercise 7-10<\/p>\n<p><script type=\"application\/processing\">\r\n\/\/Info: http:\/\/processingjs.org\/reference\r\n\r\nvoid setup() {\r\n  size(600, 300);\r\n  smooth();\r\n  background(240);\r\n  rectMode(CENTER);\r\n\/\/ done with setup\r\n}\r\n\r\nvoid draw() {\r\n\r\nbackground(240); \/\/ this kepps the whole thing from redrawing itself\r\ndrawRobot(80,mouseX-200,mouseY);\r\ndrawRobot(120,mouseX,mouseY);\r\ndrawRobot(60,mouseX+160,mouseY);\r\n\r\n}\r\n\r\nvoid drawRobot(int robotWidth, int xPos, int yPos) {\r\n\/\/ robotWidth is the width of the robot body\r\n\/\/ xPos and yPos are the x and y coordinates of the center of the robot\r\n  \r\n\/\/ body and arms\r\n  fill(200); \/\/ make it grey\r\n  rect(xPos,yPos,robotWidth,robotWidth*.6); \/\/body is 50x30\r\n  ellipse(xPos-robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);\r\n  ellipse(xPos+robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);\r\n\r\n\/\/ legs\r\n  float legStart = xPos-robotWidth*.3;\r\n  for (int i=0; i<2; i++) {\r\n  rect(legStart,yPos+robotWidth*.4,robotWidth*.2,robotWidth*.2);\r\n  legStart = legStart+robotWidth*.6;\r\n  }\r\n\r\n\r\n\/\/ head\r\n  rect(xPos,yPos-robotWidth*.45,robotWidth*.8,robotWidth*.3); \/\/ head is 40x15\r\n\r\n\/\/ mouth\r\n  fill(255);\r\n  rect(xPos,yPos-robotWidth*.37,robotWidth*.4,robotWidth*.06);\r\n\r\n\r\n\/\/ eyes\r\n  if (mousePressed == true) {\r\n  fill(255,0,0); \/\/ make the eyes red\r\n  } else {\r\n  fill(255); \/\/ make the eyes white\r\n  }\r\n  ellipse(xPos-robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1);\r\n  ellipse(xPos+robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1);\r\n\r\n}\r\n\r\n\r\n\r\n<\/script><\/p>\n<h2>and the code&#8230;<\/h2>\n<p><code><\/p>\n<p>void setup() {<br \/>\n  size(600, 300);<br \/>\n  smooth();<br \/>\n  background(240);<br \/>\n  rectMode(CENTER);<br \/>\n\/\/ done with setup<br \/>\n}<\/p>\n<p>void draw() {<\/p>\n<p>background(240); \/\/ this kepps the whole thing from redrawing itself<br \/>\ndrawRobot(80,mouseX-200,mouseY);<br \/>\ndrawRobot(120,mouseX,mouseY);<br \/>\ndrawRobot(60,mouseX+160,mouseY);<\/p>\n<p>}<\/p>\n<p>void drawRobot(int robotWidth, int xPos, int yPos) {<br \/>\n\/\/ robotWidth is the width of the robot body<br \/>\n\/\/ xPos and yPos are the x and y coordinates of the center of the robot<\/p>\n<p>\/\/ body and arms<br \/>\n  fill(200); \/\/ make it grey<br \/>\n  rect(xPos,yPos,robotWidth,robotWidth*.6); \/\/body is 50x30<br \/>\n  ellipse(xPos-robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);<br \/>\n  ellipse(xPos+robotWidth*.6,yPos-robotWidth*.1,robotWidth*.2,robotWidth*.2);<\/p>\n<p>\/\/ legs<br \/>\n  float legStart = xPos-robotWidth*.3;<br \/>\n  for (int i=0; i<2; i++) {\n  rect(legStart,yPos+robotWidth*.4,robotWidth*.2,robotWidth*.2);\n  legStart = legStart+robotWidth*.6;\n  }\n\n\n\/\/ head\n  rect(xPos,yPos-robotWidth*.45,robotWidth*.8,robotWidth*.3); \/\/ head is 40x15\n\n\/\/ mouth\n  fill(255);\n  rect(xPos,yPos-robotWidth*.37,robotWidth*.4,robotWidth*.06);\n\n\n\/\/ eyes\n  if (mousePressed == true) {\n  fill(255,0,0); \/\/ make the eyes red\n  } else {\n  fill(255); \/\/ make the eyes white\n  }\n  ellipse(xPos-robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1);\n  ellipse(xPos+robotWidth*.18,yPos-robotWidth*.49,robotWidth*.1,robotWidth*.1);\n\n}\n\n\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Exercise 7-10 and the code&#8230; void setup() { size(600, 300); smooth(); background(240); rectMode(CENTER); \/\/ done with setup } void draw() { background(240); \/\/ this kepps the whole thing from redrawing itself drawRobot(80,mouseX-200,mouseY); drawRobot(120,mouseX,mouseY); drawRobot(60,mouseX+160,mouseY); } void drawRobot(int robotWidth, int xPos, &hellip; <a href=\"https:\/\/classes.visitsteve.com\/hacking\/robot-as-a-function\/\">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":[1],"tags":[3],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-classnews","tag-processing-sketch"],"_links":{"self":[{"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":0,"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"wp:attachment":[{"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/classes.visitsteve.com\/hacking\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}