Given two points with x-y coordinates I have a line specified. Now I want to distinguish in an 2-d numpy array the points left to the line from those on the right.
The following code does the trick - but the double loop hurts my religious feelings (towards numpy). There must be a smarter way?
def myline(a_x,a_y,b_x,b_y):
start=np.zeros((100,100))
for x in range(100):
for y in range(100):
val= (b_x - a_x)*(y - a_y) - (x - a_x)*(b_y - a_y)
if val<=0:
start[x,y]=1
return start
This can be vectorized using ogrid
:
x, y = np.ogrid[:100, :100]
start = (b_x - a_x)*(y - a_y) - (x - a_x)*(b_y - a_y) <= 0
This will give you a boolean mask. If you want some other dtype
start = start.astype(np.int)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments