{"authors":[{"name":"Misha Vyrtsev","url":"https://vyrtsev.com"}],"description":"Notes by Misha Vyrtsev, tagged Algorithms","feed_url":"https://notes.vyrtsev.com/rss.json?tags=algorithms","home_page_url":"https://notes.vyrtsev.com/","icon":"https://notes.vyrtsev.com/images/avatar.png?v=a72e473e","items":[{"content_html":"<p>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.</p>\n<p>First we have the input:</p>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/1_input.png?v=22c9edfd\" alt=\"Input\" width=\"361\" height=\"405\"></p>\n<p>Results may vary. We can go many ways:</p>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/1_2_result.png?v=e981b078\" alt=\"Result 1\" width=\"346\" height=\"391\"></p>\n<hr>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/1_3_result.png?v=e580a169\" alt=\"Result 2\" width=\"346\" height=\"389\"></p>\n<hr>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/1_4_result.png?v=0356a8f4\" alt=\"Result 3\" width=\"346\" height=\"391\"></p>\n<hr>\n<p>So how do we go?</p>\n<h1 id=\"algorithm\">Algorithm</h1><p>First we do is creating grid from the vertices of each rectangle:</p>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/2_dissect.png?v=12f44cff\" alt=\"Dissecting\" width=\"500\" height=\"601\"></p>\n<p>By doing so we now have coordinate space. Let&rsquo;s say first will be y coordinate, and then x coordinate. <code>(0.0)</code>, <code>(1,0)</code>, <code>(1.1)</code> etc&hellip;</p>\n<p>Then let&rsquo;s make some <code>Set</code> where we will write down visited cells, because our algorithm will include subloops.</p>\n<p>Also we will make an <code>Array</code> where we will put our result. Each item in the array will consist of two coordinates: top left, and bottom right corners.</p>\n<h2 id=\"find-a-rectangle\">Find a rectangle</h2><p>So let&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 <code>(0,0)</code> and end at <code>(3,0)</code>. So our rectangle has height equal to <code>3</code>. Okay, so next step is we make sub loop by iterating from <code>y = 0</code> up to <code>y = 3</code> (<code>3</code> is not included). On x direction we iterate from start position <code>0</code> up to the last grid x coordinate <code>5</code>. 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&rsquo;s height. If we see that there cell is not occupied we break the loop and take last remembered value.</p>\n<p>So we go <code>(0,0)</code>, <code>(1,0)</code>, <code>(2,0)</code>, <code>(1,0)</code>, <code>(1,1)</code>, <code>(1,2)</code>, <code>(2,0)</code>, <code>(2,1)</code>, <code>(2,2)</code>. <code>(3,0)</code> is empty, so we break the loop. Resulting width we got is <code>3</code>. We got our first rectangle. We should put it into our results array. We put <code>[(0,0), (2.2)]</code>. Also we should put every cell of the rectangle into the set of visited cells.</p>\n<p>And now we continue our main loop. If the cell was already visited then we just skip it. We go:</p>\n<ul>\n<li>\n<p><code>(1,0)</code> visited — skip</p>\n</li>\n<li>\n<p><code>(2,0)</code> visited — skip</p>\n</li>\n<li>\n<p><code>(3,0)</code> empty — skip and put into visited cells set</p>\n</li>\n<li>\n<p><code>(4,0)</code> empty — skip and put into visited cells set</p>\n</li>\n<li>\n<p><code>(0,1)</code> visited — skip</p>\n</li>\n<li>\n<p><code>(0,2)</code> visited — skip</p>\n</li>\n<li>\n<p><code>(0,3)</code> new rectangle start. We repeat a process in <a href=\"#find-a-rectangle\">Find a rectangle</a> step.</p>\n</li>\n</ul>\n<h2 id=\"result\">Result</h2><p>So that is basically it. We get following picture when we end main loop iteration:</p>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/3_iterate.png?v=f01ff34d\" alt=\"Dissecting\" width=\"500\" height=\"601\"></p>\n<p>And that&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.</p>\n<p><img src=\"https://notes.vyrtsev.com/posts/reducing_rectangles/4_output.png?v=d6fbddb3\" alt=\"Output\" width=\"500\" height=\"601\"></p>\n","date_published":"2023-04-10T21:50:07+03:00","id":"https://notes.vyrtsev.com/posts/reducing_rectangles/","summary":"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…","tags":["Development","Algorithms"],"title":"Reducing multiple overlapping rectangles into set of non-overlapping ones","url":"https://notes.vyrtsev.com/posts/reducing_rectangles/"}],"language":"en-us","title":"Algorithms on Blips and Blinks","version":"https://jsonfeed.org/version/1.1"}
