How can I check if a point is below a line or not ?

前端 未结 3 1643
醉梦人生
醉梦人生 2020-12-14 11:20

How can I check if a point is below a line or not ?

I\'ve the following data:

Line [ {x1,y1}, {x2,y2} ]
Points {xA,yA}, {xB,yB} ...

3条回答
  •  心在旅途
    2020-12-14 11:29

    You could try using a cross product, but the trick is how to choose the point to form a vector, here I choose the closest point from points, assume I got pointA(you can fairly loop points to calculate to distance from loop point to Line):

    v1 = {x2-x1, y2-y1}   # Vector 1
    v2 = {xA-x1, yA-y1}   # Vector 2
    cross_product = v1.x*v2.y - v1.y*v2.x
    if cross_product > 0:
        print 'pointA is on the counter-clockwise side of line'
    elif cross_product < 0:
        print 'pointA is on the clockwise side of line'
    else:
        print 'pointA is exactly on the line'
    

提交回复
热议问题