Distance between a point and a line

Given a line r passing through two points A and B and assigned a point P, this function determines the projection of P on the line, evaluates its distance from the line and it indicates whether the projection is internal to the segment AB.


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import mathlab

importing the Math and Physics Lab project

equation describing a line between two points A-B: $$x = x_{\mathrm{A}} + s * (x_{\mathrm{B}} - x_{\mathrm{A}})$$ $$y = y_{\mathrm{A}} + s * (y_{\mathrm{B}} - y_{\mathrm{A}})$$

with: $$0 \leq s \leq 1$$

equation of a line passing through P and orthogonal to A-B: $$x = x_{\mathrm{P}} + t * sin(\alpha)$$ $$y = y_{\mathrm{P}} + t * cos(\alpha)$$

with: $$sin(\alpha) = (y_{\mathrm{B}} - y_{\mathrm{A}})/L$$ $$cos(\alpha) = (x_{\mathrm{B}} - x_{\mathrm{A}})/L$$

and

$$L = \sqrt{(x_{\mathrm{B}} - x_{\mathrm{A}})^2 + (y_{\mathrm{B}} - y_{\mathrm{A}})^2 }$$

instersection between the two line: $$x_{\mathrm{A}} + s * (x_{\mathrm{B}} - x_{\mathrm{A}}) = x_{\mathrm{P}} + t * sin(\alpha)$$ $$y_{\mathrm{A}} + s * (y_{\mathrm{B}} - y_{\mathrm{A}}) = y_{\mathrm{P}} - t * cos(\alpha)$$

system of equation: $$s * (x_{\mathrm{B}} - x_{\mathrm{A}}) - t * sin(\alpha) = x_{\mathrm{P}} - x_{\mathrm{A}}$$ $$s * (y_{\mathrm{B}} - y_{\mathrm{A}}) + t * cos(\alpha) = y_{\mathrm{P}} - y_{\mathrm{A}}$$

$$x_{\mathrm{A}} = 0; y_{\mathrm{A}} = 0; x_{\mathrm{B}} = 100; y_{\mathrm{B}} = 110; x_{\mathrm{P}} = 70; y_{\mathrm{P}} = 22$$

In [4]:
result = mathlab.point_line(0, 0, 100, 100, 70, 22)


projection point:  46.0 46.0
distance:  -33.941125497

In [ ]: