<?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.co.il &#187; Graphics</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/programming/graphics/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Visualizing Data Using the Hilbert Curve</title>
		<link>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/</link>
		<comments>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 06:57:30 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hilbert curve]]></category>
		<category><![CDATA[PIL]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=525</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he &#8230; <a href="http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he asked for my help.<br />
I installed Python &#038; PIL on his machine, and not too long after, he had the image plotted. The script looked something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data_points = get_data_points<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
n =  <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
&nbsp;
image = Image<span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span>, <span style="color: black;">&#40;</span>n, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> idx, pt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>:
	image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>idx/n, idx<span style="color: #66cc66;">%</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
image.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bla.png'</span>, <span style="color: #483d8b;">'png'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Easy enough to do. Well, easy enough if you have enough memory to handle very large data sets. Luckily enough, we had just enough memory for this script &#038; data series, and we were happy. The image was generated, and everything worked fine.</p>
<p>Still, we wanted to improve on that. One problem with this visualization is that two horizontally adjacent pixels don’t have anything to do with each other. Remembering xkcd’s &#8220;<a href="http://xkcd.com/195/">Map of the Internet</a>&#8220;, I decided to use the <a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert Curve</a>. I started with wikipedia&#8217;s version of the code for the Python turtle and changed it to generate a string of instructions of where to put pixels. On the way I improved the time complexity by changing it to have only two recursion calls instead of four. (It can probably be taken down to one by the way, I leave that as a challenge to the reader :)</p>
<p>Unfortunately, at this point we didn’t have enough memory to hold all of those instructions, so I changed it into a generator. Now it was too slow. I cached the lower levels of the recursion, and now it worked in reasonable time (about 3-5 minutes), with reasonable memory requirements (no OutOfMemory exceptions). Of course, I&#8217;m skipping a bit of exceptions and debugging along the way. Still, it was relatively straightforward.</p>
<p>Writing the generator wasn&#8217;t enough &#8211; there were still pixels to draw! It took a few more minutes to write a simple &#8220;turtle&#8221;, that walks the generated hilbert curve.<br />
Now, we were ready:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">hilbert = Hilbert<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> pt <span style="color: #ff7700;font-weight:bold;">in</span> data_points:
    x,y = hilbert.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A few minutes later, the image was generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python + Curses + Numpy -&gt; Ascii Art Rotating Cube</title>
		<link>http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/#comments</comments>
		<pubDate>Sun, 13 Apr 2008 17:40:37 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[3d graphics]]></category>
		<category><![CDATA[ascii-art]]></category>
		<category><![CDATA[roguelike]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/python-curses-numpy-ascii-art-rotating-cube/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/' addthis:title='Python + Curses + Numpy -&#62; Ascii Art Rotating Cube'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Well, the title says it all, or at least most of it. If you are still not convinced, a picture&#8217;s worth 500 dwords: / &#124;\ / &#124; \ / &#124; \ // &#124; \\ / &#124; \ / \ \ &#8230; <a href="http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/' addthis:title='Python + Curses + Numpy -&#62; Ascii Art Rotating Cube' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Well, the title says it all, or at least most of it.<br />
If you are still not convinced, a picture&#8217;s worth 500 dwords:</p>
<pre><code>
        / |\
       /  | \
      /   |  \
    //    |   \\
   /      |     \
  /       \      \
 |       / \      |
 |\     /   \    /|
 | \   /     \\ / |
 |  \ /       //  |
 |  /\       /  \ |
 | /  \\    /    \|
 |/     \  /      |
 /       \/       /
  \       |      /
   \      |     /
    \\    |   //
      \   |  /
       \  | /
        \  /
</code></pre>
<p>I&#8217;ve been toying with the idea of writing a <a href="http://en.wikipedia.org/wiki/Roguelike">roguelike</a>, and I thought that it would be really fun if it had an &#8216;Eye-Of-The-Beholder&#8217; kind of view as well, just in ascii-art. There are plenty of things I wanted to do with ascii-art, since it&#8217;s such a nice toy. So I started writing some code, and this is a small test on the way.</p>
<p>Nowadays, when I discuss 3d ascii-art, people usually think about a picture transformed with aalib or something similar. Although it is a (very) nice idea, I think it would be more fun to play with ascii-art directly.</p>
<p>Other playing possibilities include a ray-tracer. You could write some fps with ray-tracing ascii-art graphics, because there are so few pixels.<br />
Maybe I&#8217;ll write one, when I get to it.</p>
<p>Oh! The code, what about the code?<br />
<a href="http://www.algorithm.co.il/sitecode/afp.tar.gz">Here it is</a>.<br />
A few notes:<br />
1. The code ain&#8217;t too pretty, as it is a work in progress that doesn&#8217;t get much time invested in it. However, I&#8217;d still rather publish it.<br />
2. It uses curses. Under windows you&#8217;ll have to get some <a href="http://adamv.com/dev/python/curses/">replacement module</a>.<br />
3. It uses numpy. If you don&#8217;t have it already, be sure to get it.<br />
4. Run  &#8216;test_cube.py&#8217;. Use the direction keys to rotate the cube, &#8216;q&#8217; to exit.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/python-curses-numpy-ascii-art-rotating-cube/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A second look at the dragon fractal</title>
		<link>http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 13:08:06 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[dragon fractal]]></category>
		<category><![CDATA[ffmpeg]]></category>
		<category><![CDATA[PIL]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/a-second-look-at-the-dragon-fractal/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/' addthis:title='A second look at the dragon fractal'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>At first when I drew the (twin)dragon fractal, I had a small bug. I used the base 1+i instead of 1-i. This also generated a very similar looking beast. Thinking about that for a while, made me curious. Just like &#8230; <a href="http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/' addthis:title='A second look at the dragon fractal' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>At first <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-3-the-dragon/">when I drew</a> the (twin)dragon fractal, I had a small bug. I used the base 1+i instead of 1-i. This also generated a very similar looking beast. Thinking about that for a while, made me curious. Just like the Mandelbrot set, maybe other interesting fractals could be generated using exactly the same method, with a different complex &#8216;seed&#8217;.</p>
<p>So I patched up my code, and made it output a series of images for complex numbers on a path. I thought the reasonable choice would be a spiral, so using t*(cost+isint) as a base I wrote a spiral that would go around the origin several times and finally land on 1-i.<br />
It might seem obvious to you to try and make this interactive instead &#8211; why not move the mouse around and watch the different fractals that emerge? Well, I wanted a video.</p>
<p>I also wanted the fractal to convey a little more information. So each point in the set was given a color according to its generation. I decided after some trial and error that white was better for the older generation, and red for younger generations.<br />
After some PIL and ffmpeg work, <a href="http://www.algorithm.co.il/sitecode/dragon_movie.mpg.tar.gz">it was ready</a>.</p>
<p>While working on the video, I witnessed some very interesting fractals with different seeds.<br />
I was very curious as to why certain shapes emerged. For example, a square pattern was very common:<br />
<img src="http://www.algorithm.co.il/sitecode/squares.jpg" alt="Squares fractal" /><br />
This turned out to be not that surprising. Its generator number is of the shape 0+ia. So it made sense. I still didn&#8217;t figure out how come hexagon shapes were so common:<br />
<img src="http://www.algorithm.co.il/sitecode/colored_dragon682.jpg" alt="Hexagons1" /> <img src="http://www.algorithm.co.il/sitecode/colored_dragon683.jpg" alt="Hexagons2" /> <img src="http://www.algorithm.co.il/sitecode/colored_dragon751.jpg" alt="Hexagons1" /></p>
<p>Maybe it had something to do with pi/6, I&#8217;m not too sure about that. If it did, I would expect to see many more regular polygons.<br />
Here is another curious one:<br />
<img src="http://www.algorithm.co.il/sitecode/colored_dragon700.jpg" alt="holes" /></p>
<p>Another interesting phenomenon was what I started to call &#8216;the dragon&#8217;s heart&#8217;. You see, in the final dragon, the starting generations were spread about pretty evenly. However, with other bases, even ones generating something which is pretty similar to the dragon, the starting generations are clustered together &#8211; sometimes at the side, sometimes at the middle.</p>
<p>I&#8217;ve got a feeling (not proven or anything) that to be space filling, a fractal&#8217;s starting generations should be spread about. What do you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/a-second-look-at-the-dragon-fractal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fractals in 10 minutes No. 2 &#8211; Recursive Spring</title>
		<link>http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 12:00:17 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/fractals-in-10-minutes-no-2-recursive-spring/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/' addthis:title='Fractals in 10 minutes No. 2 &#8211; Recursive Spring'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Imagine a straight wire. Bend this wire until its ends meet. You get a ring. Next stage. Take another straight wire, bend it as before, but this time, don&#8217;t let the ends meet, instead continue bending the wire more and &#8230; <a href="http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/' addthis:title='Fractals in 10 minutes No. 2 &#8211; Recursive Spring' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Imagine a straight wire. Bend this wire until its ends meet. You get a ring.  Next stage.<br />
Take another straight wire, bend it as before, but this time, don&#8217;t let the ends meet, instead continue bending the wire more and more, until you get a spring. Now, Think of this spring as the first wire, and bend it until the spring&#8217;s ends meet. We get a spring-ring. (See pictures below)</p>
<p>At this point it should be obvious &#8211; instead of making the ends of the spring meet, we can continue bending the spring into a meta-spring, and again make this meta-spring&#8217;s ends meet. Rinse and repeat ad-infinitum.</p>
<p>At first I had a hard time picturing it, and I thought it would be tough to write about it without drawing it. So I set with pen and paper, and tried to draw it. That was even harder than picturing it. So I thought I could write a script to draw it, and that turned out to be pretty easy.</p>
<p>Here are the first four stages. The second and third are shown from the side. Click for bigger versions.<br />
<a href="http://www.algorithm.co.il/sitecode/s1.png"><img src="http://www.algorithm.co.il/sitecode/s1_small.png" align="left" /></a><a href="http://www.algorithm.co.il/sitecode/s2.png"><img src="http://www.algorithm.co.il/sitecode/s2_small.png" /></a></p>
<p><a href="http://www.algorithm.co.il/sitecode/s3.png"><img src="http://www.algorithm.co.il/sitecode/s3_small.png" /></a><a href="http://www.algorithm.co.il/sitecode/s4.png"><img src="http://www.algorithm.co.il/sitecode/s4_small.png" /></a></p>
<p>To implement this, I wrote a function that draws any track. A track is defined to be a function takes a float argument between 0 and 1, and returns the appropriate 3d point in between, where 0 is one end of the track and 1 is the other. (Actually, I also decided that tracks should return a &#8220;right hand&#8221; vector as well). Then I wrote a ring:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> ring<span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span>:
    pi = <span style="color: #dc143c;">math</span>.<span style="color: black;">pi</span>
    cos = <span style="color: #dc143c;">math</span>.<span style="color: black;">cos</span>
    sin = <span style="color: #dc143c;">math</span>.<span style="color: black;">sin</span>
    x = numpy.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>cos<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>pi<span style="color: #66cc66;">*</span>t<span style="color: black;">&#41;</span>,<span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>sin<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>pi<span style="color: #66cc66;">*</span>t<span style="color: black;">&#41;</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>x,-x/<span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Lastly I wrote a wrapper function, that takes any track, and springifies it. It was called springify_track. At this point I could do this:<br />
(The constants are the radius of the spring loops, the sampling delta, and the number loops.)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">t1 = ring
t2 = springify_track<span style="color: black;">&#40;</span>t1, <span style="color: #ff4500;">0.25</span>, <span style="color: #ff4500;">0.1</span>, <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span>
t3 = springify_track<span style="color: black;">&#40;</span>t2, <span style="color: #ff4500;">0.06</span>, <span style="color: #ff4500;">0.0011</span>, <span style="color: #ff4500;">5000</span><span style="color: black;">&#41;</span>
t4 = springify_track<span style="color: black;">&#40;</span>t3, <span style="color: #ff4500;">0.011</span>, <span style="color: #ff4500;">0.000012</span>, <span style="color: #ff4500;">500000</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/fractals-in-10-minutes-no-2-recursive-spring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Elegant Line Clipping Algorithm</title>
		<link>http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/</link>
		<comments>http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 21:29:04 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Geometry]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Line-Clipping]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/index.php/programming/python/elegant-line-clipping-algorithm/</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/' addthis:title='Elegant Line Clipping Algorithm'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I actually wrote about that a long time ago, in the wiki that once was algorithm.co.il. Since the wiki is long gone, and I figured this is a very elegant algorithm, I decided I should write about it. First, some &#8230; <a href="http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/' addthis:title='Elegant Line Clipping Algorithm' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I actually wrote about that a long time ago, in the wiki that once was algorithm.co.il. Since the wiki is long gone, and I figured this is a very elegant algorithm, I decided I should write about it.</p>
