Guest User

Untitled

a guest
Feb 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import random
  2. import time
  3.  
  4. class Item():
  5. def __init__(self, x, y, z, width, height, depth):
  6. self.x = x
  7. self.y = y
  8. self.z = z
  9. self.width = width
  10. self.height = height
  11. self.depth = depth
  12.  
  13. def intersects(self, other):
  14. if self.x + self.width < other.x:
  15. return False
  16. if other.x + other.width < self.x:
  17. return False
  18. if self.y + self.height < other.y:
  19. return False
  20. if other.y + other.height < self.y:
  21. return False
  22. if self.z + self.depth < other.z:
  23. return False
  24. if other.z + other.depth < self.z:
  25. return False
  26. return True
  27.  
  28.  
  29. # Make this class faster. I don't care how you do it. There's some low hanging fruit
  30. # and that's fine. Just do something to speed up the find_items_at_point.
  31. class Space():
  32. def __init__(self):
  33. self.items = []
  34.  
  35. def insert(self, item):
  36. self.items.append(item)
  37.  
  38. def find_items_at_point(self, x, y, z):
  39. point = Item(x, y, z, 0, 0, 0)
  40. ret = []
  41. for item in self.items:
  42. if item.intersects(point):
  43. ret.append(item)
  44. return ret
  45.  
  46.  
  47. def simulate(insertions, queries):
  48. def random_item():
  49. return Item(100 * random.random(),
  50. 100 * random.random(),
  51. 100 * random.random(),
  52. random.random(),
  53. random.random(),
  54. random.random())
  55.  
  56. space = Space()
  57. for _ in range(insertions):
  58. space.insert(random_item())
  59.  
  60. for _ in range(queries):
  61. space.find_items_at_point(100 * random.random(),
  62. 100 * random.random(),
  63. 100 * random.random())
  64.  
  65. start = time.time()
  66. simulate(10000, 1000)
  67. end = time.time()
  68. print(end - start)
Add Comment
Please, Sign In to add comment