<?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>-Flyぁ梦- &#187; 矩形</title>
	<atom:link href="http://blog.11034.org/tag/%e7%9f%a9%e5%bd%a2/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.11034.org</link>
	<description></description>
	<lastBuildDate>Sun, 22 Jun 2025 08:59:05 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.2.38</generator>
	<item>
		<title>判断矩形是否重叠</title>
		<link>http://blog.11034.org/2013-05/rectangle_overlap.html</link>
		<comments>http://blog.11034.org/2013-05/rectangle_overlap.html#comments</comments>
		<pubDate>Tue, 07 May 2013 07:53:13 +0000</pubDate>
		<dc:creator><![CDATA[-Flyぁ梦-]]></dc:creator>
				<category><![CDATA[数据结构和算法]]></category>
		<category><![CDATA[矩形]]></category>

		<guid isPermaLink="false">http://blog.stariy.org/?p=1767</guid>
		<description><![CDATA[正常思维下，就会去判断一个矩形的每个点是否在另一个矩形中，然后衍生出4个判断，每个判断都要带上4对大于小于比较 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>正常思维下，就会去判断一个矩形的每个点是否在另一个矩形中，然后衍生出4个判断，每个判断都要带上4对大于小于比较，甚是麻烦。</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> judgeOverlap<span style="color: #009900;">&#40;</span>Rect A, Rect B<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Point</span> p <span style="color: #339933;">:</span> all points in A<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>p in B<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>上面这段代码其实有一点问题</strong>，设A的左上右下点为A1和A2，B的为B1和B2，就是：A与B重叠不等价于judgeOverlap(A1, A2, B1, B2) == true （比如当B真包含于A时就返回false了），而是等价于judgeOverlap(A1, A2, B1, B2) == true || judgeOverlap(B1, B2, A1, A2) == true，这里很容易犯bug。</p>
<p>有一个更容易的方法来解决这个问题，反向思维也很容易，就是排除那些A和B不可能重叠的情况，就4种。</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="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">boolean</span> judgeOverlap<span style="color: #009900;">&#40;</span>Rect A, Rect B<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">right</span> <span style="color: #339933;">&lt;</span> B.<span style="color: #006633;">left</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">left</span> <span style="color: #339933;">&gt;</span> B.<span style="color: #006633;">right</span> <span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">top</span> <span style="color: #339933;">&lt;</span> B.<span style="color: #006633;">bottom</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>A.<span style="color: #006633;">bottom</span> <span style="color: #339933;">&gt;</span> B.<span style="color: #006633;">top</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>现在项目中还在使用上面版本的代码跑着，觉得第二种方法应该没有问题但没有测试过也没有应用到项目中去实践。</p>
<h4  class="related_post_title">随便看一看</h4><ul class="related_post"><li>2012-10-16 -- <a target="_blank" href="http://blog.11034.org/2012-10/zhoushan_pics.html" title="舟山游图集">舟山游图集</a></li><li>2017-05-03 -- <a target="_blank" href="http://blog.11034.org/2017-05/guilin.html" title="五一桂林阳朔游">五一桂林阳朔游</a></li><li>2010-04-30 -- <a target="_blank" href="http://blog.11034.org/2010-04/highschool.html" title="回高中">回高中</a></li><li>2010-12-14 -- <a target="_blank" href="http://blog.11034.org/2010-12/play_music_in_wordpress.html" title="wordpress中播放音乐">wordpress中播放音乐</a></li><li>2010-09-02 -- <a target="_blank" href="http://blog.11034.org/2010-09/summary_in_ebay_15th.html" title="暑期实习小结于eBay15周年">暑期实习小结于eBay15周年</a></li></ul><h4 class="related_post_title">看看 数据结构和算法 </h4><ul class="related_post"><li>2013-05-27 -- <a target="_blank" href="http://blog.11034.org/2013-05/java_map.html" title="java.util中几个Map的性能测试">java.util中几个Map的性能测试</a></li><li>2013-01-15 -- <a target="_blank" href="http://blog.11034.org/2013-01/pack_in_zoj.html" title="背包练习小集合">背包练习小集合</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/string.html" title="整理下字符串的一些数据结构和算法">整理下字符串的一些数据结构和算法</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/suffix_array.html" title="一套可用的后缀数组代码">一套可用的后缀数组代码</a></li><li>2012-12-06 -- <a target="_blank" href="http://blog.11034.org/2012-12/ac_automachine.html" title="一个OOP的AC自动机代码">一个OOP的AC自动机代码</a></li>]]></content:encoded>
			<wfw:commentRss>http://blog.11034.org/2013-05/rectangle_overlap.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
