plotting.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as patches
  3. import env
  4. class Plotting:
  5. def __init__(self, xI, xG):
  6. self.xI, self.xG = xI, xG
  7. self.env = env.Env()
  8. self.obs_bound = self.env.obs_boundary
  9. self.obs_circle = self.env.obs_circle
  10. def animation(self, nodelist, node_rand=None):
  11. plt.clf()
  12. # for stopping simulation with the esc key.
  13. plt.gcf().canvas.mpl_connect('key_release_event',
  14. lambda event: [exit(0) if event.key == 'escape' else None])
  15. if node_rand is not None:
  16. plt.plot(node_rand.x, node_rand.y, "^k")
  17. for node in nodelist:
  18. if node.parent:
  19. plt.plot(node.path_x, node.path_y, "-g")
  20. plt.plot(self.xI[0], self.xI[1], "b*")
  21. plt.plot(self.xG[0], self.xG[1], "g*")
  22. plt.axis("equal")
  23. plt.axis([-5, 55, -5, 35])
  24. plt.title("RRT")
  25. plt.grid(True)
  26. plt.pause(0.01)
  27. def plot_grid(self, name):
  28. fig, ax = plt.subplots()
  29. for x in self.obs_bound:
  30. ax.add_patch(
  31. patches.Rectangle(
  32. (x[0], x[1]), x[2], x[2],
  33. edgecolor='black',
  34. facecolor='black',
  35. fill=True
  36. )
  37. )
  38. for x in self.obs_circle:
  39. ax.add_patch(
  40. patches.Circle(
  41. (x[0], x[1]), x[2],
  42. edgecolor='gray',
  43. facecolor='gray',
  44. fill=True
  45. )
  46. )
  47. plt.plot(self.xI[0], self.xI[1], "b*")
  48. plt.plot(self.xG[0], self.xG[1], "g*")
  49. plt.title(name)
  50. plt.axis("equal")
  51. plt.show()
  52. def plot_visited(self, visited):
  53. visited.remove(self.xI)
  54. count = 0
  55. for x in visited:
  56. count += 1
  57. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  58. plt.gcf().canvas.mpl_connect('key_release_event',
  59. lambda event: [exit(0) if event.key == 'escape' else None])
  60. if count < len(visited) / 3:
  61. length = 15
  62. elif count < len(visited) * 2 / 3:
  63. length = 30
  64. else:
  65. length = 45
  66. if count % length == 0: plt.pause(0.001)
  67. def plot_path(self, path):
  68. path.remove(self.xI)
  69. path.remove(self.xG)
  70. path_x = [path[i][0] for i in range(len(path))]
  71. path_y = [path[i][1] for i in range(len(path))]
  72. plt.plot(path_x, path_y, linewidth='3', color='r', marker='o')
  73. plt.pause(0.01)
  74. plt.show()