Point in Line
Published:
Define Problem
The problem of points in line is a very classical geometric problem.Algorithm explaination
If the point located in the line, the following scenario will form.
It will causedist(AC) + dist(CB) == dist(AB)
If the point not located in the line, the following scenario will form.
It will causedist(AC) + dist(CB) != dist(AB)
Therefore, we can create the function to solve the problem easily.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;
}