{"id":243,"date":"2015-06-22T15:35:29","date_gmt":"2015-06-22T11:35:29","guid":{"rendered":"http:\/\/courses.haigarmen.com\/creativecoding\/?p=243"},"modified":"2015-06-23T09:11:05","modified_gmt":"2015-06-23T05:11:05","slug":"bonus-mouse-follower","status":"publish","type":"post","link":"https:\/\/courses.haigarmen.com\/creativecoding\/bonus-mouse-follower\/","title":{"rendered":"Bonus: Mouse Follower"},"content":{"rendered":"<pre><code>\r\n\/\/ P_2_2_3_02.pde\r\n\/\/ \r\n\/\/ Generative Gestaltung, ISBN: 978-3-87439-759-9\r\n\/\/ First Edition, Hermann Schmidt, Mainz, 2009\r\n\/\/ Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni\r\n\/\/ Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni\r\n\/\/\r\n\/\/ http:\/\/www.generative-gestaltung.de\r\n\/**\r\n * form mophing process by connected random agents\r\n * two forms: circle and line\r\n * \r\n * MOUSE\r\n * click               : start a new circe\r\n * position x\/y        : direction and speed of floating\r\n * \r\n * KEYS\r\n * 1-2                 : fill styles\r\n * 3-4                 : form styles circle\/line\r\n * arrow up\/down       : step size +\/-\r\n * f                   : freeze. loop on\/off\r\n * Delete\/Backspace    : clear display\r\n * s                   : save png\r\n * r                   : start pdf recording\r\n * e                   : stop pdf recording\r\n *\/\r\n\r\nimport processing.pdf.*;\r\nimport java.util.Calendar;\r\n\r\nboolean recordPDF = false;\r\n\r\nint formResolution = 15;\r\nint stepSize = 2;\r\nfloat distortionFactor = 1;\r\nfloat initRadius = 150;\r\nfloat centerX, centerY;\r\nfloat[] x = new float[formResolution];\r\nfloat[] y = new float[formResolution];\r\n\r\nboolean filled = false;\r\nboolean freeze = false;\r\nint mode = 0;\r\n\r\n\r\nvoid setup(){\r\n  \/\/ use fullscreen size \r\n  size(displayWidth, displayHeight);\r\n  smooth();\r\n\r\n  \/\/ init form\r\n  centerX = width\/2; \r\n  centerY = height\/2;\r\n  float angle = radians(360\/float(formResolution));\r\n  for (int i=0; i < formResolution; i++){\r\n    x[i] = cos(angle*i) * initRadius;\r\n    y[i] = sin(angle*i) * initRadius;  \r\n  }\r\n\r\n  stroke(0, 50);\r\n  background(255);\r\n}\r\n\r\n\r\nvoid draw(){\r\n  \/\/ floating towards mouse position\r\n  if (mouseX != 0 || mouseY != 0) {\r\n    centerX += (mouseX-centerX) * 0.01;\r\n    centerY += (mouseY-centerY) * 0.01;\r\n  }\r\n\r\n  \/\/ calculate new points\r\n  for (int i=0; i < formResolution; i++){\r\n    x[i] += random(-stepSize,stepSize);\r\n    y[i] += random(-stepSize,stepSize);\r\n    \/\/ ellipse(x[i], y[i], 5, 5);\r\n  }\r\n\r\n  strokeWeight(0.75);\r\n  if (filled) fill(random(255));\r\n  else noFill();\r\n\r\n  if (mode == 0) {\r\n    beginShape();\r\n    \/\/ start controlpoint\r\n    curveVertex(x[formResolution-1]+centerX, y[formResolution-1]+centerY);\r\n\r\n    \/\/ only these points are drawn\r\n    for (int i=0; i < formResolution; i++){\r\n      curveVertex(x[i]+centerX, y[i]+centerY);\r\n    }\r\n    curveVertex(x[0]+centerX, y[0]+centerY);\r\n\r\n    \/\/ end controlpoint\r\n    curveVertex(x[1]+centerX, y[1]+centerY);\r\n    endShape();\r\n  }\r\n\r\n  if (mode == 1) {\r\n    beginShape();\r\n    \/\/ start controlpoint\r\n    curveVertex(x[0]+centerX, y[0]+centerY);\r\n\r\n    \/\/ only these points are drawn\r\n    for (int i=0; i < formResolution; i++){\r\n      curveVertex(x[i]+centerX, y[i]+centerY);\r\n    }\r\n\r\n    \/\/ end controlpoint\r\n    curveVertex(x[formResolution-1]+centerX, y[formResolution-1]+centerY);\r\n    endShape();\r\n  }\r\n\r\n}\r\n\r\n\r\nvoid mousePressed() {\r\n  \/\/ init forms on mouse position\r\n  centerX = mouseX; \r\n  centerY = mouseY;\r\n\r\n  \/\/ circle\r\n  if (mode == 0) {\r\n    centerX = mouseX;\r\n    centerY = mouseY;\r\n    float angle = radians(360\/float(formResolution));\r\n    float radius = initRadius * random(0.5,1.0);\r\n    for (int i=0; i < formResolution; i++){\r\n      x[i] = cos(angle*i) * radius;\r\n      y[i] = sin(angle*i) * radius;\r\n    }\r\n  } \r\n\r\n  \/\/ line\r\n  if (mode == 1) {\r\n    centerX = mouseX;\r\n    centerY = mouseY;\r\n    float radius = initRadius * random(0.5,5.0);\r\n    float angle = random(PI);\r\n    radius = initRadius*4;\r\n    angle = 0;\r\n    \r\n    float x1 = cos(angle) * radius;\r\n    float y1 = sin(angle) * radius;\r\n    float x2 = cos(angle-PI) * radius;\r\n    float y2 = sin(angle-PI) * radius;\r\n    for(int i=0; i < formResolution; i++) {\r\n      x[i] = lerp(x1, x2, i\/(float)formResolution);\r\n      y[i] = lerp(y1, y2, i\/(float)formResolution);\r\n    }\r\n  }\r\n}\r\n\r\n\r\nvoid keyPressed() {\r\n  if (keyCode == UP) stepSize++;\r\n  if (keyCode == DOWN) stepSize--;\r\n  stepSize = max(stepSize, 1);\r\n  println(\"stepSize: \" + stepSize);\r\n}\r\n\r\nvoid keyReleased() {\r\n  if (key == 's' || key == 'S') saveFrame(timestamp()+\"_##.png\");\r\n  if (key == DELETE || key == BACKSPACE) background(255);\r\n\r\n  if (key == '1') filled = false;\r\n  if (key == '2') filled = true;\r\n  if (key == '3') mode = 0;\r\n  if (key == '4') mode = 1;\r\n\r\n  \/\/ ------ pdf export ------\r\n  \/\/ press 'r' to start pdf recording and 'e' to stop it\r\n  \/\/ ONLY by pressing 'e' the pdf is saved to disk!\r\n  if (key =='r' || key =='R') {\r\n    if (recordPDF == false) {\r\n      beginRecord(PDF, timestamp()+\".pdf\");\r\n      println(\"recording started\");\r\n      recordPDF = true;\r\n      stroke(0, 50);\r\n    }\r\n  } \r\n  else if (key == 'e' || key =='E') {\r\n    if (recordPDF) {\r\n      println(\"recording stopped\");\r\n      endRecord();\r\n      recordPDF = false;\r\n      background(255); \r\n    }\r\n  } \r\n\r\n  \/\/ switch draw loop on\/off\r\n  if (key == 'f' || key == 'F') freeze = !freeze;\r\n  if (freeze == true) noLoop();\r\n  else loop();\r\n}\r\n\r\n\r\n\/\/ timestamp\r\nString timestamp() {\r\n  Calendar now = Calendar.getInstance();\r\n  return String.format(\"%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS\", now);\r\n}\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ P_2_2_3_02.pde \/\/ \/\/ Generative Gestaltung, ISBN: 978-3-87439-759-9 \/\/ First Edition, Hermann Schmidt, Mainz, 2009 \/\/ Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni \/\/ Copyright 2009 Hartmut Bohnacker, Benedikt Gross, Julia Laub, Claudius Lazzeroni \/\/ \/\/ http:\/\/www.generative-gestaltung.de \/** * form mophing process by connected random agents * two forms: circle and line * * [&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-243","post","type-post","status-publish","format-standard","hentry","category-lessons"],"_links":{"self":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/243","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=243"}],"version-history":[{"count":6,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":257,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/posts\/243\/revisions\/257"}],"wp:attachment":[{"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/courses.haigarmen.com\/creativecoding\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}