title: Combine Adjacent 2D Points/Ranges
body: <p>I'm using <a href="https://apireference.aspose.com/java/cells" rel="nofollow noreferrer">Aspose Cells for Java</a>.</p>
  
  <p>I'm operating on an Excel spreadsheet. I'd like to iterate over every cell, and if two cells are touching and share the same style ID, combine them into a separate single range for later processing. As more cells are iterated over, existing ranges could grow.</p>
  
  <p>A range consists of <code>startRow</code>, <code>endRow</code>, <code>startCol</code>, and <code>endCol</code> properties. If all four properties are equal, it represents a single cell.</p>
  
  <p>For example, I'd expect <code>{(1,1),(1,1)}</code> and <code>{(2,2),(1,1)}</code> to combine into <code>{(1,2),(1,1)}</code>. Meanwhile <code>{(1,1),(1,1)}</code> and <code>{(2,2),(2,2)}</code> would not combine because they are diagonal, not adjacent.</p>
  
  <p>I think I could use a variation of an <a href="https://www.geeksforgeeks.org/interval-tree/" rel="nofollow noreferrer">interval tree</a> for this, but I'd have to adapt it for 2D intervals. However, in this case I don't care if two ranges overlap; if two ranges overlap, they are overlapping rectangles, in which case combining them would do nothing for minimizing the number of total ranges.</p>
  
  <p><a href="https://i.imgur.com/fhbtbdZ.png" rel="nofollow noreferrer">Here is a crudely drawn idea of the intput/output I expect</a>.</p>
  
  <p>My best guess is that I'd have to iterate outward from every cell and compare the style IDs. As I go over and down from the current cell, check the rest of the cells in that range for the same ID and create new ranges based on that.</p>
  
  <p>Thoughts?</p>