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?
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 ());
}