{"id":446,"date":"2011-12-23T10:32:22","date_gmt":"2011-12-23T18:32:22","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=446"},"modified":"2011-12-23T10:32:22","modified_gmt":"2011-12-23T18:32:22","slug":"snowflakes","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/12\/snowflakes\/","title":{"rendered":"Snowflakes"},"content":{"rendered":"<p><script type=\"application\/processing\">class Flake {\n  float x,y,vx,vy;\n  float rot,spin;\n  float orad;\n  int nbrPoints;\n  PVector[]  pts;\n  float sWeight;\n\n  Flake() {\n    init();\n  }\n  \n  void init() {\n    x = random(width);\n    y = -(24 + random(height*2));\n    rot = random(TWO_PI);\n    spin = random(-.03,.03);\n    vx = 0;\n    vy = 0;\n    orad = 12 + sq(random(1))*16;\n    sWeight = orad\/32.0;\n    nbrPoints = int(random(5,15))*2;\n    pts = new PVector[nbrPoints];\n    for (int i = 0; i < nbrPoints; ++i) {\n      pts[i] = new PVector( cos( random(TWO_PI))*random(orad), sin(random(TWO_PI))*random(orad) );   \n    }\n  }\n\n  void physics() {\n    vx += random(-.2,.2);\n    vy += random(-.2,.2);\n    vy += .2;\n    vx *= .9;\n    vy *= .9;\n    x += vx;\n    y += vy;\n    rot += spin;\n    if (y > height + 24) {\n      init();\n    }\n    if (x > width+24) {\n      x = -24;\n    }\n    if (x < -24) {\n      x = width+24;\n    }\n  }\n\n  void render() {\n    strokeWeight(sWeight);\n\n    \/\/ Shadow pass\n    stroke( #aaaaaa );\n    pushMatrix();\n      translate(x+1, y+1);\n      rotate(rot-PI\/2); \/\/ Point upwards  \n      for (int reflection = 0; reflection < 6; ++reflection) {\n        for (int i = 0; i < nbrPoints-1; i += 2) {\n            line( pts[i].x, pts[i].y, pts[i+1].x, pts[i+1].y );\n          }\n        rotate( radians(60) );\n      }\n    popMatrix();\n\n    \/\/ Foreground pass\n    stroke( #ffffff );\n    pushMatrix();\n      translate(x, y);\n      rotate(rot-PI\/2); \/\/ Point upwards  \n      for (int reflection = 0; reflection < 6; ++reflection) {\n        for (int i = 0; i < nbrPoints-1; i += 2) {\n            line( pts[i].x, pts[i].y, pts[i+1].x, pts[i+1].y );\n          }\n        rotate( radians(60) );\n      }\n    popMatrix();\n  }\n}\n\nArrayList<Flake> flakes;\nint nbrFlakes = 40;\n\nvoid setup()\n{\n  size(500,500);\n  smooth();\n  strokeWeight(2);\n  \n  flakes = new ArrayList<Flake>();\n  for (int i = 0; i < nbrFlakes; ++i) {\n     flakes.add( new Flake());\n  }  \n}\n\nvoid draw() \n{\n  for (int i = 0; i < height; ++i) {\n    stroke( lerpColor( #00bfff, #ffffff, i\/(float)height));\n    rect(0,i,width,1);\n  }\n  stroke( #ffffff );\n  noFill();\n  for (Flake f : flakes) {\n    f.physics();\n    f.render();\n  }\n}\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=snowflakes\">source<\/a><\/div>\n<p>Merry Christmas!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>source Merry Christmas!<\/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,14,15],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-animation","tag-intermediate","tag-stars","tag-symmetry"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/446","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=446"}],"version-history":[{"count":3,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":449,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/446\/revisions\/449"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}