<?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>.NET Tips and Tricks &#187; Puzzle</title>
	<atom:link href="http://kossovsky.net/index.php/tag/puzzle/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>C# LINQ Teaser</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 09:31:00 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[interview question]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[puzzles]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[tezer]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[toarray]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=517</guid>
		<description><![CDATA[Another great interview question. Will the following code compile and if yes what will be the result of it ? static void Main(string[] args) { int[] array = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Func&#60;int, int&#62; func = i =&#62; { Console.Write(array[i]); return i; }; var result = array.Where(e =&#62; [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.8" /></div><div>Rating: 4.8/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Another great interview question.</p>
<p>Will the following code compile and if yes what will be the result of it ?<br />
<span id="more-517"></span></p>
<pre class="c#" name="code">static void Main(string[] args)
{
    int[] array = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    Func&lt;int, int&gt; func = i =&gt; {
                          Console.Write(array[i]);
                          return i;
                     };

    var result = array.Where(e =&gt; e &lt;= func(2)).ToArray ();
}</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.8" /></div><div>Rating: 4.8/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-linq-teaser%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# Rectangle Packing</title>
		<link>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:45:48 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[ascii character]]></category>
		<category><![CDATA[bitmap fonts]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[common interface]]></category>
		<category><![CDATA[cygon]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[humble webmaster]]></category>
		<category><![CDATA[lightmaps]]></category>
		<category><![CDATA[linear increase]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[space efficiency]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[suitable position]]></category>
		<category><![CDATA[support assembly]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=414</guid>
		<description><![CDATA[Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without wasting much space.</p>
<p>What you need then, is a rectangle packer, an algorithm that arranges as many smaller rectangles on a larger rectangle as can possibly fit.</p>
<p><span id="more-414"></span></p>
<p>The <a title="Nuclex - Rectangle Packing" href="http://www.nuclex.org/pages/framework/rectangle-packing" target="_blank">Nuclex.Support</a> assembly contains several rectangle packing algorithms in the Nuclex.Support.Packing namespace offering various levels of compromises between space efficiency and runtime performance. All of these algorithms have been implemented as classes with a common interface, so, once implemented, you can easily switch forth and back between different algorithms to find the one best suites for your purpose.</p>
<p>Currently, there are three different packing algorithms for you to choose:</p>
<table style="width: 100%; border: none;" border="0" cellspacing="2" cellpadding="0">
<tbody>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="The Simple Packer" src="http://kossovsky.net/wp-content/uploads/2009/07/simple-rectangle-packer.png" alt="The Simple Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Simple Packer</strong><br />
This packer is optimized for runtime performance. It does sacrifice a bit of space, but the time needed to find a position for a new rectangle is O(1). In english, that means it doesn&#8217;t take longer to search for a suitable position, no matter how many rectangles have been added to the packing area.</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="cygon-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/cygon-rectangle-packer.png" alt="The Cygon Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Cygon Packer</strong><br />
Named after your humble webmaster, this algorithm is highly efficient in its space usage and offers reasonable performance. It will never exceed O(n) time but generally achieves almost O(1) on average. English translation: Search time is usually constant and guaranteed to never exceed a linear increase (so when you&#8217;ve got 99 rectangles already packed and add the 100th one, it guarantees that search time will at most be 1% longer than the time taken for the previous rectangle).</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="arevalo-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/arevalo-rectangle-packer.png" alt="The Arevalo Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Arevalo Packer</strong><br />
Named after Javier Arevalo, who posted his implementation of this packer on flipcode back in the golden times. This algorithm offers very good space efficiency and runs in slightly less then O(n) time. Just to be consistent, in english, that means, the time needed to find a suitable place for a new rectangle will only increase linearly.</td>
</tr>
</tbody>
</table>
<p>You can download the C# version of the algorithms from <a title="Nuclex Framework - C# Source Code" href="https://devel.nuclex.org/framework/browser/game/Nuclex.Game/trunk/Source/Packing" target="_blank">here</a>.</p>
<p>The usage is very simple :</p>
<pre class="c-sharp" name="code">
	Point PP;
	var ARP = new ArevaloRectanglePacker(200, 200);

	if(!ARP.TryPack(Width, Height, out PP)) {
		// Do something
	}
	else {
		// Do something else
	}
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcshar-rectangle-packing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>C# The &#8220;double&#8221; trouble</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 21:37:16 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QA]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[d1 d2]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[double]]></category>
		<category><![CDATA[double trouble]]></category>
		<category><![CDATA[floating]]></category>
		<category><![CDATA[floating point]]></category>
		<category><![CDATA[guarantees]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[numeric value]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[simple answer]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=360</guid>
		<description><![CDATA[This is a very simple question with a not so simple answer&#8230; Forget for a second about .NET. If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ? Now, let&#8217;s go back to .NET and check if our calculations are correct. static [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This is a very simple question with a not so simple answer&#8230;</p>
<p>Forget for a second about .NET.<br />
If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ?</p>
<p>Now, let&#8217;s go back to .NET and check if our calculations are correct.</p>
<p><span id="more-360"></span></p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;
            Console.WriteLine((d1 - d2) == 1);
        }</pre>
<p>If you think the result will be &#8220;true&#8221;&#8230; well, it&#8217;s not.</p>
<p>Let&#8217;s see another example :</p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;

            // Will result 1
            Console.WriteLine(d1 - d2);

            // Will result 0.99999999999999989
            Console.WriteLine((d1 - d2).ToString ("R"));
        }</pre>
<p>For those who might not know, the meaning of ToString(&#8220;R&#8221;) is <a title="MSDN - Standard Numeric Format Strings" href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx" target="_blank">Round-trip</a>.</p>
<blockquote><p>This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.</p></blockquote>
<p>So, what happened here ? Is it a bug ?<br />
The answer is &#8211; NO, IT&#8217;S NOT A BUG ( and it&#8217;s not happening only on my computer ).</p>
<p>The thing is, that the above is about how .NET rounding a floating point number.<br />
I won&#8217;t explain here the math ( there is already a great <a title="What Every Computer Scientist Should Know About Floating-Point Arithmetic" href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" target="_blank">article</a> explaining it ), and i won&#8217;t say don&#8217;t use floating point types.</p>
<p>You just need to know that when you are using it &#8211; be carefull, especialy in equations and formatting, otherwise your calculations will be wrong and your result not accurate.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-the-double-trouble%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CSS Sprite Generator</title>
		<link>http://kossovsky.net/index.php/2009/06/css-sprite-generator/</link>
		<comments>http://kossovsky.net/index.php/2009/06/css-sprite-generator/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 21:29:32 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[composite image]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Sprite]]></category>
		<category><![CDATA[css sprites]]></category>
		<category><![CDATA[multiple images]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[positioning]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[sprite]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=3</guid>
		<description><![CDATA[CSS sprites group multiple images into one composite image and display them using CSS background positioning. Coming soon… Rating: 0.0/5 (0 votes cast)<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>CSS sprites group multiple images into one composite image and display them using CSS background positioning.</p>
<p>Coming soon…</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=0.0" /></div><div>Rating: 0.0/<strong>5</strong> (0 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F06%2Fcss-sprite-generator%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/06/css-sprite-generator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