<p>First, some background &#8211; while writing <a href="http://www.algorithm.co.il/blogs/index.php/ascii-plotter/" title="Ascii-Plotter">Ascii-Plotter</a>, I had to plot lines. To plot lines correctly I had to clip them. Now since this was a fun project, I decided I wanted to figure out how to do that myself, even though the problem is really solved. So I sat down with pen and paper, and did a little thinking, and I came up this nice algorithm.</p>
<p>The idea is simply this: consider a line, that is not parallel to any of the axes. This line intersects all the four lines that make up the limits of our screen. Here is a diagram:</p>
<p><img src="http://www.algorithm.co.il/sitecode/line_clipping1.png" alt="An intersection of a line with a bounding box" height="242" width="334" /></p>
<p>Now notice that there are six points of interest: the four intersection points, and the two endpoints of the line. For each point, we find its parameter in the parametric representation of the line, where a and b are the endpoints:</p>
<p>x(t) = a+t(b-a)</p>
<p>To find out the actual endpoints of the clipped line, we sort the six points according to their parameter, and take the two innermost points (with zero based indexing, those would be 2 and 3). What if the line is outside the box you say? We check that the result points&#8217; parameters are between 0 and 1 (which correspond to the original endpoints).</p>
<p>Note that for a line parallel to one of the axes, a similar method applies, but without two of the intersection points.  However, it is probably easier to just calculate the clipping directly in that case.</p>
<p>Here is some sample code, without all the surrounding checks (for parallelism with the axes, trivial clipping etc&#8230;).<br />
The full version is available in the code of <a href="http://www.algorithm.co.il/blogs/index.php/ascii-plotter/" title="Ascii-Plotter">Ascii-Plotter</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">points_t = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0.0</span>,<span style="color: #ff4500;">1.0</span><span style="color: black;">&#93;</span>
&nbsp;
points_t.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>rect_bottom_left<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: black;">&#40;</span>line_pt_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
points_t.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>rect_top_right<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: black;">&#40;</span>line_pt_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
points_t.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>rect_bottom_left<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: black;">&#40;</span>line_pt_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
points_t.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>rect_top_right<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>/<span style="color: black;">&#40;</span>line_pt_2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-line_pt_1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
points_t.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">or</span> points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">1</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
result = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span>pt_1 + t<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>pt_2-pt_1<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> t <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span>points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>,points_t<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: black;">&#40;</span>pt_1, pt_2<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">zip</span><span style="color: black;">&#40;</span>line_pt_1, line_pt_2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,result<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span>result<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>, result<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/elegant-line-clipping-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

