Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. from itertools import product, chain, starmap
  2.  
  3. SURFACE = 0b01
  4. SIDE = 0b10
  5. def square(x, y, radius, min_x, min_y, max_x, max_y, mode):
  6. """
  7. Coordinates in/on the square center: (x, y), side length: 2 * radius
  8.  
  9. :param x: The center abscissa
  10. :param y: The center ordinate
  11. :param radius: The square "radius"
  12. :param min_x: The minimum abscissa value (included)
  13. :param min_y: The minimum ordinate value (included)
  14. :param max_x: The maximum abscissa value (exluded)
  15. :param max_y: The maximum ordinate value (exluded)
  16. :param mode: SURFACE, SIDE or both
  17.  
  18. The function creates a square whose center is at (:x:, :y:) with
  19. a side length equals to two times the :radius:. It also creates
  20. a second square out of the Point(:min_x:, :min_y:) and the
  21. Point(:max_x: - 1, :max_y: - 1). Only points that are part of both
  22. squares are returned.
  23.  
  24. In SIDE mode, it matches points on the side of the square, points
  25. whose distance to the center is exactly equals to :radius:.
  26.  
  27. In SURFACE mode, it matches points in the surface of the square,
  28. points whose distance to the center is stricly less than :radius:.
  29.  
  30. Example:
  31.  
  32. 000000 Center #, radius 3, min_x 0, min_y 0, max_x 5, max_y 4
  33. 01111- 0: outside the square
  34. 01222- 1: matched in SIDE mode
  35. 01222- 2: matched in SURFACE mode
  36. 0--#-- -: excluded by the boundaries
  37. """
  38. inbound = lambda point: min_x <= point[0] < max_x \
  39. and min_y <= point[1] < max_y
  40. translate = lambda vector: (x + vector[0], y + vector[1])
  41. notcenter = lambda vector: vector[0] != 0 or vector[1] != 0
  42. vectors = []
  43.  
  44. if mode & SURFACE:
  45. vectors.append((range(-radius + 1, radius), range(-radius + 1, radius)))
  46.  
  47. if mode & SIDE:
  48. vectors.append(((-radius, radius), range(-radius, radius + 1)))
  49. vectors.append((range(-radius + 1, radius), (-radius, radius)))
  50.  
  51. yield from filter(inbound,
  52. map(translate,
  53. chain.from_iterable(
  54. starmap(product,
  55. vectors))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement