title: Delimiting a square in a coordinate system
body: <p>I have a board of 3 squares by 3 squares, which looks like this:</p>
  <p><a href="https://i.stack.imgur.com/JSKrus.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JSKrus.png" alt="Board" /></a></p>
  <p>The edges for each square, are represented as a tuple, containing starting coordinates and ending coordinates for the edge, as such:</p>
  <pre><code>((0,0), (0,1))
  </code></pre>
  <p>The twist is that at any point one of these edges can be removed. Leaving a square incomplete.</p>
  <p>I'm attempting to write an algorithm in Python which detects squares on the board.</p>
  <p>The data for the board is kept in a dictionary, in which the keys are the edges and the corresponding values are booleans representing if the edge is currently in place or not, it looks a little like this:</p>
  <pre><code>   Graph=  {((0, 0), (0, 1)): True,
              ((0, 0), (1, 0)): True,
              ((0, 1), (0, 2)): True,
              ((0, 1), (1, 1)): True,
              ((0, 2), (0, 3)): True,
              ...}
  </code></pre>
  <p>Currently, if I iterate through all the points on the board, I can only find out if there are complete 1x1 squares, by finding the nearby edges and checking if they're set as True or not in my graph. I'm currently finding edges near the starting point (which is always on the bottom left corner of a square) by doing this:</p>
  <p>(<code>(x,y)</code> being the coordinates of the starting point)</p>
  <pre><code>left = ([edge for edge in graph if (x,y) in edge and (x, y+1) in edge])
  right = ([edge for edge in graph if (x+1, y) in edge and (x+1, y+1) in edge])
  top = ([edge for edge in graph if (x, y+1) in edge and (x+1, y+1) in edge])
  down = ([edge for edge in graph if (x, y) in edge and (x+1, y) in edge])[0]
  </code></pre>
  <p>I'm using list comprehension here because the orientation of the edges is currently arbitrary, and instead of directly accessing (e.g.) <code>((1,0),(1,1))</code> in the graph, I need to search it to see if it has either that or <code>((1,1),(1,0))</code>.</p>
  <p>Regardless of that little complication, what's troubling me is that I can't come up with a way to check for squares of larger dimensions. For example, I also need to check for 2x2 and 3x3 (if <code>(0,0)</code> is the starting point) squares, and for that my method is unsustainable.</p>
  <p>For example, I need to detect a 2x2 square starting in <code>(0,0)</code>, I want to detect the square whose edges are:</p>
  <pre><code>left: ((0,0),(0,1)), ((0,1),(0,2))
  right: ((2,0),(2,1)), ((2,1),(2,2))
  top: ((0,2),(1,2)), ((1,2),(2,2))
  bottom: ((0,0),(1,0)), ((1,0),(2,0))
  </code></pre>
  <p>I apologize for the messy code and appreciate any hint or tip.</p>
  <p>Thank you in advance.</p>