python 找过某点的线,使用Python查找线外的点

With reference to the question asked here Drawing a line given the angle and a point on the line, I have written a python function "find_normal" which returns the slope and y-intercept of the normal vector to a given point at given angle.

Now I have a list of points and I want to verify if these points are beyond that normal vector in the direction of given angle of not. e.g.

Angle = 180: True if point is to the left of the line, false otherwise

Angle = 0: True if point is to the right of the line, false otherwise

0 < Angle < 180: True if point is below the line, false otherwise

180 < Angle < 360: True if point is above the line, false otherwise

For this purpose I have written another function find_points_ahead that uses the results of find_normal to highlight all the points beyond the normal vector. As shown in the screen shot below.

jkKEm.png

But at some angles, some points that do not meet the criteria are also highlighted. For instance arrow is pointed downwards and only the points below the red line should have been highlighted. Instead some of points above the line were also highlighted. Can anyone help me here?

def find_normal(img,point,angle):

x1=point[0]

y1=point[1]

angle_rad=np.radians(angle)

c=-1*np.sin(angle_rad)

s=np.cos(angle_rad)

start_x=int(x1 - c * 640)

start_y=int(y1 - s * 640)

end_x=int(x1 + c * 640)

end_y=int(y1 + s * 640)

m=int((end_y-start_y)/(end_x-start_x))

y_intercept=end_y-(m*end_x)

cv2.line(img, (start_x,start_y), (end_x,end_y), [0, 0, 255], 3, cv2.LINE_AA)

return m,y_intercept

def find_points_ahead(img,base_point,angle,points):

m,c=find_normal(img,base_point,angle)

for pt in points:

pt_x=pt[0]

pt_y=pt[1]

result=m*pt_x-pt_y+c

if angle>0 and angle<180 and result<0:

cv2.circle(img,(pt_x,pt_y),3,[0,0,255],3,cv2.LINE_AA)

elif angle>180 and angle<360 and result>0:

cv2.circle(img,(pt_x,pt_y),3,[0,0,255],3,cv2.LINE_AA)

解决方案

If c and s are the angle's cosine and sine, the function must return True for a point (x, y) iff c*(x-x1) + s*(y-y1) >= 0 where (x1,y1) is any point on the red line.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值