Determine whether two lines would intersect on a Cartesian plane

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 ax+by+c=0 on a Cartesian plane and the slope is given by -\frac{a}{b}. Therefore if -\frac{a_{0}}{b_{0}} \neq -\frac{a_{1}}{b_{1}} for two lines, they will intersect.


	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;


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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: