# Distance from a Line

## Distance Between a Point and a Line

The following 3 methods can be used to find the Distance Between a Point and a Line

1. Distance D of a point C from any line having direction ratio $$\vec{A}$$ making a vector $$\vec{B}$$ with any point on the line is given by following formula

$$D = \frac{\vert\vec{A}\times\vec{B}\vert}{\vert \vec{A} \vert}$$

The formula given above is applicable for both 2D and 3D lines and it always gives a positive distance value.
2. For 2D only, distance D of a point C from any line having normal vector $$\vec{A}$$ making a vector $$\vec{B}$$ with any point on the line is given by following formula

$$D = \frac{\vec{A}\cdot\vec{B}}{\vert \vec{A} \vert} \hspace{.5cm} D_{abs} = \frac{|\vec{A}\cdot\vec{B|}}{\vert \vec{A} \vert}$$

The above formula gives a signed distance values (i.e. the distance values can be negative or positive). To find the actual distance absolute value must be taken.
3. If a 2D line is given by equation $$ax + by + c$$ then distance D of this line from a point C having coordinates (x1,y1) is given by following formula

$$D = \frac{ax_1 + by_1 + c}{\sqrt{a^2 + b^2}} \hspace{.5cm} D_{abs} = \frac{|ax_1 + by_1 + c|}{\sqrt{a^2 + b^2}}$$

The above formula also gives a signed distance values (i.e. the distance values can be negative or positive). To find the actual distance absolute value must be taken.
The distance from the origin is given by the following

$$D = \frac{c}{\sqrt{a^2 + b^2}} \hspace{.5cm} D_{abs} = \frac{|c|}{\sqrt{a^2 + b^2}}$$

## Distance Between 2 Lines

The following steps can be used to find distance between Line A having direction ratio vector as $$\vec{A}$$ passing through a point having position vector $$\vec{C}$$ and Line B having direction ratio vector as $$\vec{B}$$ passing through a point having position vector $$\vec{D}$$.

1. Find $$\vec{A}\times\vec{B}$$.
2. If the above cross product is zero then the lines are parallel (or co-incident). The distance between these to lines can be found out by finding the distance of point on one line to the other line by any of the applicable methods given above. In 2D distance between 2 parallel lines can also be found out by finding the absolute value of difference of the distance of the lines from origin.
3. In 2D, if the above cross product is not zero then the lines are intersecting and hence distance does not have a meaning.
4. In 3D, if the above cross product is not zero then calculate the unit vector

$$\hat{n} = \frac{\vec{A}\times\vec{B}}{\vert \vec{A}\times\vec{B} \vert}$$

The distance D can be found out by the following formula

$$D = \hat{n} \cdot (\vec{C}-\vec{D})$$

The above formula gives a signed distance values (i.e. the distance values can be negative or positive).
For 2D, $$\vec{A}$$ and $$\vec{B}$$ can also be normal vectors of the Lines A and B.