<?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>Silicon Chisel &#187; algorithms</title>
	<atom:link href="http://www.siliconchisel.com/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.siliconchisel.com</link>
	<description>Open-Source Web Development</description>
	<lastBuildDate>Wed, 07 Jul 2010 00:57:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fast Window Overlap Checking Algorithm</title>
		<link>http://www.siliconchisel.com/articles/fast-window-overlap-checking-algorithm/</link>
		<comments>http://www.siliconchisel.com/articles/fast-window-overlap-checking-algorithm/#comments</comments>
		<pubDate>Mon, 11 Jul 2005 12:56:48 +0000</pubDate>
		<dc:creator>Mark Miller</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[window occlusion]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[<p><img class="alignnone size-full wp-image-182 alignleft" style="float: left;" title="rects" src="http://www.siliconchisel.com/wp-content/uploads/2008/04/rects.gif" alt="" width="128" height="96" />Many algorithms to check if two windows overlap (a very common activity in window system and interface design) rely on doing multiple comparisons of the positions of the sides of the two rectangular areas in question. This is usually fine&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-182 alignleft" style="float: left;" title="rects" src="http://www.siliconchisel.com/wp-content/uploads/2008/04/rects.gif" alt="" width="128" height="96" />Many algorithms to check if two windows overlap (a very common activity in window system and interface design) rely on doing multiple comparisons of the positions of the sides of the two rectangular areas in question. This is usually fine for most instances, especially in these days of multi-gigahertz desktop PC&#8217;s. But sometimes you need to do so many such comparisons that speed becomes the paramount concern. And sometimes you do not have a super fast processor to rely on &#8211; such as on small hand-held devices or cell-phones.</p>
<p>The algorithm described here is one I wrote back in 1988 for a graphics workstation product. It relies on some interesting relationships between the sign of the result of subtracting opposite sides of two rectangles. By taking advantage of this, it is possible to write few lines of C code which will compile down to 20 or fewer assembly instructions.</p>
<p><span id="more-52"></span>Simply put, if you subtract opposite sides of two rectangles the signs of the results will be the same if they do not overlap along that axis. So in pseudocode that comes out to:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>sign<span style="color: #009900;">&#40;</span>top1 <span style="color: #339933;">-</span> bottom2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> sign<span style="color: #009900;">&#40;</span>bottom1 <span style="color: #339933;">-</span> top2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span>sign<span style="color: #009900;">&#40;</span>left1 <span style="color: #339933;">-</span> right2<span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> sign<span style="color: #009900;">&#40;</span>right1 <span style="color: #339933;">-</span> left2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This can be further optimized by using the XOR function, which allows the entire algorithm to be reduced to a 1-line preprocessor macro which has only one logical comparison &#8211; everything else is integer or bit-wise math. In this case all we are trying to do is get the high-order bit (which determines sign of the integer) to be set based on the subtractions, and then test it for the occlusion.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> t<span style="color: #339933;">,</span> l<span style="color: #339933;">,</span> r<span style="color: #339933;">,</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> rect<span style="color: #339933;">;</span>   
&nbsp;
<span style="color: #339933;">#define test_olap (_r1, _r2) </span>
    <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>_r1.<span style="color: #202020;">t</span> <span style="color: #339933;">-</span> _r2.<span style="color: #202020;">b</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span>_r1.<span style="color: #202020;">b</span> <span style="color: #339933;">-</span> _r2.<span style="color: #202020;">t</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span>_r1.<span style="color: #202020;">l</span> <span style="color: #339933;">-</span> _r2.<span style="color: #202020;">r</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span>_r1.<span style="color: #202020;">r</span> <span style="color: #339933;">-</span> _r2.<span style="color: #202020;">l</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">:</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Further optimizations could be done for a C/C++ coded version for early rejection based a negative return from the top/bottom or left/right comparison. But the gain would be negligable since that would require kicking out to logical operations and comparisons. The macro above compiled down to around 20 instructions on SPARC/680X0 series chips, and loading the registers for an early compare would not shave much off that.</p>
<p>If this has been of use to you, please let me know where and how you used it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.siliconchisel.com/articles/fast-window-overlap-checking-algorithm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
