Peripendicular distance from a line segment I have a line given by $Ax + By + C= 0$, and a point $x0,y0$. From that point $x0,y0$ in the direction of the line up to distance $d$, I want to find the perpendicular distance of the points from this line segment.
In the figure, below I wish to calculate this only for $x1,y1$ and $x4,y4$. The points $x2,y2$ and $x3,y3$ should be excluded as they lie above the red line depicted.
Please check figure below:
 A: Translate all points so that $(x_0,y_0)$ becomes $(0,0)$.
The unit vector $(a,b):=(A,B)/\sqrt{A^2+B^2}$ is perpendicular to the black line, so that a vector $(x,y)$ decomposes as $d_\perp=(a,b)\cdot(x,y)$ and $d_\parallel=(b,-a)\cdot(x,y)$.
The answer is given by $|d_\perp|$ if $0\le d_\parallel\le d$.
A: The distance $d(P,D)$ of your $P$ with coordinates $(x_P,y_P)$ to the line $D \equiv Ax+By+X=0$ is given by the formula $$d(P,D) = \frac{\vert Ax_P + By_P+C \vert}{\sqrt{A^2+B^2}}$$
A: As best I can understand the question, what you really want to do is
related to this figure:

Here we see your point $(x_0, y_0)$, the line $L$ given by $Ax+By+C=0$,
and the $x$- and $y$-coordinate axes with respect to which
those $x$ and $y$ coordinates are defined.
(I am assuming you meant for the line $L$
to pass through the point $(x_0, y_0)$.)
The figure also shows a line $M$ through $(x_0, y_0)$
perpendicular to the line $L$.
Together, these two lines define a transformed coordinate system
that is translated and rotated with respect to the $x,y$ coordinate system.
An arbitrary point $(x,y)$ somewhere in the plane, as shown in the figure,
has coordinates $(x',y')$ in the transformed system, where
$x'$ is measured parallel to $L$ and $y'$ is measured perpendicular to $L$.
Your red line appears near the upper right corner of the figure,
perpendicular to the line $L$.
The distance from the line $M$ to the red line is $d$.
The equation of the red line
(in the transformed coordinate system) would be $x' = d.$
It is my understanding that for such an arbitrary point $(x,y)$,
with transformed coordinates $(x',y')$,
you want to know first of all whether $x' \leq d$.
If $x' \leq d$, you then want to know the value of $|y'|$,
which is the perpendicular distance from $(x,y)$ to the line $L$
given by $Ax+By+C=0$.
If $x' > d$, then $x'$ is on the "wrong" side of the red line
and you are not interested in its distance from the line $L$.
The transformed coordinates are found by the equations
$$x' = \frac{\varepsilon (B(x - x_0) - A(y - y_0))}{\sqrt{A^2 + B^2}}$$
$$y' = \frac{\varepsilon (A(x - x_0) + B(y - y_0))}{\sqrt{A^2 + B^2}}$$
where $\varepsilon$ is a constant and $\varepsilon=1$ or $\varepsilon=-1$
depending on which of those two values produces the desired sign of $x'$.
For a point at coordinates $(x,y)$, compute $x'$ according to the
equation above; then, if $x'\leq d$, compute $|y'|$.
The reason for $\varepsilon$ is so that the red line will be in the
desired direction from $(x_0,y_0)$: one value of $\varepsilon$ will
cause the line $x'=d$ to be to the right of $(x_0,y_0)$
and the other value of $\varepsilon$ will cause the line to be to the left.
Which value of $\varepsilon$ puts the line on which side depends on the
signs of $A$ and $B$.
You can figure out which value of $\varepsilon$ to use by trial and error:
set a value of $\varepsilon$,
choose a point $(x,y)$ that is obviously on the "wrong" side of the
red line, and compute that point's $x'$ coordinate.
If $x' > d$, you have set $\varepsilon$ to the correct value;
otherwise you should reverse the sign of $\varepsilon$.
Note that because taking the absolute value cancels the sign of $y'$,
and because we assumed that $Ax_0 + By_0 + C = 0,$
the formula for $|y'|$ using the equation above is equal to the
usual formula for finding the distance of a point from a line
given by $Ax + By + C = 0$,
$$|y'| = \frac{|Ax + By + C|}{\sqrt{A^2 + B^2}}.$$
