<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Algorithm Blogs &#187; fractal</title>
	<atom:link href="http://www.algorithm.co.il/blogs/index.php/tag/fractal/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Thu, 22 Apr 2010 21:04:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fractals in 10 minutes No. 5: Sierpinski Chaos Game</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-5-sierpinski-chaos-game/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-5-sierpinski-chaos-game/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 07:44:55 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ascii-art]]></category>
		<category><![CDATA[fractal]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=157</guid>
		<description><![CDATA[A few years back, I was in some course where they also taught me some Matlab. One of the exercises was to draw the Sierpinski triangle using a method of progressing a point randomly. I was quite surprised at the time, because I thought the Sierpinski triangle was more of an analytical thing: you drew [...]]]></description>
			<content:encoded><![CDATA[<p>A few years back, I was in some course where they also taught me some Matlab. One of the exercises was to draw the <a href="http://en.wikipedia.org/wiki/Sierpinski_triangle">Sierpinski triangle</a> using a method of progressing a point randomly. I was quite surprised at the time, because I thought the Sierpinski triangle was more of an analytical thing: you drew it using inverted triangles. </p>
<p>I wanted to check up on it, and it turns out the method has a name: <a href="http://en.wikipedia.org/wiki/Chaos_game">Chaos Game</a>.<br />
To generate a Sierpinski triangle using this method, one starts with a some point inside the triangle. Then, at each step, the next point is half the distance from the current point to one of the corners selected at random. I think this method of generating the Sierpinski triangle is even easier than the analytical one.</p>
<p><img src="http://www.algorithm.co.il/sitecode/sierpinski_chaos_game.png" alt="A sierpinski triangle generated using a chaos game" border="2" /></p>
<p>I used pylab (matplotlib) to create this image.<br />
As I usually do, I also wanted to draw it using ascii-art. However, I must confess, I am not satisfied with the result:</p>
<pre>
              %6
             6*%8
            %8  66
           %6%*6%%6
          6%'    '66
         6866'  '6%86
        %6* 86 '%8 *66
       %**%%'***%6***%%
      8'6            6*8
     6%%*8          %*'*6
    6%6'666        86# 86%
   %%*''**%6      %**''*'%6
  *%8'   '6%6    668'   '#%%
 %% '6' '#*'6%  *% %8' '6''6*
%68%#6#*#68*86*%6#%8%8'6%6*6%*
</pre>
<p>The code isn&#8217;t really good, as I didn&#8217;t put much thought into it and just hacked it up. Still, <a href="http://www.algorithm.co.il/sitecode/chaos_game.zip">I&#8217;m putting it up</a>, and I might improve it someday.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-5-sierpinski-chaos-game/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fractals in 10 minutes No. 4: Mandelbrot and Julia in Ascii Art</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 10:06:36 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[ascii-art]]></category>
		<category><![CDATA[fractal]]></category>
		<category><![CDATA[julia]]></category>
		<category><![CDATA[mandelbrot]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=145</guid>
		<description><![CDATA[I felt like it's about time I tackled the Mandelbrot and Julia sets, in Ascii-Art. Heck, the Mandelbrot fractal is on the logo of this Blog! However, being the most well-known fractal, this issue was tackled already, with satisfactory results.
Still, I took the ten minutes required to draw them in Python, using numpy:


def get_mandelbrot&#40;x, num_iter [...]]]></description>
			<content:encoded><![CDATA[<p>I felt like it's about time I tackled the <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot and Julia sets</a>, in Ascii-Art. Heck, the Mandelbrot fractal is on the logo of this Blog! However, being the most well-known fractal, this issue was <a href="http://bc.tech.coop/blog/040811.html">tackled</a> <a href="http://deems.wordpress.com/2008/11/19/howto-generate-a-mandelbrot-using-ascii-art-using-tsql/">already</a>, with satisfactory results.</p>
<p>Still, I took the ten minutes required to draw them in Python, using numpy:</p>
<div class="syntax_hilite">
<div id="python-3">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> get_mandelbrot<span style="color: black;">&#40;</span>x, num_iter = <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; c = x<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>num_iter<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; x = x**<span style="color: #ff4500;">2</span> + c<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>&gt;<span style="color: #ff4500;">2</span></p>
<p><span style="color: #00007f;font-weight:bold;">def</span> get_julia<span style="color: black;">&#40;</span>x, c, num_iter = <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>num_iter<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; x = x**<span style="color: #ff4500;">2</span> + c<br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> <span style="color: #008000;">abs</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>&gt;<span style="color: #ff4500;">2</span></div>
</div>
</div>
<p></p>
<p>"Hey!" you might say, "There's no loop in here!". Indeed, with numpy loops can sometimes be avoided, when using arrays. When the expression x**2 + c is applied to an array x, it is applied element-wise, allowing for implicit loops. The actual magic happens in the following function:</p>
<div class="syntax_hilite">
<div id="python-4">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> get_board<span style="color: black;">&#40;</span>bottom_left, top_right, num_x, num_y<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; x0, y0 = bottom_left<br />
&nbsp; &nbsp; x1, y1 = top_right<br />
&nbsp; &nbsp; x_values = numpy.<span style="color: #000000;">arange</span><span style="color: black;">&#40;</span>x0, x1, <span style="color: black;">&#40;</span>x1-x0<span style="color: black;">&#41;</span>/<span style="color: #008000;">float</span><span style="color: black;">&#40;</span>num_x<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; y_values = numpy.<span style="color: #000000;">arange</span><span style="color: black;">&#40;</span>y0, y1, <span style="color: black;">&#40;</span>y1-y0<span style="color: black;">&#41;</span>/<span style="color: #008000;">float</span><span style="color: black;">&#40;</span>num_y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: black;">&#91;</span>x+1j*y <span style="color: #00007f;font-weight:bold;">for</span> x <span style="color: #00007f;font-weight:bold;">in</span> x_values<span style="color: black;">&#93;</span> <span style="color: #00007f;font-weight:bold;">for</span> y <span style="color: #00007f;font-weight:bold;">in</span> y_values<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></div>
</div>
</div>
<p></p>
<p>The result of get_board will be used as the "x input" later on. It should be noted though that while that's a cute trick this time, it might grow unhandy for more complicated computations. For example, making each element to reflect the iteration number on which it "escaped".<br />
So, here are the results:</p>
<pre>
############################   ##############
###########################   ###############
##########################     ##############
#####################                ########
####################                #########
############ #######                 ########
############                         ########
#########                           #########
#########                           #########
############                         ########
############ #######                 ########
####################                #########
#####################                ########
##########################     ##############
###########################   ###############

############################################################
############################################################
############################################################
############################ ###############################
######################### ##   #############################
############## #######           ###########################
######## ##      ###              ##########  ##############
#########  ###        ###         ######       #############
##############       ######         ###        ###  ########
###############  ##########              ###      ## #######
############################           ####### #############
##############################   ## ########################
################################ ###########################
############################################################
############################################################
</pre>
<p>Here's the <a href="http://www.algorithm.co.il/sitecode/mandelbrot.zip">code</a>. (With the usual bsd-license slapped on top of it :)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-4-mandelbrot-and-julia-in-ascii-art/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fractals in 10 minutes no. 3 &#8211; The Dragon</title>
		<link>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-3-the-dragon/</link>
		<comments>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-3-the-dragon/#comments</comments>
		<pubDate>Wed, 16 Jan 2008 20:12:33 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[base systems]]></category>
		<category><![CDATA[dragon fractal]]></category>
		<category><![CDATA[fractal]]></category>
		<category><![CDATA[Knuth]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-3-the-dragon/</guid>
		<description><![CDATA[When first I looked through the pages of the book "Hacker's Delight", I found myself looking at the chapter about bases. There I learned a very curious fact - with the digits of 0,1 and the base of -2, you can represent any integer. Right afterwards I learned something even more interesting - with the [...]]]></description>
			<content:encoded><![CDATA[<p>When first I looked through the pages of the book <a href="http://www.amazon.com/Hackers-Delight-Henry-S-Warren/dp/0201914654/">"Hacker's Delight"</a>, I found myself looking at the chapter about bases. There I learned a very curious fact - with the digits of 0,1 and the base of -2, you can represent any integer. Right afterwards I learned something even more interesting - with the digits of 0,1 and the base of 1-i, you can represent and number of the form a+bi where a and b are integers. Having nothing to do with this curious fact, I let the subject go.<br />
Some time later, I was reading through Knuth's <a href="http://www.amazon.com/Art-Computer-Programming-Volumes-Boxed/dp/0201485419/">"Art of Computer Programming"</a>, and found that with the base of (1-i)^-1, and digits of 0,1 you can generate the dragon fractal!</p>
<p><img src="http://www.algorithm.co.il/sitecode/dragon.jpg" alt="The dragon fractal" /></p>
<p>Generating the fractal is quite simple actually:</p>
<div class="syntax_hilite">
<div id="python-6">
<div class="python"><span style="color: #00007f;font-weight:bold;">def</span> create_dragon_set<span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"calculate the dragon set, according to Knuth"</span><span style="color: #483d8b;">""</span><br />
&nbsp; &nbsp; s = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span>.<span style="color: #ff4500;">0</span>+<span style="color: #ff4500;">0</span>.0j<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">for</span> i <span style="color: #00007f;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>n<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; new_power = <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>.<span style="color: #ff4500;">0</span>-<span style="color: #ff4500;">1</span>.0j<span style="color: black;">&#41;</span>**<span style="color: black;">&#40;</span>-i<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; s |= <span style="color: #008000;">set</span><span style="color: black;">&#40;</span>x+new_power <span style="color: #00007f;font-weight:bold;">for</span> x <span style="color: #00007f;font-weight:bold;">in</span> s<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #00007f;font-weight:bold;">return</span> s</div>
</div>
</div>
<p></p>
<p>(By the way, can you do it better?)</p>
<p>The annoying part is converting the complex numbers to drawable integer points. After doing so, I used PIL to draw the jpeg.<br />
Here's a link to <a href="http://www.algorithm.co.il/sitecode/dragon.py">the code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-3-the-dragon/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
