When I read a book today, I found the following code to check whether two lines intersect:
struct Line{
static double epsilon = 0.000001;
double slope;
double yintercept;
};
bool intersect(Line& line_1, Line& line_2)
{
bool ret_1 = abs(line_1.slope - line_2.slope) > epsilon;
bool ret_2 = abs(line_1.yintercept - line_2.yintercept) < epsilon;
return ret_1 || ret_2;
}
The program uses slope and y-intercept to determine whether two line intersect. However, I am confusing here why we need epsilon
? Why cannot directly make use of '==' here?
There is a common below this method. The author says never check for equality with ==. Instead, check if the difference is less than an epsilon value.