<?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>eDesign.nl</title>
	<atom:link href="http://www.edesign.nl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.edesign.nl</link>
	<description>Thoughts and concepts on software development</description>
	<lastBuildDate>Fri, 08 Apr 2011 13:35:29 +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>HTML5 Canvas experiments</title>
		<link>http://www.edesign.nl/2011/03/23/html5-canvas-experiments/</link>
		<comments>http://www.edesign.nl/2011/03/23/html5-canvas-experiments/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 21:45:11 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=841</guid>
		<description><![CDATA[

HTML5 is supported by more and more browsers. It has a nice feature called the &#8216;canvas&#8217; element. It is an element on which you can &#8216;paint&#8217; using e.g. JavaScript. I experimented with this new canvas element. You can view the experiments (clock, 3D-axis and rotation, ray tracer) and view the source too! The ray tracer [...]]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">
<p><a href="http://www.edesign.nl/wp-content/uploads/2011/03/03-01_html5_canvas_element_ld_img.png"><img class="size-medium wp-image-843 alignleft" title="HTML5 Canvas element" src="http://www.edesign.nl/wp-content/uploads/2011/03/03-01_html5_canvas_element_ld_img-300x109.png" alt="HTML5 Canvas element" width="150" height="54" /></a></p>
<p>HTML5 is supported by more and more browsers. It has a nice feature called the &#8216;canvas&#8217; element. It is an element on which you can &#8216;paint&#8217; using e.g. JavaScript. I experimented with this new canvas element. You can view <a href="http://www.edesign.nl/examples/html5/" target="_blank">the experiments</a> (<a href="http://www.edesign.nl/examples/html5/clock/index.php?mode=ns" target="_blank">clock</a>, <a href="http://www.edesign.nl/examples/html5/rotate/" target="_blank">3D-axis and rotation</a>, <a href="http://www.edesign.nl/examples/html5/raytrace/" target="_blank">ray tracer</a>) and view the source too! The ray tracer should be activated in Chrome or Safari as Firefox and Internet Explorer tend to be (very) slow.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2011/03/23/html5-canvas-experiments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Avoid becoming a spam source</title>
		<link>http://www.edesign.nl/2010/08/21/avoid-becoming-a-spam-source/</link>
		<comments>http://www.edesign.nl/2010/08/21/avoid-becoming-a-spam-source/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 22:03:17 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Spam protection]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=818</guid>
		<description><![CDATA[The number one rule in programming is not to trust external data, especially not user data. One of the reasons not to trust user data is the threat of hackers searching for injection possibilities. When you start develloping web applications you&#8217;ll soon learn the dangers of SQL injection. This might be the most important form [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.edesign.nl/wp-content/uploads/2010/08/spam.jpg"><img class="alignleft size-thumbnail wp-image-819" title="SPAM" src="http://www.edesign.nl/wp-content/uploads/2010/08/spam-150x150.jpg" alt="" width="150" height="150" /></a>The number one rule in programming is not to trust external data, especially not user data. One of the reasons not to trust user data is the threat of hackers searching for injection possibilities. When you start develloping web applications you&#8217;ll soon learn the dangers of SQL injection. This might be the most important form of injection, but by far it is not the only one out there: cross site scripting is another good example. If your application sends out mail, you might want to make sure you sanitize user input as well as the application could be a magnet for spammers who&#8217;ll send spam mail using your IP.<span id="more-818"></span></p>
<p>Mail is formatted using <a href="http://en.wikipedia.org/wiki/MIME" target="_blank">MIME</a>, a content discription format. In this format, all properties of an email are specified in the header of the message. Examples include &#8216;From:&#8217;, &#8216;Subject:&#8217; and &#8216;Date:&#8217; for instance and also &#8216;To:&#8217; and &#8216;Cc:&#8217; are headers in the MIME.</p>
<p>Mail is generally sent using SMTP. In contrast to MIME, the message format description, SMTP is a protocol. One can tell a mail server (SMTP) the recipients of an email using this protocol. Therefor these recipients set with SMTP can be different than the ones specified in the MIME message (ever noticed a header like &#8216;undisclosed recipients&#8217;, which is clearly not a valid email address).</p>
<p>Because it is redundant at a high programming level, like PHP, to set recipients twice (once in MIME and once in SMTP), methods are defined for sending mail. PHP&#8217;s mail() function for example accepts &#8216;to&#8217;, &#8217;subject&#8217; and &#8216;message&#8217; parameters. Also, since the number of different MIME headers are virtually infinite, it also accepts an &#8216;additional headers&#8217; parameter. This is one part of introducing a potential security breach.</p>
<div style="float: right; width: 300px; border: 1px solid #CCCCCC; padding: 3px; font-family: calibri; line-height: 20px; font-size: 12px;">This actually is a disputable &#8216;violation&#8217; of the headers. As you might have noticed, this way you&#8217;ll have to add Bcc recipients to the message&#8217;s header. As by definition, you don&#8217;t want other recipients to be able to see Bcc recipients as part of the message they recieve so the smtp client will have to strip it before sending.</div>
<p>The other part is in the function&#8217;s handling of the additional headers parameter. Of course it has to be possible to send mail using recipients specified as Cc or Bcc instead of To. To this end mail() examines the parameter for these headers and extracts them. As headers are specified per line in the MIME format, you can inject headers to the &#8216;additional headers&#8217; parameter if one has access to a field in it, let&#8217;s say &#8216;Date:&#8217; or &#8216;From:&#8217;. Headers are separated using a line feed character (LF), \n in PHP or %0A url encoded.</p>
<p>A MIME email usually has the following layout:</p>
<pre>To: you@yourdomain.com
From: me@mydomain.com
Subject: Hi there

I'm just mailing to say hello!</pre>
<p>Now if you have your website visitors send you mail and have them set the &#8216;From:&#8217; header contents for instance, they might fill out an arbitrary email address and add &#8216;%0ABcc: extra@email.com; extra2@email.com;&#8230; (etc)&#8217;. Now also an Bcc header is set and your mail form just became a spam robot. Workaround is simple: don&#8217;t allow newlines in MIME header fields! Another post about this common pittfall can be found on <a href="http://www.websiterepairs.net/smtpinjection.php" target="_blank">websiterepairs.net</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2010/08/21/avoid-becoming-a-spam-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Decoration aid</title>
		<link>http://www.edesign.nl/2010/08/03/decoration-aid/</link>
		<comments>http://www.edesign.nl/2010/08/03/decoration-aid/#comments</comments>
		<pubDate>Tue, 03 Aug 2010 14:36:28 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=783</guid>
		<description><![CDATA[When it comes to designing websites choosing what colors to use is one of the first steps. Nowadays online applications often let users personalize the look and feel of the site by offering a way to customize the colors the website wears. JavaScript and HTML can be used effectively to this end. A problem is [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.edesign.nl/wp-content/uploads/2010/08/Runge_Farbenkugel3.jpg"><img class="alignleft size-thumbnail wp-image-791" title="Runge Farbenkugel" src="http://www.edesign.nl/wp-content/uploads/2010/08/Runge_Farbenkugel3-150x150.jpg" alt="" width="150" height="150" /></a>When it comes to designing websites choosing what colors to use is one of the first steps. Nowadays online applications often let users personalize the look and feel of the site by offering a way to customize the colors the website wears. JavaScript and HTML can be used effectively to this end. A problem is arising for component developers using JavaScript: what framework to use? There is <a href="http://mootools.net/" target="_blank">MooTools</a>, <a href="http://www.prototypejs.org/" target="_blank">ProtoType</a>, <a href="http://developer.yahoo.com/yui/" target="_blank">YUI library</a>, <a href="http://jquery.com/" target="_blank">jQuery</a>, etc, etc&#8230; This is the reason I developed a standalone <a href="http://www.edesign.nl/colorpicker/" target="_self">color picker</a>. It also works with every JavaScript framework, as it does not depend on one or interferes with one. It is one single .js file, no extra images, css, javascript or whatsoever.</p>
<p><span id="more-783"></span></p>
<p>Using it is easy. Basic usage has tree steps.</p>
<ol>
<li>Include the colorpicker.js in the &lt;head&gt; of the HTML file.</li>
<li>Add the class &#8216;colorpicker&#8217; to the &lt;input&gt; tag you use in a form for the color to choose.</li>
<li>On document load, initialize the color picker with: ColorPicker.init();</li>
</ol>
<p>It is as simple as that. Example:</p>
<pre>&lt;hmtl&gt;</pre>
<pre> &lt;head&gt;</pre>
<pre>  &lt;title&gt;ColorPicker example&lt;/title&gt;</pre>
<pre>  &lt;script type="text/javascript" src="colorpicker.js" /&gt;</pre>
<pre> &lt;/head&gt;</pre>
<pre> &lt;body onload="ColorPicker.run();"&gt;</pre>
<pre>  &lt;form&gt;</pre>
<pre>   Choose a color: &lt;input type="text" name="color" class="colorpicker" /&gt;</pre>
<pre>   &lt;input type="submit" /&gt;</pre>
<pre>  &lt;/form&gt;</pre>
<pre> &lt;/body&gt;</pre>
<pre>&lt;/html&gt;</pre>
<p>Multiple pickers can be added by inserting more &lt;input&gt; tags with the classname &#8216;colorpicker&#8217; which the standard ColorPicker.init() method all transforms into color pickers.</p>
<p>The manual way to initialize color pickers is by calling:</p>
<pre>new ColorPicker(element);</pre>
<p>You&#8217;ll need to repeat that line for every color picker you have if you don&#8217;t want to use ColorPicker.init().</p>
<h2>Customizing</h2>
<p>The look and feel of the color picker can be modified by adding an configuration hash table as argument to the init() function. This way you can change the colors, borders and language to your needs. For instance, in Dutch the word &#8216;Cancel&#8217; means &#8216;Annuleren&#8217;. So if I want a color picker on a Dutch website I would change the init() call to:</p>
<pre>ColorPicker.init({'language': {'cancel': 'Annuleren'}})</pre>
<p>This way you can set parts of the configuration (e.g. only the word &#8216;cancel&#8217;) or the entire configuration by extending the hash table.</p>
<p><a href="http://www.edesign.nl/wp-content/uploads/2010/08/Capture.png"><img class="alignnone size-full wp-image-803" title="Color Picker" src="http://www.edesign.nl/wp-content/uploads/2010/08/Capture.png" alt="" width="385" height="386" /></a></p>
<h2>Download</h2>
<p>You can get the latste version of the ColorPicker from <a href="http://projects.edesign.nl/colorpicker/latest/colorpicker.js" target="_blank">http://projects.edesign.nl/colorpicker/latest/colorpicker.js</a>. The project page is available at <a href="http://projects.edesign.nl/colorpicker/" target="_blank">http://projects.edesign.nl/colorpicker/</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2010/08/03/decoration-aid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>No robots beyond this line</title>
		<link>http://www.edesign.nl/2010/04/23/no-robots-beyond-this-line/</link>
		<comments>http://www.edesign.nl/2010/04/23/no-robots-beyond-this-line/#comments</comments>
		<pubDate>Fri, 23 Apr 2010 17:43:30 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Spam protection]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=729</guid>
		<description><![CDATA[Online communities are hot. Globally recognized examples are easy to give: websites like Facebook, LinkedIn and are very popular, manufacturers have online fora to have their customers support each other, newspapers let you leave comments on their articles on their websites and you can share everything with tools like Delicious, Digg and Reddit. This development [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-751" href="http://www.edesign.nl/2010/04/23/no-robots-beyond-this-line/customs/"><img class="alignleft size-thumbnail wp-image-751" title="Customs passport check" src="http://www.edesign.nl/wp-content/uploads/2010/04/customs-150x150.jpg" alt="Customs passport check" width="150" height="150" /></a>Online communities are hot. Globally recognized examples are easy to give: websites like <a href="http://www.facebook.com/" target="_blank">Facebook</a>, <a href="http://www.linkedin.com/" target="_blank">LinkedIn</a> and are very popular, manufacturers have <a href="http://vip.asus.com/forum/default.aspx?SLanguage=en-us" target="_blank">online fora</a> to have their customers support each other, newspapers let you leave <a href="http://community.nytimes.com/comments/bits.blogs.nytimes.com/2010/04/06/yelp-makes-changes-to-appease-small-business-owners/?sort=newest&amp;scp=1&amp;sq=captcha&amp;st=cse#postComment" target="_blank">comments</a> on their articles on their websites and you can share everything with tools like <a href="http://delicious.com/" target="_blank">Delicious</a>, <a href="http://digg.com/" target="_blank">Digg</a> and <a href="http://www.reddit.com/" target="_blank">Reddit</a>. This development on the Internet supports new possibilities which were unknown before. Of course this also counts for rogues. <a href="http://en.wikipedia.org/wiki/Spam_%28electronic%29" target="_blank">Spam</a> is a commonly known phenomenon and global annoyance. Beside spamming unwanted messages by mail, spamming the comment boxes and fora is an issue web programmers have to deal with too. Spamming often is automated and this is a feature which can be used to counter spam. The goal is to identify a messenger being human or robot.</p>
<p>For this purpose the <a href="http://en.wikipedia.org/wiki/CAPTCHA" target="_blank">captcha</a> was invented. Besides the fact that captcha sounds nice enough to be a buzz word it actually is short for Completely Automated Public Turing test to tell Computers and Humans Apart, although this is a bit contrived. This means that a captcha is a challenge response mechanism but it doesn&#8217;t need to be in the form of an image depicting distorted text which has to be copied in a text box which is the most common form of captchas. Creative new captchas can be found, like a transistor image which has to be read.<span id="more-729"></span></p>
<h3>Technology</h3>
<p>Wikipedia mentions a couple of features a captcha must have to qualify as one. A captcha is a <a href="http://www.edesign.nl/2009/05/05/challenge-hash/" target="_self">challenge-response</a> test between a system and a user of which</p>
<ol>
<li>current software is unable to solve accurately,</li>
<li>most humans can solve, and</li>
<li>does not rely on the type of CAPTCHA being new to the attacker.</li>
</ol>
<p>The first one remarks a captcha as temporal. This means that with increasing processing power and increasing insight in artificial intelligence challenges we now consider to be captchas might not be in the (near) future. Philosophically, this means that captchas are a temporary phenomenon because mankind will eventually be able to build robots which are at least as intelligent as humans are. But for now they&#8217;ll do.</p>
<p>The second one emphasises the differences we see between humans and robots. This is actually quite an interesting point because mankind actually admits its current limitations in its own intelligence being unable to write software which is able to solve &#8216;puzzles&#8217; which are easy to solve for humans.</p>
<p>Which bridges to the last bullet on the list. Of course you could just add a simple checkbox labeled &#8220;Do not check this box if you are human&#8221;. No attacker would think of a spam protection this weak but because of that it might just work. The robot stumbling across your comment submission form does not expect such a protection and therefor cannot bypass it. Although this does not qualify as a captcha because the novelty of the protection will only make the attacker look into it to solve it in an instance. Of course a captcha can be a captcha when it is an innovative challenge but it should not rely on being unknown. <a href="http://en.wikipedia.org/wiki/Security_through_obscurity" target="_blank">Security through obscurity</a> is not security at all.</p>
<h3>Processing power and algorithms</h3>
<p>As time passes by technology advances resulting in more processing power in both processor quantity and quality and more mathematical developments. These are the engine propelling artificial intelligence development. On the other hand, having this field of computer science developing the struggle for making captchas to tell humans and computers apart becomes harder and harder. Who knows when software becomes as advanced as to be able to not only solve puzzles or identify puzzle types but to be really intelligent and thereby be able to find ways to solve a puzzle without knowing the puzzle&#8217;s rules in advance? 20 years? 30? 5?!</p>
<h3>Usefulness</h3>
<p>Captcha&#8217;s can be useful too. The <a href="http://recaptcha.net/" target="_blank">Recaptcha</a> program for instance helps digitizing books by showing snippets scanned from books which they are unable to parse with their <a href="http://en.wikipedia.org/wiki/Optical_character_recognition" target="_blank">OCR software</a>. This way the snippets are &#8216;decyphered&#8217; by hundreds of people insuring accuracy and helping the system in which it is implemented to be bot-free.</p>
<p>Other examples of captchas might be usefull to the website&#8217;s theme such as a math class forum&#8217;s captcha challenging users with simple math like <img src="http://www.edesign.nl/wp-content/cache/tex_f8f58d4d35433e1e2eaf7438fd68073d.png" align="absmiddle" class="tex" alt="3 + 5 =" /> or <img src="http://www.edesign.nl/wp-content/cache/tex_8dae6646d5937556b6a8bcc3c53f935c.png" align="absmiddle" class="tex" alt="4 * 3 =" />. Another example of such a situated captcha is Adafruit&#8217;s. <a href="http://www.adafruit.com/blog/" target="_blank">Adafruit</a> is a website and webshop on the <a href="http://www.arduino.cc/" target="_blank">Arduino</a>, which is a do it yourself programmable breadboard. You&#8217;ll need to &#8216;read&#8217; the resistor&#8217;s value in order to post a comment.</p>
<div id="attachment_758" class="wp-caption alignleft" style="width: 526px"><a rel="attachment wp-att-758" href="http://www.edesign.nl/2010/04/23/no-robots-beyond-this-line/capture/"><img class="size-full wp-image-758" title="Adafruit's resistor captcha" src="http://www.edesign.nl/wp-content/uploads/2010/04/capture.png" alt="Adafruit's resistor captcha" width="516" height="351" /></a><p class="wp-caption-text">Adafruit&#39;s resistor captcha makes you slide the four sliders to match the color code on the resistor above.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2010/04/23/no-robots-beyond-this-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Game of Office Decoration</title>
		<link>http://www.edesign.nl/2010/04/22/the-game-of-office-decoration/</link>
		<comments>http://www.edesign.nl/2010/04/22/the-game-of-office-decoration/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 17:48:17 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=730</guid>
		<description><![CDATA[This week I had a discussion with a colleague of mine about decoration for the new office rooms we hired. We thought of an Arduino based project, because it would just be cool to have some fun with the gadget.
We could make a giant board filled with buttons with a led in them. You&#8217;d press a button [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-731" href="http://www.edesign.nl/2010/04/22/the-game-of-office-decoration/life_logo/"><img class="alignleft size-thumbnail wp-image-731" title="Life Logo" src="http://www.edesign.nl/wp-content/uploads/2010/04/life_logo-150x150.png" alt="Life Logo" width="135" height="135" /></a>This week I had a discussion with a colleague of mine about decoration for the new office rooms we hired. We thought of an <a href="http://www.arduino.cc/" target="_blank">Arduino</a> based project, because it would just be cool to have some fun with <a href="http://www.adafruit.com/blog/2009/11/30/the-adafruit-gps-shield-for-arduino-in-an-aircraft/" target="_blank">the gadget</a>.</p>
<p>We could make a giant board filled with buttons with a led in them. You&#8217;d press a button to toggle it. This board could be controlled by an Arduino and display several things on it. A <a href="http://technabob.com/blog/2008/11/16/persistence-of-vision-led-clock/" target="_blank">clock</a> for instance or <a href="http://cdn.wn.com/ph/img/72/de/ab0a744b1f98ccf7a805906d8f76-grande.jpg" target="_blank">news headlines</a> from an arbitrary RSS feed or&#8230; since they&#8217;re buttons, <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life" target="_blank">Conway&#8217;s Game of Life</a>!<span id="more-730"></span></p>
<p>Conway&#8217;s Game of Life is a cellular automaton. It is a discrete model which can used in studies like mathematics, theoretical biology and physics. The rules are pretty simple. There is a grid filled with cells which are alive or dead. In each &#8216;turn&#8217; (iteration) the number of alive neighbors are counted for every cell. If a cell is currently dead and it has exactly three alive neighbor cells that cell will be alive in the next iteration. If a cell is alive and it has less than two or more than three alive neighbors it dies. This way you can model certain patterns with it, run the simulation game for a couple of iterations and see what it does.</p>
<p>I found implementations in <a href="http://www.bitstorm.org/gameoflife/" target="_blank">Java</a> and <a href="http://www.codeproject.com/KB/cs/GameOfLifeMultipleInherit.aspx" target="_blank">C#</a>. I made one in JavaScript using HTML for the gui. <a href="http://www.edesign.nl/examples/gol/" target="_blank">Check it out and play with it</a>. There is a lot of information on this game on Wikipedia, including all kinds of &#8216;<a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life#Examples_of_patterns" target="_blank">famous patterns</a>&#8216; and many links.</p>
<p><a href="http://www.edesign.nl/examples/gol/" target="_blank">Conway&#8217;s Game of Life example</a> is viewed best with Google Chrome or Safari (because of <a href="http://webkit.org/" target="_blank">WebKit</a>). It does work in other browsers too but way slower, especially Internet Explorer isn&#8217;t very good in processing the Javscript game.</p>
<p>UPDATE: I found another site explaining the mathematical interesting nature of <a href="http://www.math.com/students/wonders/life/life.html" target="_blank">Conway&#8217;s Game of Life</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2010/04/22/the-game-of-office-decoration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Math behind a world sunlight map</title>
		<link>http://www.edesign.nl/2009/05/14/math-behind-a-world-sunlight-map/</link>
		<comments>http://www.edesign.nl/2009/05/14/math-behind-a-world-sunlight-map/#comments</comments>
		<pubDate>Thu, 14 May 2009 00:59:29 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Image processing]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=434</guid>
		<description><![CDATA[My neighbour has a map of the world on the wall. You can see it from the street in front of his house. It has a backlight but that only illuminates half of the map. The transition from day to night is shaped like a sine wave most of the time. It actually is a [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-436" href="http://www.edesign.nl/2009/05/14/math-behind-a-world-sunlight-map/full-17572/"><img class="alignleft size-thumbnail wp-image-436" title="World sunlight map fraction" src="http://www.edesign.nl/wp-content/uploads/2009/05/full-17572-150x150.jpg" alt="World sunlight map fraction" width="150" height="150" /></a>My neighbour has a map of the world on the wall. You can see it from the street in front of his house. It has a backlight but that only illuminates half of the map. The transition from day to night is shaped like a sine wave most of the time. It actually is a <a href="http://www.geochronusa.com/new_geo/" target="_blank">physical world sunlight map</a>. Of course, you can <a href="http://www.die.net/earth/" target="_blank">simulate this </a>with a computer too. There even is an <a href="http://www.daylightmap.com/index.php" target="_blank">instance using Google maps</a>.</p>
<p>As many roads lead to Rome multiple ways are possible to this simulation. One could model the sun, earth, maybe more and start <a href="http://en.wikipedia.org/wiki/Ray_tracing_(graphics)" target="_blank">ray tracing</a>. This approach would include solar eclipses but is quite heavy by means of the load on the processor. Because of the number of calculations involved in ray tracing is quite high. The way I choose to describe fully in this article is one close to it. Using <a href="http://en.wikipedia.org/wiki/Euclidean_vector" target="_blank">vectors</a> pointing from a sphere (<a href="http://en.wikipedia.org/wiki/Earth" target="_blank">earth</a>) to a point (<a href="http://en.wikipedia.org/wiki/Sun" target="_blank">sun</a>) I map a <a href="http://en.wikipedia.org/wiki/Mercator_projection" target="_blank">Mercator projected map</a> of the world on the sphere. The challenges included are the yearly orbit of earth around the sun and it&#8217;s 23.5° tilted 24 hour spin.<span id="more-434"></span></p>
<p>About a decade ago I made a similar program in highschool. I didn&#8217;t know vectors and <a href="http://en.wikipedia.org/wiki/Trigonometry" target="_blank">trigonometry</a> then as I do now. Back then I used to have the map of the world where for each longitude (the ones parallel to the equator) I calculated sunset and sunrise times for that longitude. This way I knew when to start painting night over the daylight map I had. This result was pretty accurate and an <a href="http://users.electromagnetic.net/bu/astro/sunrise-set.php" target="_blank">algorithm to determine those solar times</a> is not hard to implement. This time I used another more advanced approach to the problem.</p>
<h2>Space</h2>
<p>First let us define a mathematical 3D space, with three axis: <img src="http://www.edesign.nl/wp-content/cache/tex_9dd4e461268c8034f5c8564e155c67a6.png" align="absmiddle" class="tex" alt="x" /> (pointing from left to right), <img src="http://www.edesign.nl/wp-content/cache/tex_415290769594460e2e485922904f345d.png" align="absmiddle" class="tex" alt="y" /> (pointing up) and <img src="http://www.edesign.nl/wp-content/cache/tex_fbade9e36a3f36d3d676c1b808451dd7.png" align="absmiddle" class="tex" alt="z" /> (pointing into your monitor). Pointing directions are given to help your imagination. Axis point from small (negative infinite) to big (positive infinite), they intersect at <img src="http://www.edesign.nl/wp-content/cache/tex_80c93660da9d0792e674ba1085c32ace.png" align="absmiddle" class="tex" alt="O_{x,y,z} = (0, 0, 0)" />.</p>
<h2>Earth</h2>
<p>Define a sphere on point <img src="http://www.edesign.nl/wp-content/cache/tex_f186217753c37b9b9f958d906208506e.png" align="absmiddle" class="tex" alt="O" />, with a radius of 1. This will be the earth. The funny thing is that for every point<img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" />(x,y,z coordinate) on the earth, because the earth is centered around <img src="http://www.edesign.nl/wp-content/cache/tex_f186217753c37b9b9f958d906208506e.png" align="absmiddle" class="tex" alt="O" />, automatically is the same a the vector pointing orthoganally away from the earth starting on that point<img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" />. This comes in handy when we do calculations later on.</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_33380c0b9e4cdcdf3e81f1180aea86ba.png" title="\vec{p} = (x, y, z)" class="etex" alt="\vec{p} = (x, y, z)" /></center></p>
<h2>Sun</h2>
<p>We don&#8217;t need the sun for this method. We only need the direction to it from the earth. So define a vector <em>S</em> which points in the direction of the sun from <img src="http://www.edesign.nl/wp-content/cache/tex_f186217753c37b9b9f958d906208506e.png" align="absmiddle" class="tex" alt="O" />. To do this we must use sine and cosine. On time <img src="http://www.edesign.nl/wp-content/cache/tex_1f48e973d6a9075dbaaf41a9e85f034e.png" align="absmiddle" class="tex" alt="t = 0" /> the direction of the sun will be <img src="http://www.edesign.nl/wp-content/cache/tex_4967dfa815448036b242326491623bad.png" align="absmiddle" class="tex" alt="(1, 0, 0)" />, which is a vector pointing to the right. On <img src="http://www.edesign.nl/wp-content/cache/tex_7fcd6686b2518bb9eefd38d7531edfba.png" align="absmiddle" class="tex" alt="t = 12" /> (hour) that should be pointing right <img src="http://www.edesign.nl/wp-content/cache/tex_d12de2b341d428af7c1eeab87422c070.png" align="absmiddle" class="tex" alt="(-1, 0, 0)" />. The same goes for 6 o&#8217;clock am and pm, being respectively <img src="http://www.edesign.nl/wp-content/cache/tex_8da033c96917f68a2d4465bf831bf683.png" align="absmiddle" class="tex" alt="(0, 1, 0)" /> and <img src="http://www.edesign.nl/wp-content/cache/tex_069359415cada53c746dae706831bb51.png" align="absmiddle" class="tex" alt="(0, -1, 0)" />. This gives us insight that the direction <img src="http://www.edesign.nl/wp-content/cache/tex_954322810f486ac24f0d70218627d2a0.png" align="absmiddle" class="tex" alt="\vec{d}" /> to the sun can be given as (<img src="http://www.edesign.nl/wp-content/cache/tex_e358efa489f58062f10dd7316b65649e.png" align="absmiddle" class="tex" alt="t" /> should be scaled to a value from 0 to 1, so we need to divide <img src="http://www.edesign.nl/wp-content/cache/tex_e358efa489f58062f10dd7316b65649e.png" align="absmiddle" class="tex" alt="t" /> by 24):</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_3150abfca1663b9bdd3db560ded4268b.png" title="\vec{d} =(\cos{\frac{2\pi t}{24}}, 0, \sin{\frac{2\pi t}{24}})" class="etex" alt="\vec{d} =(\cos{\frac{2\pi t}{24}}, 0, \sin{\frac{2\pi t}{24}})" /></center></p>
<p>With this approach we actually make the sun rotate around the earth.</p>
<h2>Tilted earth</h2>
<div id="attachment_459" class="wp-caption alignleft" style="width: 310px"><a rel="attachment wp-att-459" href="http://www.edesign.nl/2009/05/14/math-behind-a-world-sunlight-map/north_season/"><img class="size-medium wp-image-459" title="Earth tilted axis" src="http://www.edesign.nl/wp-content/uploads/2009/05/north_season-300x165.jpg" alt="Earth tilted axis" width="300" height="165" /></a><p class="wp-caption-text">Tilted orbit of the spinning earth around the sun. The method discussed here turns it around as it models the sun orbitting the earth.</p></div>
<p>The above formula still is not complete. The axis around which the earth is spinning is not orthogonal to the plane defined by the orbit of the earth around the sun. It is tilted 23.5°. This is how we get the four seasons on earth of course. To model this a nice trick can be applied to <em>d</em>. We know that on or closely around June 21th the sun is right above the 23.5° latitude (<a href="http://en.wikipedia.org/wiki/Tropic_of_Cancer" target="_blank">Tropic of Cancer</a>), the <a href="http://en.wikipedia.org/wiki/Solstice">summer solstice</a>. From here it takes a sine with a period of a year to go to -23.5° and back again. June 21th is the 172th day of the year. The standard cosine function has the form:</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_4059e79313887878e46828ae448a06bc.png" title="y = a \cdot \cos{(bx + c)} + d" class="etex" alt="y = a \cdot \cos{(bx + c)} + d" /></center></p>
<p>We can simply fill this out. The <img src="http://www.edesign.nl/wp-content/cache/tex_0cc175b9c0f1b6a831c399e269772661.png" align="absmiddle" class="tex" alt="a" /> must be 23.5, <img src="http://www.edesign.nl/wp-content/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.png" align="absmiddle" class="tex" alt="b" /> is <img src="http://www.edesign.nl/wp-content/cache/tex_744d20bf0129464fb19fee5dd435a613.png" align="absmiddle" class="tex" alt="\frac{2\pi}{365}" />, <img src="http://www.edesign.nl/wp-content/cache/tex_4a8a08f09d37b73795649038408b5f33.png" align="absmiddle" class="tex" alt="c" /> is -172 and <img src="http://www.edesign.nl/wp-content/cache/tex_8277e0910d750195b448797616e091ad.png" align="absmiddle" class="tex" alt="d" /> equals 0. If you define <img src="http://www.edesign.nl/wp-content/cache/tex_415290769594460e2e485922904f345d.png" align="absmiddle" class="tex" alt="y" /> the observable tilt (because the real tilt will always be 23.5) and <img src="http://www.edesign.nl/wp-content/cache/tex_9dd4e461268c8034f5c8564e155c67a6.png" align="absmiddle" class="tex" alt="x" /> is the day of the year:</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_9fd4ce29cabb03153f7a603bfe765419.png" title="observableTilt = 23.5 \cos{(\frac{2\pi}{365} ({dayOfYear} - 172))}" class="etex" alt="observableTilt = 23.5 \cos{(\frac{2\pi}{365} ({dayOfYear} - 172))}" /></center></p>
<p>Now this observable tilt is the angle there is between what is the real direction to the sun and the already calculated direction <em>d</em>. To calculate the vector (<img src="http://www.edesign.nl/wp-content/cache/tex_415290769594460e2e485922904f345d.png" align="absmiddle" class="tex" alt="y" /> component, pointing up or down) which should be added to <img src="http://www.edesign.nl/wp-content/cache/tex_954322810f486ac24f0d70218627d2a0.png" align="absmiddle" class="tex" alt="\vec{d}" /> to get the real direction you can use tangens.</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_bfe8c80f2cfd4c7d565b2c8084c718d0.png" title="\vec{tiltCorrection} = \vec{d} \cdot \tan{ 2\pi \frac{observableTilt}{360}}" class="etex" alt="\vec{tiltCorrection} = \vec{d} \cdot \tan{ 2\pi \frac{observableTilt}{360}}" /></center></p>
<p>Then the real direction vector <img src="http://www.edesign.nl/wp-content/cache/tex_0b41e62b40d2668f0de148e45d42f78e.png" align="absmiddle" class="tex" alt="\vec{rd}" /> is the addition of <img src="http://www.edesign.nl/wp-content/cache/tex_7c54a32d6ac073f230f089ae1703bfc5.png" align="absmiddle" class="tex" alt="\vec{tiltCorrection}" /><em> </em>to <img src="http://www.edesign.nl/wp-content/cache/tex_954322810f486ac24f0d70218627d2a0.png" align="absmiddle" class="tex" alt="\vec{d}" />:</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_ac1ecf22584f2bbba1b86a580ce3b2c7.png" title="\vec{rd} = \vec{tiltCorrection} + \vec{d}" class="etex" alt="\vec{rd} = \vec{tiltCorrection} + \vec{d}" /></center></p>
<h2>Calcultating illumination and mapping</h2>
<p>Using the <a href="http://mathworld.wolfram.com/DotProduct.html" target="_blank">dot product</a> (or <a href="http://www.netcomuk.co.uk/~jenolive/vect6.html" target="_blank">scalar product</a> or <a href="http://en.wikipedia.org/wiki/Dot_product" target="_blank">inner product</a>) you can calculate the angle between two vectors. Or, if the vectors are both normalized (length = 1) it is the projection of the one vector onto the other. So if we calculate the dot product for every normal vector from the surface of the earth (<img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" />) with <img src="http://www.edesign.nl/wp-content/cache/tex_0b41e62b40d2668f0de148e45d42f78e.png" align="absmiddle" class="tex" alt="\vec{rd}" /> we should get some meaningful results.</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_934bbf22f5024754ad96d504e678bdab.png" title="illumination = \vec{rd} \cdot \vec{p}" class="etex" alt="illumination = \vec{rd} \cdot \vec{p}" /></center></p>
<p>If the <img src="http://www.edesign.nl/wp-content/cache/tex_f47cdb7d31a3b19cebc72d46123a33b7.png" align="absmiddle" class="tex" alt="illumination &lt; 0" /> the sun is in another direction so <img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" /> is pointing to the night side of the earth. If <img src="http://www.edesign.nl/wp-content/cache/tex_f68036c925fc7246cbd239e8cd6c41e0.png" align="absmiddle" class="tex" alt="illumination \geq 0" /> that piece of earth recieves solar light.</p>
<p>We have a 2D map (Mercator projection) we need to map on the 3D sphere. We need to do this to determine which we need to calculate dot products for. To avoid confusion, lets call <em>x</em> and <img src="http://www.edesign.nl/wp-content/cache/tex_415290769594460e2e485922904f345d.png" align="absmiddle" class="tex" alt="y" /> from the 2D map <img src="http://www.edesign.nl/wp-content/cache/tex_7b774effe4a349c6dd82ad4f4f21d34c.png" align="absmiddle" class="tex" alt="u" /> and <img src="http://www.edesign.nl/wp-content/cache/tex_9e3669d19b675bd57058fd4664205d2a.png" align="absmiddle" class="tex" alt="v" />. We can iterate for each <img src="http://www.edesign.nl/wp-content/cache/tex_7b774effe4a349c6dd82ad4f4f21d34c.png" align="absmiddle" class="tex" alt="u" /> and each <img src="http://www.edesign.nl/wp-content/cache/tex_9e3669d19b675bd57058fd4664205d2a.png" align="absmiddle" class="tex" alt="v" /> for each <img src="http://www.edesign.nl/wp-content/cache/tex_7b774effe4a349c6dd82ad4f4f21d34c.png" align="absmiddle" class="tex" alt="u" /> and map it to a <img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" /> in 3D. To do this we need to determine phi (<img src="http://www.edesign.nl/wp-content/cache/tex_1ed346930917426bc46d41e22cc525ec.png" align="absmiddle" class="tex" alt="\phi" />, logitude: <img src="http://www.edesign.nl/wp-content/cache/tex_9e3669d19b675bd57058fd4664205d2a.png" align="absmiddle" class="tex" alt="v" />)and theta (<img src="http://www.edesign.nl/wp-content/cache/tex_2554a2bb846cffd697389e5dc8912759.png" align="absmiddle" class="tex" alt="\theta" />, latitude: <img src="http://www.edesign.nl/wp-content/cache/tex_7b774effe4a349c6dd82ad4f4f21d34c.png" align="absmiddle" class="tex" alt="u" />). In the iteration (<img src="http://www.edesign.nl/wp-content/cache/tex_e862d31aae219d30f927d5835bb1aefa.png" align="absmiddle" class="tex" alt="maxV" /> is the height of the map and <img src="http://www.edesign.nl/wp-content/cache/tex_ce778d1af59137ea75e31062e9b5071a.png" align="absmiddle" class="tex" alt="maxU" /> is the width of the map):</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_ea04300b34f21520a92879871d570de7.png" title="\phi = 2\pi (\frac{v}{maxV})" class="etex" alt="\phi = 2\pi (\frac{v}{maxV})" /></center><br />
<center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_8a9c80d2a42b86a8030ef85d5fc9134e.png" title="\theta = 2\pi (\frac{u}{maxU})" class="etex" alt="\theta = 2\pi (\frac{u}{maxU})" /></center></p>
<p>Now <img src="http://www.edesign.nl/wp-content/cache/tex_7b774effe4a349c6dd82ad4f4f21d34c.png" align="absmiddle" class="tex" alt="u" /> and <img src="http://www.edesign.nl/wp-content/cache/tex_9e3669d19b675bd57058fd4664205d2a.png" align="absmiddle" class="tex" alt="v" /> map to <img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" /> <a href="http://en.wikipedia.org/wiki/Sphere#Equations_in_R3" target="_blank">as follows</a>:</p>
<p><center><img src="http://www.edesign.nl/wp-content/plugins/easy-latex/cache/tex_da487b437d943ae2192deb8d49f55840.png" title="\left\{\begin{matrix} \vec{p}_x = \sin{\phi} \cos{\theta} \\ \vec{p}_y = \cos{\theta} \\ \vec{p}_z = \sin{\phi} sin{\theta} \end{matrix}\right." class="etex" alt="\left\{\begin{matrix} \vec{p}_x = \sin{\phi} \cos{\theta} \\ \vec{p}_y = \cos{\theta} \\ \vec{p}_z = \sin{\phi} sin{\theta} \end{matrix}\right." /></center></p>
<p>Now we can for each coordinate <img src="http://www.edesign.nl/wp-content/cache/tex_f406f917636fcaa71ae578800a9700f5.png" align="absmiddle" class="tex" alt="(u, v)" /> on the map if it should be displayed as day or night.</p>
<h2>Fun with shading</h2>
<p>Because you know the angle the sunlight makes with the earth&#8217;s surface you can make add some shading by making it increasingly dark on the edges of the day. It the dot product of <img src="http://www.edesign.nl/wp-content/cache/tex_5808829302c573af69fc6aa7f83b41e6.png" align="absmiddle" class="tex" alt="\vec{p}" /> and <img src="http://www.edesign.nl/wp-content/cache/tex_0b41e62b40d2668f0de148e45d42f78e.png" align="absmiddle" class="tex" alt="\vec{rd}" /> is smaller than 0.1 (<img src="http://www.edesign.nl/wp-content/cache/tex_a45c2c2471bdd7da560847792785984c.png" align="absmiddle" class="tex" alt="illumination &lt; 0.1" />) the point is in dusk or dawn and you could mix day and night as a gradient to make the difference between them look more fluently. Another joke is the reflection of the sun: if the product is greater than, let&#8217;s say, 0.95 (<img src="http://www.edesign.nl/wp-content/cache/tex_0d4df00bd8936b0588687977c7e6cde1.png" align="absmiddle" class="tex" alt="illumination &gt; 0.95" />) the sun is approximately orthogonally above that coordinate an you could make it more white to have it look like the sun is reflecting in the map.</p>
<h2>Demo and discussion</h2>
<p>A <a href="http://www.edesign.nl/examples/sunlightmap/" target="_blank">demontration of this method</a> is available at the edesign example site. There are a number of assumptions done and restrictions set while creating this demo.</p>
<div class="wp-caption aligncenter" style="width: 510px"><a href="http://www.edesign.nl/examples/sunlightmap/"><img title="Live World Sunlight map" src="/examples/sunlightmap/map.php" alt="" width="500" height="250" /></a><p class="wp-caption-text">Live sunlight map generated using the algorithm discussed in this article. Click the image to go to the example page where you can override current time.</p></div>
<ul>
<li>I assumed the solar rays are parallel. The distance from the sun to the earth is huge, but it is actually wrong to assume rays are parallel. They are only by approximation so it is good enough for this simulation.</li>
<li>Because of this assumption, the night &#8217;starts&#8217; exactly at the half of the world in the shade while actually this is different. The sun is far bigger than the earth and therefor some rays should be able to reach the shadow half of the earth (near the limit depicted in the first image). By approximation I eliminated this too.</li>
<li>Timing is not very accurate. As well as for the 24h clock as for the solstices rough estimates are used.</li>
<li>A perfect sphere is taken as world. No fattening is modelled (polar radius should be smaller than equatorial radius).</li>
<li>The &#8216;fun with shading&#8217; should implement something to make land mass not reflect sunlight.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2009/05/14/math-behind-a-world-sunlight-map/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Textual difference detector</title>
		<link>http://www.edesign.nl/2009/05/07/textual-difference-detector/</link>
		<comments>http://www.edesign.nl/2009/05/07/textual-difference-detector/#comments</comments>
		<pubDate>Thu, 07 May 2009 15:55:13 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Text processing]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=397</guid>
		<description><![CDATA[Today I uploaded my textual difference detector to the eDesign examples. This is an example application demonstrating the theory of applying the Levenshtein algorithm to detect differences between two versions of the same text. Also, the &#8216;Find the differences&#8216; post is updated with a link to this example.
This example takes two texts as input and [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-398" href="http://www.edesign.nl/2009/05/07/textual-difference-detector/comparedifflarge/"><img class="alignleft size-thumbnail wp-image-398" title="comparedifflarge" src="http://www.edesign.nl/wp-content/uploads/2009/05/comparedifflarge-150x150.jpg" alt="comparedifflarge" width="150" height="150" /></a>Today I uploaded my <a href="http://www.edesign.nl/examples/levenshtein/" target="_blank">textual difference detector</a> to the eDesign examples. This is an example application demonstrating the theory of applying the <a href="http://www.edesign.nl/2009/04/12/find-the-differences/" target="_self">Levenshtein algorithm</a> to detect differences between two versions of the same text. Also, the &#8216;<a href="http://www.edesign.nl/2009/04/12/find-the-differences/" target="_self">Find the differences</a>&#8216; post is updated with a link to this example.</p>
<p>This example takes two texts as input and outputs one merged text marked with what was deleted and what was added. <a href="http://www.edesign.nl/examples/levenshtein/" target="_blank">Take a look</a> and feel free to download the <a href="http://www.edesign.nl/examples/levenshtein/levenshtein.zip">source code</a>. This also inlcludes the <a href="http://www.edesign.nl/examples/levenshtein/levenshtein.zip">Levenshtein algorithm source code</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2009/05/07/textual-difference-detector/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Challenge Hash</title>
		<link>http://www.edesign.nl/2009/05/05/challenge-hash/</link>
		<comments>http://www.edesign.nl/2009/05/05/challenge-hash/#comments</comments>
		<pubDate>Tue, 05 May 2009 07:47:51 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=202</guid>
		<description><![CDATA[The Internet is a crowd and everybody in it can potentially hear what you say. Methods have been developed to prevent this and ensure identity, integrity and authenticity. Often these three can be seen as properties of encryption. Encryption implies the possibility of decryption. Passwords are precious things you don&#8217;t want others to decrypt and [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-59" href="http://www.edesign.nl/2009/05/05/challenge-hash/hide_a_key/"><img class="alignleft size-medium wp-image-59" title="Hide a key" src="http://www.edesign.nl/wp-content/uploads/2009/03/hide_a_key-300x199.jpg" alt="Hide a key" width="167" height="110" /></a>The Internet is a crowd and everybody in it can potentially hear what you say. Methods have been developed to prevent this and ensure <a href="http://www.edesign.nl/2009/04/24/security-basics/" target="_self">identity, integrity and authenticity</a>. Often these three can be seen as properties of encryption. Encryption implies the possibility of decryption. Passwords are precious things you don&#8217;t want others to decrypt and read. With a technique called challenge hashing you don&#8217;t need to have any worries about it. Challenge hashing is a technique used to verify a password on site B which was sent from site A without sending the password in plain text. This article covers how.<span id="more-202"></span></p>
<h2>Hash</h2>
<p>A <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function" target="_blank">cryptographic hash function</a> is a deterministic procedure that takes an arbitrary block of data and returns a fixed-size bit string, the (cryptographic) hash value, such that an accidental or intentional change to the data will change the hash value. The data to be encoded is often called the &#8220;message&#8221;, and the hash value is sometimes called the message digest or simply digest. (<a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function" target="_blank">Wikipedia</a>, retreived may 2009). In other words a hash (digest) is the result of a hashing function from a certain input (password, file, etc.).</p>
<h2>Challenge</h2>
<p>The <a href="http://en.wikipedia.org/wiki/Challenge-response_authentication" target="_blank">challenge</a> is a question presented to a party who needs to provide the correct answer. A common form of this algorithm is where the challenge is asking for the password and the valid response is the correct password. Also <a href="http://recaptcha.net/" target="_blank">CAPCHAs</a> are a well known implementation.</p>
<h2>One step further</h2>
<p>When you combine these two an intuitive way of keeping a password secret while being sent along a publicly accessible area and still being valid for authentication checks emerges.</p>
<p>A system has stored user information (username, password, email, etc.) in a database and has the password stored as an MD5 hash. MD5 is the name of the function as there are more hashing functions. When a user requests a login prompt, the server generates a random string (the challenge) and sends it along with the login prompt. Also it stores the string in the session of that request.</p>
<p>The user enters his username and password and hits &#8216;login&#8217;. Just before submitting, a client side script is triggered which calculates the MD5 hash of the password, concatenates the challenge to the digest and hashes that result. This is submitted as the &#8216;password&#8217; in code.</p>
<p>Now the server has to verify the password. As there is no way to reverse the MD5 digest, the coded password is matched agains the database in a special way. The database needs to concatenate the previously generated challenge to the stored digests and calculate the MD5 hash of that. When the result is the same as the submitted coded password a login is successful.</p>
<h2>Discussion</h2>
<p>A downside to this technique is the database server processing capacity is required as password digests need to be hashed every login attempt. Worst case (most processing time) is when such an attempt fails or the last hit is a success as every password in the database needs to be checked. Therefore this system is not really scalable to systems aiming for masses of users.</p>
<p>Client scripting must be available. This is not really a critical downside as e.g. JavaScript is common, but you can not assume everybody supports it.</p>
<h2>Demonstration</h2>
<p>A <a href="http://www.edesign.nl/examples/challengehash/" target="_blank">demonstration of challenge hashing</a> is available in JavaScript and PHP for you to investigate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2009/05/05/challenge-hash/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Character entities</title>
		<link>http://www.edesign.nl/2009/05/04/character-entities/</link>
		<comments>http://www.edesign.nl/2009/05/04/character-entities/#comments</comments>
		<pubDate>Mon, 04 May 2009 09:40:58 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Character Encoding]]></category>
		<category><![CDATA[Web standards]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=180</guid>
		<description><![CDATA[As in real life characters that build written language differ from system to system. Ελληνικά characters differ from Русский,  汉语 and Latin characters. Fortunately these character sets have been standardized and called alphabets. The same goes for character sets in the digital world. As computers can only process binary data, all characters are mapped to [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-317" href="http://www.edesign.nl/2009/05/04/character-entities/codage2/"><img class="alignleft size-medium wp-image-317" title="Character encoding" src="http://www.edesign.nl/wp-content/uploads/2009/05/codage2-300x214.jpg" alt="Character encoding" width="191" height="136" /></a>As in real life characters that build written language differ from system to system. Ελληνικά characters differ from Русский,  <span lang="zh-Hans">汉语</span> and Latin characters. Fortunately these character sets have been standardized and called alphabets. The same goes for character sets in the digital world. As computers can only process binary data, all characters are mapped to a number. In the early days such a mapping of the Latin alphabet, along with some other graphical &#8216;characters&#8217;, digits and control characters (e.g. escape, tab, line feed, carriage return) was standardized. This standard is known as  the American Standard Code for Information Interchange (<a href="http://en.wikipedia.org/wiki/Ascii" target="_blank">ASCII</a>) and was developed by the American Standards Association (currently: <a href="http://en.wikipedia.org/wiki/American_National_Standards_Institute" target="_blank">ANSI</a>). This 7-bit encoding lacked digital representations for many characters of e.g. foreign characters (as respectively Greek, Russian and Chinese are mentioned above) but also accents like å, è, ï, ó and û were not represented in the set. But as you can see in this paragraph, improvements have been made to facilitate such &#8217;special&#8217; characters.<span id="more-180"></span></p>
<h2>Character sets</h2>
<p>Other character sets have been defined. The International Organization for Standardization came up with the <a href="http://alis.isoc.org/codage/iso8859/jeuxiso.en.htm" target="_blank">ISO-8859</a> series to satisfy this shortcoming and defined some different character sets using 8 bits per character. Microsoft developed its own schemes too like <a href="http://en.wikipedia.org/wiki/Windows-1252" target="_blank">cp-1252</a> for instance, along with some others (like IBM). Also some local institutes needed to create encoding to facilitate the needs of their native language which the standardized sets still lacked. This introduced the problem of multiple interpretations of numbers. What characters are they mapped to? What character set do I need to use to decode 65 for instance? Does <em>65</em> mean <em>A or </em><em>a</em> or <em>ä</em> or <em>ç</em> or <em>R</em> or&#8230;</p>
<p>With the ISO 8-bit character sets, 256 characters were possible (190 characters without control characters, etc). This was sufficient as the sets covered the top ten most used languages. Still for instance Chinese and Japanese we not at all covered. This is where the Unicode Transformation Format (<a href="http://en.wikipedia.org/wiki/Unicode" target="_blank">UTF</a>) comes into play, developed by the Unicode Consortium. Unicode are multi-byte character sets which means that per character one, two, three or four 8-bit bytes are used to identify that character. This UTF-8 scheme is nowadays the most commonly used set. It is backward compatible with ASCII and with over 30.000 characters it is able to represent most of the living languages with a single code. UTF contains information of how to convert lowercase characters to uppercase and vice verse which is not the same or even symmetric for every character, and it has sorting rules. E.g. traditional Spanisch knows a single <em>ch</em> character which is sorted between the <em>c</em> and <em>d</em> and in Greek the uppercase <em>Σ</em> in lowercase is a <em>σ</em>, but if it is the last character of a word the lowercase of <em>Σ</em> is ς.</p>
<h2>Problems or challenges</h2>
<p>When programming web applications for instance a programmer often has to work with a database, file system, web server and one or several browsers. These systems and the data traffic between them (protocols) need to be tuned so they use the same character sets. If this is not the case errors can occur. For instance, if you send &#8220;Hellø world&#8221; from the database in UTF-8 to the browser, which interprets the bit stream it is receiving as CP-1252, the string is displayed as &#8220;Hell? world&#8221; and you have probably seen the <em>€</em> sign appear as <em>�</em> because of this error. The solution to this problem, of course is to keep all character sets the same when different systems (applications) communicate with each other.</p>
<div id="attachment_350" class="wp-caption alignright" style="width: 308px"><a rel="attachment wp-att-350" href="http://www.edesign.nl/2009/05/04/character-entities/copy-html-entity/"><img class="size-full wp-image-350" title="Copyright sign and it's NCR" src="http://www.edesign.nl/wp-content/uploads/2009/05/copy-html-entity.jpg" alt="Copy sign sgml entity" width="298" height="71" /></a><p class="wp-caption-text">Copyright sign, it&#39;s Numeric Character Reference and alias</p></div>
<p>A way to prevent this problem from occurring is to obey the HTML standard which requires characters not defined in certain character sets to be converted to their <a href="http://www.w3.org/International/tutorials/tutorial-char-enc/#Slide0430" target="_blank">Numeric Character Reference</a> (<a href="http://www.w3.org/TR/html4/sgml/entities.html" target="_blank">HTML entity</a>). That doing this is mandatory for element attribute values is fairly unknown. This is done by code points denoted by &amp;#[code]; where [code] is replaced by a number. For most used characters an alias is available. E.g. the equivalent for <em>&amp;</em> is <em>&amp;amp;</em>, <em>ë </em>has <em>&amp;euml;</em>, a space is denoted by <em>&amp;nbsp; </em>and the copyright sign has equivalent <em>&amp;#169;</em> with alias <em>&amp;copy;</em>. The codes (digits) and aliases itself only contain characters from the ASCII set, meaning an entire SGML (including HMTL, XML, etc) document can be composed of only ASCII characters. PHP has a <a href="http://www.php.net/htmlentities" target="_blank">built in function</a> that searches and converts such characters in an input. Also, a nice tool to <a href="http://leftlogic.com/lounge/articles/entity-lookup/" target="_blank">look up entities</a> is available at LeftLogic.com.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2009/05/04/character-entities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sudoku Logic &#8211; part I</title>
		<link>http://www.edesign.nl/2009/05/01/sudoku-logic-part-i/</link>
		<comments>http://www.edesign.nl/2009/05/01/sudoku-logic-part-i/#comments</comments>
		<pubDate>Fri, 01 May 2009 12:51:25 +0000</pubDate>
		<dc:creator>Jurgen</dc:creator>
				<category><![CDATA[Algorithms]]></category>

		<guid isPermaLink="false">http://www.edesign.nl/?p=262</guid>
		<description><![CDATA[If you haven&#8217;t heard of Sudoku puzzles (数独,, 								sūdoku) you&#8217;ve either been sleeping under a rock or been space traveling for quite a while. These 9×9 square puzzles originating from around 1900 became an international hit in 2005. Sudokus appear in newspapers, online and special sudoku puzzle books all-over-the-world. And as if that is not [...]]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-299" href="http://www.edesign.nl/2009/05/01/sudoku-logic-part-i/vinyl-leolan-puzzle-large/"><img class="alignleft size-thumbnail wp-image-299" title="vinyl-leolan-puzzle-large" src="http://www.edesign.nl/wp-content/uploads/2009/05/vinyl-leolan-puzzle-large-150x150.jpg" alt="vinyl-leolan-puzzle-large" width="150" height="150" /></a>If you haven&#8217;t heard of <a href="http://en.wikipedia.org/wiki/Sudoku" target="_blank">Sudoku puzzles</a> <span style="font-weight: normal;">(<span class="t_nihongo_kanji" lang="ja" xml:lang="ja">数独</span><span class="t_nihongo_comma" style="display: none;">,</span>, 								<em><span class="t_nihongo_romaji">sūdoku</span></em>) </span>you&#8217;ve either been sleeping under a rock or been space traveling for quite a while. These 9×9 square puzzles originating from around 1900 became an international hit in 2005. Sudokus appear in <a href="http://www.nytimes.com/ref/crosswords/sudoku/easy.html" target="_blank">newspapers</a>, <a href="http://www.websudoku.com/" target="_blank">online</a> and <a href="http://www.sudoku.nl/default.aspx?id=7fcb013e-72cf-41d2-bc44-6756bf842126" target="_blank">special sudoku puzzle books</a> <a href="http://www.sudokuweb.nl/" target="_blank">all</a>-<a href="http://www.e-sudoku.fr/" target="_blank">over</a>-<a href="http://www.sudokukryss.se/" target="_blank">the</a>-<a href="http://www.sudoku.name/" target="_blank">world</a>. And as if that is not yet enough <a href="http://en.wikipedia.org/wiki/Sudoku#Recent_popularity" target="_blank">Sudoku TV shows</a> and all kinds of <a href="http://en.wikipedia.org/wiki/Sudoku#Variants" target="_blank">variants</a> of the puzzle are made. One can solve a sudoku using logic only. Because of this <a href="http://eric4ever.googlepages.com/sudoku.html" target="_blank">computational algorithms</a> to solve every possible Sudoku must exist. This is part one in the series on such algorithms.<span id="more-262"></span></p>
<p>We must speak of multiple algorithms (strategies) because one algorithm would consist of multiple <a href="http://www.scanraid.com/Strategy_Families" target="_blank">solve strategies</a>. The most simple strategy not included in this article is a brute force or trail and error &#8216;attack&#8217; on given input. This is not logic and could take quite a while as there are <a href="http://forums.whirlpool.net.au/forum-replies-archive.cfm/351170.html#r5116245" target="_blank">70759827985602812313600000000 possibilities</a> to a default 9×9 sudoku, not considering <a href="http://nl.wikipedia.org/wiki/Sudoku#varianten" target="_blank">variations (in Dutch)</a> of e.g. 16×16 hex or jigsaw sudokus. The strategies discussed by <a href="http://www.scanraid.com/" target="_blank">Andrew Stuart</a> are based on the default  9×9 sudoku and some are limited to those puzzles and some block only strategies overlap with row column strategies. What I want to try with this article is programming a more generic solution which implements some strategies making the result cover all strategies. I think this is possible if you imagine a sudoku board not as a set of cells, divided in rows, columns and blocks, but as a set of equal sections which might have one or more cells in common. This way it will also be easy to implement variants simply by extending the model for a sudoku, resulting in e.g. the X-sudoku or hypersudoku and strategies need to be implemented only once.</p>
<h2>Sudoku model</h2>
<p>First of all, a model is needed which represents every possible sudoku. That model should include the character set (numbers 1 to 9), the cells (81) and the sections (27). Defaults for a 9×9 sudoku are filled out between brackets. If not mentioned otherwise, every example will concern such a default 9×9 sudoku.</p>
<p>In the model, each cell is identified by a number (0-80). This array keeps an array of possibilities for every cell. On initilization, every possibility is set to true, as everything still is possible but while solving the sudoku more and more will toggle to false, finally leaving only one to true.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$possibilities</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">,</span>                <span style="color: #666666; font-style: italic;">//false if unsolved, if solved integer of solution (redundant*)</span>
<span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #666666; font-style: italic;">//1 - 9 are possible (true)</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">81</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$cells</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$possibilities</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>* The first key in the &#8216;possibilities&#8217; array is redundant: it could be determined by the other keys (if only one is true, it is solved and has that value). But this way, the array keys match the value they represent and it might increase speed by avoiding determining if the cell is solved or not over and over again.</p>
<p>The sections are probably the most important piece of the model as they define how cells relate to each other. There are 27 sections, each containing <em>nP</em><em>ossibilities</em> (9) cells. There are the rows, columns and blocks. Example resp. top row, left column and top left block with the cell identifiers:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$section</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$section</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//top row</span>
<span style="color: #666666; font-style: italic;">//... 8 more ...</span>
<span style="color: #000088;">$section</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">27</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">45</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">54</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">63</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">72</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//left column</span>
<span style="color: #666666; font-style: italic;">//... 8 more ...</span>
<span style="color: #000088;">$section</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">11</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">18</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">19</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//top left block</span>
<span style="color: #666666; font-style: italic;">//... 8 more ...</span></pre></div></div>

<p>Also, some default actions (methods) can be added to this model (class). A way to set a cell to a value, one to set a possibility to false and one to count instances of possible locations for a digit in a section. They can call each other to implement recursion:</p>
<ul>
<li>setCellValue() calls removeFromCell() for all other cells for the same value</li>
<li>removeFromCell() calls countPossibleLocations() for that value on every section in which that cell occurs, and if the count equals one</li>
<li>countPossibleLocations() calls setCellValue() for that value</li>
</ul>
<h2>Solving loop</h2>
<p>When you defined the model and filled out some initial digits the algorithm should include a loop searching for the solution. I have included a &#8216;changed&#8217; property to the sudoku model. This boolean is initially set to true. The first line in the while(sudoku.changed) loop is setting this boolean to false. Next, when iterating over every section trying to find solutions on every change (setting or deleting a number from a cell), this boolean is set to true causing another iteration to occur (a kind of recursion).</p>
<p>Now we have a setup where we can add solve strategies to the loop. In this article, the first strategy will be explained.</p>
<h2>Simple elimination strategy</h2>
<p>This first strategy to implement in the section iteration is elimination. This is pretty straight forward. As a digit can only occur once in a section (row, column or block), other cells still having this digit set to true can toggle it. This iterative process continues while this resolves possibilities in a single cell to be unique for a section, all other possibles have been set to false.</p>
<p>In Sudoku Logic &#8211; part II more strategies will be discussed. Also some sudoku extensions, to simulate sudoku variants and a live demonstration of an implementation of this algorithm in PHP will be included.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.edesign.nl/2009/05/01/sudoku-logic-part-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

