'Numpy indices in quadrilateral
Is there a smart way to get the indices of all the entries within four points? In particular this is about the case where I have four points (x_1, y_1), (x_1, y_2), (x_2, y_3), (x_2, y_4) and I need all the indices "within", including the boundaries. One could obviously loop through all x values, but as the array is quite large, I would prefer a direct way. In the end I need an array with ones inside the quadrilateral and zeros outside.
For example this input:
np.arange(1, 13).reshape((4, 3)) #= [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
x_1 = 0
x_2 = 3
y_1 = 0
y_2 = 1
y_3 = 1
y_4 = 2
should give this output:
np.array([[1,1,0],[0,1,0],[0,1,0],[0,1,1]])#numbers 1, 2, 5, 8, 11, 12 have a 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|