Find if point lays on line segment

前端 未结 12 1325
抹茶落季
抹茶落季 2020-11-30 04:01

I have line segment defined by two points A(x1,y1,z1) and B(x2,y2,z2) and point p(x,y,z). How can I check if the point lays on the line segment?

12条回答
  •  感动是毒
    2020-11-30 04:48

    Find the distance of point P from both the line end points A, B. If AB = AP + PB, then P lies on the line segment AB.

    AB = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1));
    AP = sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)+(z-z1)*(z-z1));
    PB = sqrt((x2-x)*(x2-x)+(y2-y)*(y2-y)+(z2-z)*(z2-z));
    if(AB == AP + PB)
        return true;
    

提交回复
热议问题