{"id":26523,"date":"2015-04-21T16:58:17","date_gmt":"2015-04-21T21:58:17","guid":{"rendered":"http:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26523"},"modified":"2015-04-21T21:43:12","modified_gmt":"2015-04-22T02:43:12","slug":"what-the-heck-is-a-fractal-and-how-does-it-apply-to-games","status":"publish","type":"post","link":"https:\/\/www.shamusyoung.com\/twentysidedtale\/?p=26523","title":{"rendered":"What The Heck is a Fractal and How Does It Apply to Games?"},"content":{"rendered":"<p>This week I&#8217;m changing things up. Instead of <del datetime=\"2015-04-21T20:37:06+00:00\">complaining about<\/del> <em>pontificating on<\/em> the state of AAA games, I&#8217;m doing <a href=\"http:\/\/www.escapistmagazine.com\/articles\/view\/video-games\/columns\/experienced-points\/13809-Here-is-How-Fractals-Apply-to-Procedurally-Generated-Games\">a faux-educational bit on fractals and programming<\/a>.  This will be pretty remedial if you already know about fractals, but some people don&#8217;t and I&#8217;m hoping they&#8217;ll find it interesting.<\/p>\n<p>Let&#8217;s talk about the Mandelbrot set:<\/p>\n<p><!--more--><table class='nomargin' cellspacing='0' width='100%' cellpadding='0' align='center' border='0'><tr><td><iframe loading=\"lazy\" width=\"1024\" height=\"576\" src=\"https:\/\/www.youtube.com\/embed\/PD2XgQOyCCk\" frameborder=\"0\" allowfullscreen class=\"embed\"><\/iframe><br\/><small><a href='http:\/\/www.youtube.com\/watch?v=PD2XgQOyCCk'>Link (YouTube)<\/a><\/small><\/td><\/tr><\/table><\/p>\n<p>The Mandelbrot set actually kind of freaks me out. I find it kind of scary and hypnotic. As the image zooms in, I keep waiting for it to reach the &#8220;end&#8221;. I know there isn&#8217;t one and there can&#8217;t be one, but I keep waiting for it anyway. I think this video gives a much better sense of &#8220;infinity&#8221; than (say) looking up into the night sky and talking about the white dots in terms of millions of light-years. <\/p>\n<p>In the column I said the math for the Mandelbrot set can fit on an index card. It&#8217;s a little larger (but not much) if you express it in computer code instead:<\/p>\n<pre lang=\"c\">For each pixel (Px, Py) on the screen, do:\r\n{\r\n  x0 = scaled x coordinate of pixel (scaled to lie in the Mandelbrot X scale (-2.5, 1))\r\n  y0 = scaled y coordinate of pixel (scaled to lie in the Mandelbrot Y scale (-1, 1))\r\n  x = 0.0\r\n  y = 0.0\r\n  iteration = 0\r\n  max_iteration = 1000\r\n  while ( x*x + y*y < 2*2  AND  iteration < max_iteration )\r\n  {\r\n    xtemp = x*x - y*y + x0\r\n    y = 2*x*y + y0\r\n    x = xtemp\r\n    iteration = iteration + 1\r\n  }\r\n  color = palette[iteration]\r\n  plot(Px, Py, color)\r\n}<\/pre>\n<p>So you have this grid of x, y values that correspond to the pixels in your final image. You stick them trough this function, and then take the answer and feed it back into the function. Repeat over and over.<\/p>\n<p>You'll get one of two outcomes: <\/p>\n<p>1) The values will \"run away\", getting larger and larger as you go. Like multiplying 1.1 by itself, you end up with a value that climbs.<br \/>\n2) The values grind along, or trend towards zero. Like multiplying 1 by itself.<\/p>\n<p>So you watch for your number to go above some value. Then you look at how many times you cranked through the function to get there. You use that count to determine the pixel color. Great. Now you're done with one pixel of the final image.<\/p>\n<p>When you \"zoom in\", you're looking at smaller and smaller ranges of numbers. At the start of the video above, you're looking at numbers between -2.5 and +1. But at some point you're looking at numbers between (say) 0.1 and 0.2. Eventually you'll be dealing with stuff like 0.10000000001 through 0.10000000009. At some point you'll have values so precise that they won't fit inside typical variables offered by most computer languages. And then you have to write your own low-level system for doing math on numbers of arbitrary precision. I've never done any work like that, but I imagine you'll quickly run into some serious speed concerns. The video above apparently took 4 weeks to produce. You can find other videos on YouTube<span class='snote' title='1'>Like <a href=\"https:\/\/www.youtube.com\/watch?v=ohzJV980PIQ\">this one<\/a>.<\/span> that go super deep, so that by the end the values they're tracking are a thousand digits long. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week I&#8217;m changing things up. Instead of complaining about pontificating on the state of AAA games, I&#8217;m doing a faux-educational bit on fractals and programming. This will be pretty remedial if you already know about fractals, but some people don&#8217;t and I&#8217;m hoping they&#8217;ll find it interesting. Let&#8217;s talk about the Mandelbrot set:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[],"class_list":["post-26523","post","type-post","status-publish","format-standard","hentry","category-weekly-column"],"_links":{"self":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/26523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26523"}],"version-history":[{"count":0,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=\/wp\/v2\/posts\/26523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shamusyoung.com\/twentysidedtale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}