Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Just calculate <code>Top</code> and <code>Bottom</code> values for each <code>MyClass</code>.</p> <p>Use a <code>Boundary</code> variable, initializing it to <code>0</code>.</p> <ol> <li><p>Pick an element with biggest height (<code>Bottom</code> - <code>Top</code>), whose <code>Top</code> = <code>Boundary</code>. If some elements are ax equo (same <code>Top</code>, same height), pick the one on the left.</p></li> <li><p>Shift (set) <code>Boundary</code> to <code>Bottom</code> of this element (this ensures that the next element you pick will not overlap with this one).</p></li> <li><p>Go to <code>1</code>.</p></li> </ol> <p>When going horizontally, just use <code>Left</code> instead of <code>Top</code> etc.</p> <p>Given your requirements (<em>"Will they always fit nicely together like a puzzle as you have it?"</em> - you reply <em>"yes it will always fit together"</em>) it would work as you described: 1, 4, 8.</p> <p><strong>Edit:</strong> okay. Then we just pick an element whose Top is GREATER or equal than the previous element's Bottom.</p> <p>Something along the lines of:</p> <pre><code>public List&lt;MyClass&gt; GetBiggestNotOverlappingVertically(ICollection&lt;MyClass&gt; elements) { var result = new List&lt;MyClass&gt;(); var boundary = 0; var stop = false; Func&lt;MyClass, MyClass, bool&gt; inTheSameLane = (first, second) =&gt; ((first.Left &gt;= second.Left &amp;&amp; first.Left &lt;= second.Right) || (first.Right &lt;= second.Right &amp;&amp; first.Right &gt;= second.Left)); while (!stop) { var nextElement = (from element in elements where element.Top &gt;= boundary &amp;&amp; // and where we are NOT "jumping over" any element in the same lane: // there's no element between the two (in the same lane, that is) !(from otherElement in elements where otherElement != element &amp;&amp; inTheSameLane(element, otherElement) &amp;&amp; otherElement.Top &gt;= boundary &amp;&amp; otherElement.Bottom &lt;= element.Top select otherElement).Any() orderby element.Height descending, element.Left ascending select element).FirstOrDefault(); stop = nextElement == null; if (!stop) { result.Add(nextElement); boundary = nextElement.Bottom; } } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload