Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import product, chain, starmap
- SURFACE = 0b01
- SIDE = 0b10
- def square(x, y, radius, min_x, min_y, max_x, max_y, mode):
- """
- Coordinates in/on the square center: (x, y), side length: 2 * radius
- :param x: The center abscissa
- :param y: The center ordinate
- :param radius: The square "radius"
- :param min_x: The minimum abscissa value (included)
- :param min_y: The minimum ordinate value (included)
- :param max_x: The maximum abscissa value (exluded)
- :param max_y: The maximum ordinate value (exluded)
- :param mode: SURFACE, SIDE or both
- The function creates a square whose center is at (:x:, :y:) with
- a side length equals to two times the :radius:. It also creates
- a second square out of the Point(:min_x:, :min_y:) and the
- Point(:max_x: - 1, :max_y: - 1). Only points that are part of both
- squares are returned.
- In SIDE mode, it matches points on the side of the square, points
- whose distance to the center is exactly equals to :radius:.
- In SURFACE mode, it matches points in the surface of the square,
- points whose distance to the center is stricly less than :radius:.
- Example:
- 000000 Center #, radius 3, min_x 0, min_y 0, max_x 5, max_y 4
- 01111- 0: outside the square
- 01222- 1: matched in SIDE mode
- 01222- 2: matched in SURFACE mode
- 0--#-- -: excluded by the boundaries
- """
- inbound = lambda point: min_x <= point[0] < max_x \
- and min_y <= point[1] < max_y
- translate = lambda vector: (x + vector[0], y + vector[1])
- notcenter = lambda vector: vector[0] != 0 or vector[1] != 0
- vectors = []
- if mode & SURFACE:
- vectors.append((range(-radius + 1, radius), range(-radius + 1, radius)))
- if mode & SIDE:
- vectors.append(((-radius, radius), range(-radius, radius + 1)))
- vectors.append((range(-radius + 1, radius), (-radius, radius)))
- yield from filter(inbound,
- map(translate,
- chain.from_iterable(
- starmap(product,
- vectors))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement