{"id":227,"date":"2015-06-22T15:30:13","date_gmt":"2015-06-22T11:30:13","guid":{"rendered":"http:\/\/courses.haigarmen.com\/creativecoding\/?p=227"},"modified":"2015-06-23T09:08:13","modified_gmt":"2015-06-23T05:08:13","slug":"exercise-55","status":"publish","type":"post","link":"https:\/\/courses.haigarmen.com\/creativecoding\/exercise-55\/","title":{"rendered":"Exercise 55"},"content":{"rendered":"<pre><code>\r\nint _numChildren = 4;\r\nint _maxLevels = 4;\r\nBranch _trunk;\r\nvoid setup() {\r\nsize(750,500);\r\nbackground(255);\r\nnoFill();\r\nsmooth();\r\nnewTree();\r\n}\r\nvoid newTree() {\r\n_trunk = new Branch(1, 0, width\/2, height\/2);\r\n_trunk.drawMe();\r\n}\r\nvoid draw() {\r\nbackground(255);\r\n_trunk.updateMe(width\/2, height\/2);\r\n_trunk.drawMe();\r\n}<\/code>\r\n\r\n\r\n<code>class Branch {\r\n  float level, index;\r\n  float x, y;\r\n  float endx, endy;\r\n  float strokeW, alph;\r\n  float len, lenChange;\r\n  float rot, rotChange;\r\n\r\n  Branch[] children = new Branch[0];\r\n  Branch(float lev, float ind, float ex, float why) {\r\n    level = lev;\r\n    index = ind;\r\n    strokeW = (1\/level) * 10;\r\n    alph = 255 \/ level;\r\n    len = (1\/level) * random(500);\r\n    rot = random(360);\r\n    lenChange = random(10) - 5;\r\n    rotChange = random(10) - 5;\r\n    updateMe(ex, why);\r\n    if (level < _maxLevels) {\r\n      children = new Branch[_numChildren];\r\n      for (int x=0; x < _numChildren; x++) {\r\n        children[x] = new Branch(level+1, x, endx, endy);\r\n      }\r\n    }\r\n  }\r\n  void updateMe(float ex, float why) {\r\n    x = ex;\r\n    y = why;\r\n    rot += rotChange;\r\n    if (rot > 360) { \r\n      rot = 0;\r\n    } else if (rot < 0) { \r\n      rot = 360;\r\n    }\r\n    len -= lenChange;\r\n    if (len < 0) { \r\n      lenChange *= -1;\r\n    } else if (len > 500) { \r\n      lenChange *= -1;\r\n    }\r\n    float radian = radians(rot);\r\n    endx = x + (len * cos(radian));\r\n    endy = y + (len * sin(radian));\r\n    for (int i=0; i < children.length; i++) { \r\n        children[i].updateMe(endx, endy);\r\n    }\r\n  }\r\n  void drawMe() {\r\n    if (level > 1) {\r\n      strokeWeight(strokeW);\r\n      stroke(0, alph);\r\n      fill(255, alph);\r\n      line(x, y, endx, endy);\r\n      ellipse(endx, endy, len\/12, len\/12);\r\n    }\r\n    for (int i=0; i < children.length; i++) {\r\n      children[i].drawMe();\r\n    }\r\n  }\r\n}\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>int _numChildren = 4; int _maxLevels = 4; Branch _trunk; void setup() { size(750,500); background(255); noFill(); smooth(); newTree(); } void newTree() { _trunk = new Branch(1, 0, width\/2, height\/2); _trunk.drawMe(); } void draw() { background(255); _trunk.updateMe(width\/2, height\/2); _trunk.drawMe(); } class Branch { float level, index; float x, y; float endx, endy; float strokeW, alph; float [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-lessons"],"_links":{"self":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":6,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":247,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/227\/revisions\/247"}],"wp:attachment":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}