Let A, B, C be some points.
The easiest way to verify that they are on the same line is to count the cross-products of the vectors BA, CA.
If it is zero, they are on the same line:
// X_ab, Y_ab - coordinates of vector BA. float X_ab = Bx - Ax float Y_ab = By - Ay // X_ac, Y_ac - coordinates of vector CA. float X_ac = Cx - Ax float Y_ac = Cy - Ay float crossproduct = Y_ab * X_ac - X_ab * Y_ac if (crossproduct < EPS) // if crossprudct == 0 { // on the same line. } else { // not on the same line. }
Once you know that A, B, C are on the same line, it is easy to see if B is between A and C, the inner product of the vectors BA and CA. If B lies between A and C, then (BA) has the same direction as (CA), and the inner product is> 0, 0:
float innerproduct = X_ab * X_ac + Y_ab * Y_ac; if (innerproduct > 0) { // B is between A and C. } else { // B is not between A and C. }
Max
source share