fork download
  1. # Modelado de una torre de contral
  2. # con OVs (objetos volantes),
  3. # que pueden colisionar.
  4.  
  5.  
  6. import math
  7.  
  8.  
  9. class Pos:
  10. """Pos: x, y, z"""
  11. X = 0
  12. Y = 1
  13. Z = 2
  14. ORG = None
  15.  
  16. @staticmethod
  17. def org():
  18. """Devuelve el origen de coordenadas.
  19. :return: el objeto Pos(0, 0, 0)
  20. """
  21. if not Pos.ORG:
  22. Pos.ORG = Pos(0, 0, 0)
  23. ...
  24.  
  25. return Pos.ORG
  26. ...
  27.  
  28. def __init__(self, x, y, z):
  29. self._pos = [x, y, z]
  30. ...
  31.  
  32. def __len__(self):
  33. return len(self._pos)
  34. ...
  35.  
  36. def __getitem__(self, item):
  37. return self._pos[item]
  38. ...
  39.  
  40. @property
  41. def x(self):
  42. return self._pos[0]
  43. ...
  44.  
  45. @property
  46. def y(self):
  47. return self._pos[1]
  48. ...
  49.  
  50. @property
  51. def z(self):
  52. return self._pos[2]
  53. ...
  54.  
  55. def distancia_a(self, otro):
  56. """Calcula la distancia entre esta pos. y otra dada.
  57. :param otro: Una pos. dada.
  58. :return: la distancia, como un decimal.
  59. """
  60. delta__sq_x = (self.x - otro.x) ** 2
  61. delta__sq_y = (self.y - otro.y) ** 2
  62. delta__sq_z = (self.z - otro.z) ** 2
  63. return math.sqrt(delta__sq_x + delta__sq_y + delta__sq_z)
  64. ...
  65.  
  66. def __sub__(self, otro):
  67. return self.distancia_a(otro)
  68. ...
  69.  
  70. def __eq__(self, otro):
  71. return (otro
  72. and self.x == otro.x
  73. and self.y == otro.y
  74. and self.z == otro.z)
  75. ...
  76.  
  77. def __ne__(self, otro):
  78. return not(self == otro)
  79. ...
  80.  
  81. def __str__(self):
  82. return f"({self.x}, {self.y}, {self.z})"
  83. ...
  84. ...
  85.  
  86.  
  87. class OV:
  88. """Cualquier objecto volante."""
  89. def __init__(self, x, y, z):
  90. self._pos = Pos(x, y, z)
  91. ...
  92.  
  93. @property
  94. def pos(self):
  95. return self._pos
  96. ...
  97.  
  98. def __str__(self):
  99. return f"{self.pos}"
  100. ...
  101. ...
  102.  
  103.  
  104. class OVNI(OV):
  105. """Cualquier objeto volante no identificado."""
  106. def __init__(self, x, y, z):
  107. super().__init__(x, y, z)
  108. ...
  109.  
  110. def __str__(self):
  111. return "ovni/" + super().__str__()
  112. ...
  113. ...
  114.  
  115.  
  116. class OVI(OV):
  117. """Cualquier objeto volante con empresa y num. de vuelo."""
  118. def __init__(self, com, num_vuelo, x, y, z):
  119. super().__init__(x, y, z)
  120. self._com = str(com).capitalize()
  121. self._num_vuelo = num_vuelo
  122. ...
  123.  
  124. @property
  125. def com(self):
  126. """Devuelve el nombre de la empresa."""
  127. return self._com
  128.  
  129. @property
  130. def num_vuelo(self):
  131. """Devuelve el num. de vuelo."""
  132. return self._num_vuelo
  133. ...
  134.  
  135. def __str__(self):
  136. return f"{self.com} {str(self.num_vuelo)}/{super().__str__()}"
  137. ...
  138. ...
  139.  
  140.  
  141. class TorreControl:
  142. """Desde la torre de control se controlan los vuelos."""
  143. def __init__(self):
  144. self._vuelos = []
  145. ...
  146.  
  147. def __add__(self, otro: OV):
  148. self._vuelos += [otro]
  149. return self
  150. ...
  151.  
  152. @property
  153. def vuelos(self):
  154. """Devuelve una lista con los vuelos actuales."""
  155. return list(self._vuelos)
  156. ...
  157.  
  158. def avisos_colision(self, limite: float):
  159. toret = []
  160.  
  161. for i in range(len(self.vuelos)):
  162. for j in range(i + 1, len(self._vuelos)):
  163. if (self._vuelos[i].pos - self._vuelos[j].pos) < limite:
  164. toret += [(self._vuelos[i], self._vuelos[j])]
  165. ...
  166. ...
  167. ...
  168.  
  169. return toret
  170. ...
  171.  
  172. def __str__(self):
  173. return str.join(", ", [str(v) for v in self._vuelos])
  174. ...
  175. ...
  176.  
  177.  
  178. if __name__ == "__main__":
  179. t = TorreControl()
  180. ov1 = OVI("iberia", 1212, 10, 100, 15000)
  181. ov2 = OVI("qantas", 3456, 100, 150, 12000)
  182. ov3 = OVNI(110, 160, 13000)
  183. colisiones = t.avisos_colision(500)
  184.  
  185. t += ov1
  186. t += ov2
  187. t += ov3
  188.  
  189. print(f"torre: {t}")
  190. print(f"Avisos colisión:\n{str.join(", ", [str(v1) + " !! " + str(v2) for (v1, v2) in colisiones])}")
  191. ...
  192.  
Success #stdin #stdout 0.1s 13992KB
stdin
Standard input is empty
stdout
torre: Iberia 1212/(10, 100, 15000), Qantas 3456/(100, 150, 12000), ovni/(110, 160, 13000)
Avisos colisión: