Guest User

ps6_visualise

a guest
May 12th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. # Problem Set 6:
  2. # Visualization code for simulated robots.
  3. #
  4. # See the problem set for instructions on how to use this code.
  5.  
  6. import math
  7. import time
  8.  
  9. from Tkinter import *
  10.  
  11. class RobotVisualization:
  12. def __init__(self, num_robots, width, height, delay = 0.2):
  13. "Initializes a visualization with the specified parameters."
  14. # Number of seconds to pause after each frame
  15. self.delay = delay
  16.  
  17. self.max_dim = max(width, height)
  18. self.width = width
  19. self.height = height
  20. self.num_robots = num_robots
  21.  
  22. # Initialize a drawing surface
  23. self.master = Tk()
  24. self.w = Canvas(self.master, width=500, height=500)
  25. self.w.pack()
  26. self.master.update()
  27.  
  28. # Draw a backing and lines
  29. x1, y1 = self._map_coords(0, 0)
  30. x2, y2 = self._map_coords(width, height)
  31. self.w.create_rectangle(x1, y1, x2, y2, fill = "white")
  32.  
  33. # Draw gray squares for dirty tiles
  34. self.tiles = {}
  35. for i in range(width):
  36. for j in range(height):
  37. x1, y1 = self._map_coords(i, j)
  38. x2, y2 = self._map_coords(i + 1, j + 1)
  39. self.tiles[(i, j)] = self.w.create_rectangle(x1, y1, x2, y2,
  40. fill = "gray")
  41.  
  42. # Draw gridlines
  43. for i in range(width + 1):
  44. x1, y1 = self._map_coords(i, 0)
  45. x2, y2 = self._map_coords(i, height)
  46. self.w.create_line(x1, y1, x2, y2)
  47. for i in range(height + 1):
  48. x1, y1 = self._map_coords(0, i)
  49. x2, y2 = self._map_coords(width, i)
  50. self.w.create_line(x1, y1, x2, y2)
  51.  
  52. # Draw some status text
  53. self.robots = None
  54. self.text = self.w.create_text(25, 0, anchor=NW,
  55. text=self._status_string(0, 0))
  56. self.time = 0
  57. self.master.update()
  58.  
  59. def _status_string(self, time, num_clean_tiles):
  60. "Returns an appropriate status string to print."
  61. percent_clean = 100 * num_clean_tiles / (self.width * self.height)
  62. return "Time: %04d; %d tiles (%d%%) cleaned" % \
  63. (time, num_clean_tiles, percent_clean)
  64.  
  65. def _map_coords(self, x, y):
  66. "Maps grid positions to window positions (in pixels)."
  67. return (250 + 450 * ((x - self.width / 2.0) / self.max_dim),
  68. 250 + 450 * ((self.height / 2.0 - y) / self.max_dim))
  69.  
  70. def _draw_robot(self, position, direction):
  71. "Returns a polygon representing a robot with the specified parameters."
  72. x, y = position.getX(), position.getY()
  73. d1 = direction + 165
  74. d2 = direction - 165
  75. x1, y1 = self._map_coords(x, y)
  76. x2, y2 = self._map_coords(x + 0.6 * math.sin(math.radians(d1)),
  77. y + 0.6 * math.cos(math.radians(d1)))
  78. x3, y3 = self._map_coords(x + 0.6 * math.sin(math.radians(d2)),
  79. y + 0.6 * math.cos(math.radians(d2)))
  80. return self.w.create_polygon([x1, y1, x2, y2, x3, y3], fill="red")
  81.  
  82. def update(self, room, robots):
  83. "Redraws the visualization with the specified room and robot state."
  84. # Removes a gray square for any tiles have been cleaned.
  85.  
  86. # for i in range(self.width):
  87. # for j in range(self.height):
  88. # if room.isTileCleaned(room.getDirty):
  89. # self.w.delete(self.tiles[(i, j)])
  90. for tile in room.getCleanTiles():
  91. self.w.delete(self.tiles[(int(tile.split(",")[0]), int(tile.split(",")[1]))])
  92.  
  93. # Delete all existing robots.
  94. if self.robots:
  95. for robot in self.robots:
  96. self.w.delete(robot)
  97. self.master.update_idletasks()
  98. # Draw new robots
  99. self.robots = []
  100. for robot in robots:
  101. pos = robot.getRobotPosition()
  102. x, y = pos.getX(), pos.getY()
  103. x1, y1 = self._map_coords(x - 0.08, y - 0.08)
  104. x2, y2 = self._map_coords(x + 0.08, y + 0.08)
  105. self.robots.append(self.w.create_oval(x1, y1, x2, y2,
  106. fill = "black"))
  107. self.robots.append(
  108. self._draw_robot(robot.getRobotPosition(), robot.getRobotDirection()))
  109. # Update text
  110. self.w.delete(self.text)
  111. self.time += 1
  112. self.text = self.w.create_text(
  113. 25, 0, anchor=NW,
  114. text=self._status_string(self.time, room.getNumCleanedTiles()))
  115. self.master.update()
  116. time.sleep(self.delay)
  117.  
  118. def done(self):
  119. "Indicate that the animation is done so that we allow the user to close the window."
  120. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment