<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Algorithms on Blips and Blinks</title>
    <link>https://notes.vyrtsev.com/</link>
    <description>Notes by Misha Vyrtsev, tagged Algorithms</description>
    <language>en-us</language>
    <lastBuildDate>Tue, 01 Dec 2026 04:30:00 +0400</lastBuildDate>
    <atom:link href="https://notes.vyrtsev.com/rss.xml?tags=algorithms" rel="self" type="application/rss+xml" />
    <item>
      <title>Reducing multiple overlapping rectangles into set of non-overlapping ones</title>
      <link>https://notes.vyrtsev.com/posts/reducing_rectangles/</link>
      <guid>https://notes.vyrtsev.com/posts/reducing_rectangles/</guid>
      <pubDate>Mon, 10 Apr 2023 21:50:07 +0300</pubDate>
      <category>Development</category>
      <category>Algorithms</category>
      <description>Consider we have a set of multiple overlapping rectangles and we want to reduce it so in the result we have a set that occupies the same area but rectangles inside do not overlap with each other&amp;hellip;</description>
      <content:encoded>&lt;p&gt;Consider we have a set of multiple overlapping rectangles and we want to reduce it so in the result we have a set that occupies the same area but rectangles inside do not overlap with each other.&lt;/p&gt;&#xA;&lt;p&gt;First we have the input:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/1_input.png?v=22c9edfd&#34; alt=&#34;Input&#34; width=&#34;361&#34; height=&#34;405&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;Results may vary. We can go many ways:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/1_2_result.png?v=e981b078&#34; alt=&#34;Result 1&#34; width=&#34;346&#34; height=&#34;391&#34;&gt;&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/1_3_result.png?v=e580a169&#34; alt=&#34;Result 2&#34; width=&#34;346&#34; height=&#34;389&#34;&gt;&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/1_4_result.png?v=0356a8f4&#34; alt=&#34;Result 3&#34; width=&#34;346&#34; height=&#34;391&#34;&gt;&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;So how do we go?&lt;/p&gt;&#xA;&lt;h1 id=&#34;algorithm&#34;&gt;Algorithm&lt;/h1&gt;&lt;p&gt;First we do is creating grid from the vertices of each rectangle:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/2_dissect.png?v=12f44cff&#34; alt=&#34;Dissecting&#34; width=&#34;500&#34; height=&#34;601&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;By doing so we now have coordinate space. Let&amp;rsquo;s say first will be y coordinate, and then x coordinate. &lt;code&gt;(0.0)&lt;/code&gt;, &lt;code&gt;(1,0)&lt;/code&gt;, &lt;code&gt;(1.1)&lt;/code&gt; etc&amp;hellip;&lt;/p&gt;&#xA;&lt;p&gt;Then let&amp;rsquo;s make some &lt;code&gt;Set&lt;/code&gt; where we will write down visited cells, because our algorithm will include subloops.&lt;/p&gt;&#xA;&lt;p&gt;Also we will make an &lt;code&gt;Array&lt;/code&gt; where we will put our result. Each item in the array will consist of two coordinates: top left, and bottom right corners.&lt;/p&gt;&#xA;&lt;h2 id=&#34;find-a-rectangle&#34;&gt;Find a rectangle&lt;/h2&gt;&lt;p&gt;So let&amp;rsquo;s start by iterating on Y (top to bottom) and then x (left to right) coordinates. Our goal is to find first rectangle start position and height. In out case we got start position at &lt;code&gt;(0,0)&lt;/code&gt; and end at &lt;code&gt;(3,0)&lt;/code&gt;. So our rectangle has height equal to &lt;code&gt;3&lt;/code&gt;. Okay, so next step is we make sub loop by iterating from &lt;code&gt;y = 0&lt;/code&gt; up to &lt;code&gt;y = 3&lt;/code&gt; (&lt;code&gt;3&lt;/code&gt; is not included). On x direction we iterate from start position &lt;code&gt;0&lt;/code&gt; up to the last grid x coordinate &lt;code&gt;5&lt;/code&gt;. Our goal here is to find the width of the rectangle. We iterate by column and remember x coordinate where column height equals our rectangle&amp;rsquo;s height. If we see that there cell is not occupied we break the loop and take last remembered value.&lt;/p&gt;&#xA;&lt;p&gt;So we go &lt;code&gt;(0,0)&lt;/code&gt;, &lt;code&gt;(1,0)&lt;/code&gt;, &lt;code&gt;(2,0)&lt;/code&gt;, &lt;code&gt;(1,0)&lt;/code&gt;, &lt;code&gt;(1,1)&lt;/code&gt;, &lt;code&gt;(1,2)&lt;/code&gt;, &lt;code&gt;(2,0)&lt;/code&gt;, &lt;code&gt;(2,1)&lt;/code&gt;, &lt;code&gt;(2,2)&lt;/code&gt;. &lt;code&gt;(3,0)&lt;/code&gt; is empty, so we break the loop. Resulting width we got is &lt;code&gt;3&lt;/code&gt;. We got our first rectangle. We should put it into our results array. We put &lt;code&gt;[(0,0), (2.2)]&lt;/code&gt;. Also we should put every cell of the rectangle into the set of visited cells.&lt;/p&gt;&#xA;&lt;p&gt;And now we continue our main loop. If the cell was already visited then we just skip it. We go:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(1,0)&lt;/code&gt; visited — skip&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(2,0)&lt;/code&gt; visited — skip&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(3,0)&lt;/code&gt; empty — skip and put into visited cells set&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(4,0)&lt;/code&gt; empty — skip and put into visited cells set&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(0,1)&lt;/code&gt; visited — skip&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(0,2)&lt;/code&gt; visited — skip&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;&lt;code&gt;(0,3)&lt;/code&gt; new rectangle start. We repeat a process in &lt;a href=&#34;#find-a-rectangle&#34;&gt;Find a rectangle&lt;/a&gt; step.&lt;/p&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;result&#34;&gt;Result&lt;/h2&gt;&lt;p&gt;So that is basically it. We get following picture when we end main loop iteration:&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/3_iterate.png?v=f01ff34d&#34; alt=&#34;Dissecting&#34; width=&#34;500&#34; height=&#34;601&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;And that&amp;rsquo;s it! By repeating steps above we can find a new set of non-overlapping rectangles which occupy the same area as an input.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;https://notes.vyrtsev.com/posts/reducing_rectangles/4_output.png?v=d6fbddb3&#34; alt=&#34;Output&#34; width=&#34;500&#34; height=&#34;601&#34;&gt;&lt;/p&gt;&#xA;</content:encoded>
    </item>
  </channel>
</rss>
