<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: fractal code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 13 Oct 2008 18:05:36 GMT</pubDate>
    <description>DZone Snippets: fractal code</description>
    <item>
      <title>Cantor's Sets with Processing</title>
      <link>http://snippets.dzone.com/posts/show/5102</link>
      <description>This simple Processing programme generates the &lt;a href="http://en.wikibooks.org/wiki/Fractals/Cantor's_Set"&gt;Cantor's Sets&lt;/a&gt; fractal.&lt;br /&gt;&lt;br /&gt;Images are generated randomly, but tend to look like &lt;a href="http://scvalex.deviantart.com/art/Cantor-s-Sets-76451637"&gt;this&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int setHeight = 60;&lt;br /&gt;color setColour = color(184 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40),&lt;br /&gt;                        124 + random(-40, 40));&lt;br /&gt;int wid = 600;&lt;br /&gt;&lt;br /&gt;void setup() {&lt;br /&gt;  size(wid, (int)((log(wid) / log(3) + 1) * (setHeight + 5)));&lt;br /&gt;  background(0);&lt;br /&gt;  smooth();&lt;br /&gt;  &lt;br /&gt;  cantorSet(10, 10, width - 20, setColour);&lt;br /&gt;  &lt;br /&gt;  save("cantorSet.png");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;float rnd(float x) {&lt;br /&gt;  return x + random(-20, 20);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void brushRect(int x, int y, int w, int h) {&lt;br /&gt;  for (int i = 0; i &lt; h; ++i) {&lt;br /&gt;    float r = random(3);&lt;br /&gt;    strokeWeight(r);&lt;br /&gt;    float downpull = random(h / 4);&lt;br /&gt;    float shudder = random(-2, 2);&lt;br /&gt;    line(x + shudder, y + i, x + w - shudder, y + i + downpull);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void cantorSet(int y, int offX, int wid, color col) {&lt;br /&gt;  if ((y &gt; height - 10) || (wid &lt; 1))&lt;br /&gt;    return;&lt;br /&gt;&lt;br /&gt;  stroke(col);&lt;br /&gt;  brushRect(offX, y, wid, setHeight);&lt;br /&gt;  //fill(col);&lt;br /&gt;  //rect(offX, y, wid, setHeight);&lt;br /&gt;  &lt;br /&gt;  color newCol = color(rnd(red(col)), rnd(green(col)),&lt;br /&gt;                      rnd(blue(col)));&lt;br /&gt;  cantorSet(y + setHeight + 5, offX, wid / 3, newCol);&lt;br /&gt;  cantorSet(y + setHeight + 5, offX + wid*2/3, wid / 3, newCol);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Feb 2008 11:08:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5102</guid>
      <author>scvalex (Alexandru Scvortov)</author>
    </item>
    <item>
      <title>Mandelbrot Set Fractal</title>
      <link>http://snippets.dzone.com/posts/show/1350</link>
      <description>I take the code from &lt;a href=http://labix.org/snippets/mandelbrot&gt;Gustavo Niemeyer&lt;/a&gt; and modify it a bit.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import e32&lt;br /&gt;from appuifw import *&lt;br /&gt;&lt;br /&gt;app.screen = 'full'&lt;br /&gt;app.body = canvas = Canvas()&lt;br /&gt;width, height = canvas.size&lt;br /&gt;&lt;br /&gt;xaxis = width/2&lt;br /&gt;yaxis = height/1.5&lt;br /&gt;scale = 60&lt;br /&gt;iterations = 25&lt;br /&gt;&lt;br /&gt;for y in range(height):&lt;br /&gt;  for x in range(width):&lt;br /&gt;    magnitude = 0&lt;br /&gt;    z = 0+0j&lt;br /&gt;    c = complex(float(y-yaxis)/scale, float(x-xaxis)/scale)&lt;br /&gt;    for i in range(iterations):&lt;br /&gt;      z = z**2+c&lt;br /&gt;      if abs(z) &gt; 2:&lt;br /&gt;        v = 765*i/iterations&lt;br /&gt;        if v &gt; 510:&lt;br /&gt;          color = (255, 255, v%255)&lt;br /&gt;        elif v &gt; 255:&lt;br /&gt;          color = (255, v%255, 0)&lt;br /&gt;        else:&lt;br /&gt;          color = (v%255, 0, 0)&lt;br /&gt;        break&lt;br /&gt;    else:&lt;br /&gt;      color = (0, 0, 0)&lt;br /&gt;    canvas.point((x, y), color)&lt;br /&gt;  e32.ao_yield()&lt;br /&gt; &lt;br /&gt;lock = e32.Ao_lock()&lt;br /&gt;app.exit_key_handler = lock.signal&lt;br /&gt;lock.wait()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 03 Feb 2006 13:40:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1350</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
