Find the points beyond a line using Python

情到浓时终转凉″ 提交于 2019-12-11 03:34:17

问题


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.

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)

回答1:


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.



来源:https://stackoverflow.com/questions/45505032/find-the-points-beyond-a-line-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!