[Math] How to determine if two lines are parallel/ almost parallel

geometry

I have $2$ lines in this form
Line $1$: $(x_1,y_1)$ $(x_2,y_2)$
Line $2$: $(x_3,y_3)$ $(x_4,y_4)$

I want to detect if the two lines are parallel or almost parallel.

My idea is to if the angle between the two lines is $\leq$ some threshold angle (like $10$ degrees or so), then they are almost parallel.

But don't know how to compute angle between two lines.

Please help me with the equations or if there is any idea to detect almost parallel lines.

Thank you.

Best Answer

A line of slope $m$ makes a signed angle with the $x$-axis equal to $\theta = \arctan(m)$ (expressed in radians), and this angle has value between $-\pi/2$ and $+\pi/2$.

For two lines of slopes $m_1$, $m_2$, which make signed angles equal to $\theta_1 = \arctan(m_1)$ and $\theta_2 = \arctan(m_2)$, the "angular distance" between those two lines will be either $|\theta_1 - \theta_2|$ or $\pi - |\theta_1 - \theta_2|$, whichever is smaller. The alternative $|\theta_1 - \theta_2|$ is used when that number is $\le \pi/2$, and if that number is $> \pi/2$ then the other alternative $\pi - |\theta_1 - \theta_2]$ will be $\le \pi/2$.

Once you have computed the angular distance between the two lines, set it to be less than whatever threshold you desire.

Related Question