# Two line Segments Calculator

This tool calculates two line Segments relative position in a 2D space : checks if they are parallel or perpendicular, intersection point, shortest distance and angle between them.
Example : for a line segment with endpoints (1,2) and (6,0), input : 1 2 6 0 (separator : blank).

Notations :
- S1 and S2 : two line segments of a cartesian plane
- Segment S1 passing through points of coordinates (a1 , b1) and (c1 , d1).
- Segment S2 passing through points of coordinates (a2 , b2) and (c2 , d2).

## Line segment Slope

Line segment slope formula is identical to the one that applies to the line passing through segment endpoints.
Slope calculator : Line Calculator

m1 : S1 slope
m2 : S2 slope

m_1 = (d_1-b_1) / (c_1-a_1)

m_2 = (d_2-b_2) / (c_2-a_2)

## Determine if two segments are parallel

Two segments are parallel if and only if one of the following conditions holds :
- segments are vertical (e.g. are both parrallel to y-axis)
- segments have the same slope, m_1 = m_2

## Determine if two segments are perpendicular

Two segments are perpendicular if and only if one of the following conditions holds :
- One of the segments is vertical (parallel to y-axis) and the other is horizontal (parallel to x-axis).
- The product of the segments slopes is equal to -1 : m_1 * m_2 = -1

## Intersection point of two segments

We're using parametric equations of the two segments S1 and S2. For more informations about line segment equation forms, see : Line segment Equations.

A point M (x,y) of the plane is common to segments S1 and S2 if and only if its coordinates x and y fulfill parametric equations of the two segments :

$$\left\{ \begin{array}{c} x=a_1+t_1.(c_1-a_1) \\ y=b_1+t_1.(d_1-b_1) \\ 0<=t_1<=1 \end{array} \right.$$

$$\left\{ \begin{array}{c} x=a_2+t_2.(c_2-a_2) \\ y=b_2+t_2.(d_2-b_2) \\ 0<=t_2<=1 \end{array} \right.$$

S1 and S2 have non-empty intersection if the following equations system has at least one solution (t1 , t2 ) with 0 ≤ t1 ≤1 and 0 ≤ t2 ≤ 1 :

$$\left\{ \begin{array}{c} a_1+t_1.(c_1-a_1) = a_2+t_2.(c_2-a_2) \\ b_1+t_1.(d_1-b_1) = b_2+t_2.(d_2-b_2) \\ \end{array} \right.$$

This system can be rewritten as follows,

$$\left\{ \begin{array}{c} t_1.(c_1-a_1) - t_2.(c_2-a_2) = a_2-a_1 \\ t_1.(d_1-b_1) - t_2.(d_2-b_2) = b_2- b_1 \\ \end{array} \right.$$

This is a Cramer system which solutions can be found using our calculator : Cramer system Calculator.

The most important thing in a Cramer system is the determinant of its coefficients matrix :

\text{det}([(c_1-a_1,-(c_2-a_2)),(d_1-b_1,-(d_2-b_2))]) = -(c_1-a_1)*(d_2-b_2)+(c_2-a_2)*(d_1-b_1)

There are several possibilities :
- This determinant is non-zero so a unique solution (t1 and t2)exists. If t1 and t2 lie between 0 and 1 then, the two segments have a unique intersection point.
- If this determinant is non zero but the unique solution values (t1 and t2) does not lie between 0 and 1 then the segments do not intersect.
In fact, the lines on which lie the two segments intersect at a point outside the segments.
- The determinant is zero. In this case, either the system has no solution (case of parallel segments or segments that lie on the same line without intersecting), either, there are an infinity of solutions : the two segments overlap.

## Angle between two segments

Angle between two segments is equivalent to angle beetween lines on which lie the segments. See formula and Calculator here : Angle between two lines.