rectangle.py 904 B

12345678910111213141516171819202122
  1. '''
  2. Created on 8 nov. 2016
  3. Rectangle algorythms
  4. rectangles are assumed to be composed of vertical/horizontal sides
  5. @author: olinox
  6. '''
  7. def rect(x1, y1, x2, y2):
  8. """return a list of cells in a rectangle between (X1, Y1), (X2, Y2)"""
  9. if not all(isinstance(val, int) for val in [x1, y1, x2, y2]):
  10. raise TypeError("x1, y1, x2, y2 should be integers")
  11. xa, ya, xb, yb = min([x1, x2]), min([y1, y2]), max([x1, x2]), max([y1, y2])
  12. return [(x, y) for x in range(xa, xb + 1) for y in range(ya, yb + 1)]
  13. def hollow_rect(x1, y1, x2, y2):
  14. """return a list of cells composing the sides of the rectangle between (X1, Y1), (X2, Y2)"""
  15. if not all(isinstance(val, int) for val in [x1, y1, x2, y2]):
  16. raise TypeError("x1, y1, x2, y2 should be integers")
  17. return [(x, y) for x, y in rect(x1, y1, x2, y2)
  18. if (x == x1 or x == x2 or y == y1 or y == y2)]