Given p(y,x)
, how do I retrieve perimeter points with offset 1 [(y-1,x), (y 1,x 1), (y,x 1),(y 1,x 1), (y 1,x), (y-1,x-1), (y,x-1), (y-1,x-1)]
in list comprehension in Python?
|------------|----------|------------|
| (y-1, x-1) | (y-1,x) | (y-1, x 1) |
|------------|----------|------------|
| (y, x-1) | (y,x) | (y, x 1) |
|------------|----------|------------|
| (y 1, x-1) | (y 1, x) | (y 1, x 1) |
|------------|----------|------------|
CodePudding user response:
Just for fun, using complex numbers:
[(round(z.imag), round(z.real))
for a in range(8)
for z in [complex(x, y) 1j**(a/2)]]
CodePudding user response:
The perimeter points are all of the form (x dx, y dy)
for suitable values of dx
and dy
.
What are those suitable values? 1
, 0
, and -1
.
from itertools import product
pp = [(x dx, y dy) for dx, dy in product([-1, 0, 1], repeat=2) if dx != 0 or dy != 0]
The filter prevent you from adding (x,y)
itself to the result (i.e., when dx == 0
and dy == 0
).