{"id":202,"date":"2011-11-11T13:14:01","date_gmt":"2011-11-11T21:14:01","guid":{"rendered":"http:\/\/new.joyofprocessing.com\/blog\/?p=202"},"modified":"2011-11-11T13:31:11","modified_gmt":"2011-11-11T21:31:11","slug":"hilbert-curve","status":"publish","type":"post","link":"https:\/\/joyofprocessing.com\/blog\/2011\/11\/hilbert-curve\/","title":{"rendered":"Hilbert Curve"},"content":{"rendered":"<p><script type=\"application\/processing\">\/\/ Hilbert L System - Jim Bumgardner, for both Processing and Processing.js\n\nString axiom = \"A\";\nString[] rules = {\"A\", \"lBfrAfArfBl\",  \/\/ production rules copied from wikipedia article on Hilbert curve\n                  \"B\", \"rAflBfBlfAr\"};\nHashMap rulesH = new HashMap();\n\nint nbrGenerations = 4;\nfloat lineLength = 0;\nString lsys;\n\nvoid setup()\n{\n  size(500,500);\n  noLoop();\n  setupLSystem();\n}\n\nvoid setupLSystem()\n{\n  for (int j = 0; j < rules.length; j += 2) {\n    rulesH.put(rules[j], rules[j+1]);\n  }\n}\n\nString lsysGen(String lsys, String[] rules, int nbrGenerations)\n{\n  while (nbrGenerations > 0) {\n    String dst = \"\";\n    for (int i = lsys.length()-1; i >= 0; --i) {\n      String ch = lsys.substring(i,i+1);\n      if (rulesH.containsKey(ch))\n        dst += rulesH.get(ch);\n      else\n         dst += ch;\n    }\n    lsys = dst;\n    --nbrGenerations; \/\/ 1 down\n  }\n  return lsys;\n}\n\n\/\/ Angles are in integer amounts (1 = 90 degrees) to avoid rounding error\nvoid lsysDraw(String src, float sx, float sy, float len, int angD) \n{\n  \/\/ Table of 90 degree sin\/cos values\n  int[][] sincos  = {{1,0},{0,1},{-1,0},{0,-1}}; \n\n  float px = sx;\n  float py = sy;\n  \/\/ println(\"Drawing at \" + px + \",\" + py);\n  beginShape();\n  vertex(px,py);\n  \n  for (int i = 0; i < src.length(); ++i) {\n     String ch = lsys.substring(i,i+1);\n     if (ch.equals(\"f\")) {\n       px += sincos[angD][0]*len;\n       py += sincos[angD][1]*len;\n       vertex(px,py);\n     } else if (ch.equals(\"l\")) {\n       angD = (angD+3)%4; \/\/ Counter-clockwise turn\n     } else if (ch.equals(\"r\")) {\n       angD = (angD+1)%4;  \/\/ Clockwise turn\n     }\n  }\n  endShape();\n}\n\nvoid draw()\n{\n  lsys = lsysGen(axiom,rules, nbrGenerations);\n  \/\/ println(\"--> \" + lsys);\n  background(255);\n  stroke(128);\n  noFill();\n  smooth();\n  \n  float lineLength = width\/2.0 * pow(.5, nbrGenerations-1);\n  strokeWeight(lineLength\/2.0);\n  strokeCap(PROJECT);\n  \/\/ strokeJoin(ROUND);\n  lsysDraw(lsys, lineLength\/2, height-lineLength\/2, width\/2 * pow(.5, nbrGenerations-1), 0);\n}\n\nvoid mouseClicked()\n{\n  if (mouseX > width\/2) {\n    nbrGenerations = min(8, nbrGenerations+1);\n  }\n  else {\n    nbrGenerations = max(1, nbrGenerations-1);\n  }\n  redraw();\n}\n\n\n<\/script><\/p>\n<div class=\"ps_cap\"><a href=\"\/showexample.php?ex=hilbert\">source<\/a><\/div>\n<p>\nThis <a href=\"http:\/\/en.wikipedia.org\/wiki\/Hilbert_curve\">Hilbert curve<\/a> is implemented using a simple <a href=\"http:\/\/en.wikipedia.org\/wiki\/L-system\">L-system<\/a>.  Click on the left and right sides of the figure to generate simple or more complex versions of the curve.  <\/p>\n<p>I made this script to generate the pattern for the inlaid top of a wooden music box that I&#8217;m constructing as a Christmas present.  I&#8217;ll post a photo of the finished music box when it&#8217;s done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>source This Hilbert curve is implemented using a simple L-system. Click on the left and right sides of the figure to generate simple or more complex versions of the curve. I made this script to generate the pattern for the inlaid top of a wooden music box that I&#8217;m constructing as a Christmas present. I&#8217;ll [&hellip;]<\/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":[19,8,17,18,15],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-fractal","tag-intermediate","tag-lsystem","tag-recursion","tag-symmetry"],"_links":{"self":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/202","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=202"}],"version-history":[{"count":8,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":209,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/posts\/202\/revisions\/209"}],"wp:attachment":[{"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joyofprocessing.com\/blog\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}