Grid.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. '''
  2. Created on 7 nov. 2016
  3. Game Grid
  4. @author: olinox
  5. '''
  6. from core import bresenham, triangle, rectangle
  7. from core.constants import GRID_GEOMETRIES
  8. from core.triangle import ANGLES
  9. class Grid(object):
  10. def __init__(self, geometry, width, height):
  11. self._geometry = geometry
  12. self._width = width
  13. self._height = height
  14. # properties
  15. @property
  16. def geometry(self):
  17. return self._geometry
  18. @geometry.setter
  19. def geometry(self, geometry):
  20. if not geometry in GRID_GEOMETRIES:
  21. raise ValueError("'geometry' has to be a value from GRID_GEOMETRIES")
  22. self._geometry = geometry
  23. @property
  24. def width(self):
  25. return self._width
  26. @width.setter
  27. def width(self, width):
  28. if not isinstance(width, int) or not width > 0:
  29. raise ValueError("'width' has to be a strictly positive integer")
  30. self._width = width
  31. @property
  32. def height(self):
  33. return self._height
  34. @height.setter
  35. def height(self, height):
  36. if not isinstance(height, int) or not height > 0:
  37. raise ValueError("'width' has to be a strictly positive integer")
  38. self._height = height
  39. # methods
  40. def cases_number(self):
  41. return self.height * self.width
  42. def in_grid(self, x, y):
  43. """return True if the coordinates are in the grid"""
  44. return (x > 0 and x <= self._width and y > 0 and y <= self._height)
  45. def line(self, x1, y1, x2, y2):
  46. return bresenham.line2d(self.geometry, x1, y1, x2, y2)
  47. def line3d(self, x1, y1, z1, x2, y2, z2):
  48. return bresenham.line3d(self.geometry, x1, y1, z1, x2, y2, z2)
  49. def triangle(self, xa, ya, xh, yh, iAngle):
  50. return triangle.triangle(self.geometry, xa, ya, xh, yh, iAngle)
  51. def triangle3d(self, xa, ya, za, xh, yh, zh, iAngle):
  52. return triangle.triangle3d(self.geometry, xa, ya, za, xh, yh, zh, iAngle)
  53. def rect(self, x1, y1, x2, y2):
  54. return rectangle.rect(x1, y1, x2, y2)
  55. def hollow_rect(self, x1, y1, x2, y2):
  56. return rectangle.hollow_rect(x1, y1, x2, y2)
  57. if __name__ == '__main__':
  58. gr = Grid(5, 100, 100)
  59. print(gr.cases_number())
  60. print(gr.line(1,1,5,10))
  61. print(gr.line3d(1,1,1,5,10,10))
  62. print(gr.triangle(1,1,5,10,ANGLES[1]))
  63. print(gr.triangle3d(1,1,1,5,10,10, ANGLES[1]))