Point in Line

less than 1 minute read

Published:

  1. Define Problem


    The problem of points in line is a very classical geometric problem.

  2. Algorithm explaination
    If the point located in the line, the following scenario will form.


    It will cause dist(AC) + dist(CB) == dist(AB)

    If the point not located in the line, the following scenario will form.


    It will cause dist(AC) + dist(CB) != dist(AB)
    Therefore, we can create the function to solve the problem easily.

  3. Code implementation

bool point_in_line(vector<vector<double>> line, vector<double> point)
{
    double distance_AC = sqrt(pow((line[0][0]-point[0]),2)+pow((line[0][1]-point[1]),2));
    double distance_BC = sqrt(pow((line[1][0]-point[0]),2)+pow((line[1][1]-point[1]),2));
    double distance_AB = sqrt(pow((line[0][0]-line[1][0]),2)+pow((line[0][1]-line[1][1]),2));
    if (distance_AC+distance_BC == distance_AB)
        return true;
    return false;
}