There may have been questions asked about finding if a point is on a line but not in this context and they don't go into answering why various methods have different flaws. What is the best method for accurately finding if a point is on a line given the the coordinate of the point and the coordinates of the line? I've attempted to implement certain methods on my own but they all seem to have their own problems after tests. I'm sure there are other methods out there and I know Java has a method that calculates the distance of a point from a line and returns 0 if its on the line. What method does Java use to find out if a point is on a line?
Method One
The first method compares the distance from point A to point B with the distance from point A to C plus point C to B. The problem with this method is that it isn't precise since it uses Math.sqrt.
public static boolean inLine(double x1, double y1, double x2, double y2, double x3, double y3){
if((distance(x1, y1, x3, y3) + distance(x2, y2, x3, y3)) == distance(x1, y1, x2, y2)){
return true;
}
return false;
}
public static double distance(double x1, double y1, double x2, double y2){
double base = x2 - x1;
double height = y2 - y1;
double hypotenuse = Math.sqrt((base * base) + (height * height));
return hypotenuse;
}
Method Two
This is the second method I came up with. It checks if the point is on the line by comparing the the y value of the point to the y value on the line with the same x value as the point. The problem with this method is that it won't work when the line is vertical or horizontal so I implemented some tests to check if its on the line if the line is horizontal or vertical.
public static boolean inLine(double x1, double y1, double x2, double y2, double x3, double y3){
//Check if X and Y Values of the point are within the range of the line
if(inBetween(x1, x2, x3) & inBetween(y1, y2, y3) ){
//Check if denominator is going to equal 0 when finding the slope and x of point has the same value
if(x1 == x2 && x2 == x3){
if(inBetween(y1, y2, y3)){
return true;
}else{
return false;
}
}else if(y1 == y2 && y2 == y3){
if(inBetween(x1, x2, x3)){
return true;
}else{
return false;
}
}else{
double slope = (y2-y1)/(x2-x1);
//Check if the y value of the line is equal to the y value of the point
if(findYIntercept(slope, x1, y1)+slope*x3 == y3){
return true;
}
}
}else{
return false;
}
return false;
}
public static double findYIntercept(double slope, double x, double y){
return y-(slope*x);
}
public static boolean inBetween(double a, double b, double c){
if(a <= c && c <= b){
return true;
}
else if(a >= c && c >= b){
return true;
}
return false;
}
Method Three
This method is similar to the second method but checks if the slopes are identical. It also doesn't work if the line is horizontal or vertical. I also have implemented a situation if the line is horizontal or vertical.
public static boolean inLine(double x1, double y1, double x2, double y2, double x3, double y3){
//Check if X and Y Values of the point are within the range of the line
if(inBetween(x1, x2, x3) & inBetween(y1, y2, y3) ){
//Check if denominator is going to equal 0 when finding the slope and x of point has the same value
if(x1 == x2 && x2 == x3){
if(inBetween(y1, y2, y3)){
return true;
}else{
return false;
}
}else if(y1 == y2 && y2 == y3){
if(inBetween(x1, x2, x3)){
return true;
}else{
return false;
}
}else{
double slope1 = (y2-y1)/(x2-x1);
double slope2 = (y3-y1)/(x3-x1);
//Check if the y value of the line is equal to the y value of the point
if(slope1 == slope2){
return true;
}
}
}else{
return false;
}
return false;
}
public static double findYIntercept(double slope, double x, double y){
return y-(slope*x);
}
public static boolean inBetween(double a, double b, double c){
if(a <= c && c <= b){
return true;
}
else if(a >= c && c >= b){
return true;
}
return false;
}