Find if point lays on line segment

前端 未结 12 1305
抹茶落季
抹茶落季 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:51

    You could check if the point lies between the two planes defined by point1 and point2 and the line direction:

    ///  Returns the closest point from @a point to this line on this line.
    vector3 
    line3d ::closest_point (const vector3  & point) const
    {
        return this -> point () + direction () * dot (point - this -> point (), direction ());
    }
    
    ///  Returns true if @a point lies between point1 and point2.
    template 
    bool
    line_segment3 ::is_between (const vector3  & point) const
    {
        const auto closest = line () .closest_point (point);
        return abs ((closest - point0 ()) + (closest - point1 ())) <= abs (point0 () - point1 ());
    }
    

提交回复
热议问题