{"id":437,"date":"2011-12-22T11:14:10","date_gmt":"2011-12-22T19:14:10","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=437"},"modified":"2011-12-22T11:14:47","modified_gmt":"2011-12-22T19:14:47","slug":"fun-with-sine","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/12\/fun-with-sine\/","title":{"rendered":"Fun with Sine"},"content":{"rendered":"<p><script type=\"application\/processing\">\nPVector p1,p2;\n\nvoid setup()\n{\n  size(500,500);\n  p1 = new PVector(20,20);\n  p2 = new PVector(width-20, height-20);\n  smooth();\n}\n\nvoid sineTo(PVector p1, PVector p2, float freq, float amp, float phase)\n{\n  float d = PVector.dist(p1,p2);\n  float a = atan2(p2.y-p1.y,p2.x-p1.x);\n  noFill();\n  pushMatrix();\n    translate(p1.x,p1.y);\n    rotate(a);\n    beginShape();\n      for (float i = 0; i <= d; i += 1) {\n        float y = sin(i*TWO_PI*freq\/d+phase)*amp;\n        if (i < d\/10)\n          y = lerp(0,y,i\/(d\/10));\n        else if (i > d-d\/10)\n          y = lerp(y,0,(i-(d-d\/10))\/(d\/10));\n        vertex(i,y);\n      }\n    endShape();\n  popMatrix();\n}\n\nvoid draw()\n{\n  float tm = millis()*.001;\n  float freq = 8 + sin(tm)*6;\n  float amp = 30 + cos(tm\/2)*20;\n  float phase = tm*10;\n  \n  background(255);\n  sineTo(p1,p2,freq,amp,phase);\n  fill(255,255,0);\n  ellipse(p1.x,p1.y,20,20);\n  ellipse(p2.x,p2.y,20,20);\n  if (mousePressed) {\n      if (dist(p1.x,p1.y,pmouseX,pmouseY) < 10) {\n        p1.x += mouseX - pmouseX;\n        p1.y += mouseY - pmouseY;\n      }  else if (dist(p2.x,p2.y,pmouseX,pmouseY) < 10) {\n        p2.x += mouseX - pmouseX;\n        p2.y += mouseY - pmouseY;\n      }\n  }\n}\n\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=sine_fun\">source<\/a><\/div>\n<p>This sketch provides a useful subroutine<\/p>\n<pre>\r\nvoid sineTo(PVector p1, PVector p2, float freq, float amp, float phase)\r\n<\/pre>\n<p>That can be used to draw a sine wave from one point to another.  The end segments of the wave are lerped so that the wave meets at the point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>source This sketch provides a useful subroutine void sineTo(PVector p1, PVector p2, float freq, float amp, float phase) That can be used to draw a sine wave from one point to another. The end segments of the wave are lerped so that the wave meets at the point.<\/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":[11,8,30,24,31],"class_list":["post-437","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-animation","tag-intermediate","tag-sine","tag-time","tag-trigonometry"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/437","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=437"}],"version-history":[{"count":8,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions"}],"predecessor-version":[{"id":445,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/437\/revisions\/445"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}