Given two lines on a Cartesian plane, determine whether the two lines would intersect.

**My initial thoughts:**

On a Cartesian plane, if two lines do not intersect, they must be parallel with each other. Hence, their slopes must be the same. If their slopes are different, they would intersect. A line is represented as on a Cartesian plane and the slope is given by . Therefore if for two lines, they will intersect.

**Solution:**

public class Line {
static final double epsilon = 0.000001;
public double slope;
public double yintercept;
public Line(double s, double y) {
slope = s;
yintercept = y;
}
public boolean intersect(Line line2) {
return Math.abs(slope - line2.slope) > epsilon
|| Math.abs(yintercept - line2.yintercept) < epsilon;
}
}

Comments:

- Two lines can be the same. In that case, we assume they intersects (overlap).
- We need to consider the floating system in a computer. Never use == to compare two floating numbers.

### Like this:

Like Loading...

*Related*