Check if three points are aligned

Let’s consider three points A, B and C. The best way to know if the three points are aligned is to compute the cross product of vectors \vec{AB} and \vec{AC}. If \vec{AB} \times \vec{AC} is a null vector, the three points are aligned or two or more points coincide.

C++ source code

* \brief rOc_segment::isPointAligned check if a point is aligned with the segment
* \param P point to test
* \return ROC_SEGMENT_INTERSEC_NONE if the point doesn’t lay with the segment
* ROC_SEGMENT_INTERSEC_EXTREMITY_P1 if the point is merged with P1
* ROC_SEGMENT_INTERSEC_EXTREMITY_P2 if the point is merged with P2
* ROC_SEGMENT_INTERSEC_CROSS if the point belongs to the segment (extremity no included)
bool rOc_segment::isPointAligned(rOc_point P)
// Compute vectors AB and AC
rOc_vector AB=this->vector();
rOc_vector AC(this->point1(),P);

// Check if the cross product is a null vector
if (AB.cross(AC).isNull()) return true;
return false;

Leave a Reply

Your email address will not be published. Required fields are marked *