Asked • 05/08/19

Calculating the angle between two lines without having to calculate the slope? (Java)?

I have two Lines: L1 and L2. I want to calculate the angle between the two lines. L1 has points: `{(x1, y1), (x2, y2)}` and L2 has points: `{(x3, y3), (x4, y4)}`. How can I calculate the angle formed between these two lines, without having to calculate the slopes? The problem I am currently having is that sometimes I have horizontal lines (lines along the x-axis) and the following formula fails (divide by zero exception): arctan((m1 - m2) / (1 - (m1 * m2))) where `m1` and `m2` are the slopes of line 1 and line 2 respectively. Is there a formula/algorithm that can calculate the angles between the two lines without ever getting divide-by-zero exceptions? Any help would be highly appreciated. This is my code snippet: // Calculates the angle formed between two lines public static double angleBetween2Lines(Line2D line1, Line2D line2) { double slope1 = line1.getY1() - line1.getY2() / line1.getX1() - line1.getX2(); double slope2 = line2.getY1() - line2.getY2() / line2.getX1() - line2.getX2(); double angle = Math.atan((slope1 - slope2) / (1 - (slope1 * slope2))); return angle; } Thanks.

2 Answers By Expert Tutors

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.