Drawing a perpendicular line from any angle I'm trying to draw a line perpendicular to a line from any angle. I found following solution: https://math.stackexchange.com/a/1107295/303637

The slope between the given points is $$ m = \frac{3 - 17}{11 - 4} =
> -2 $$ The slope of the perpendicular line is $$ m' = -\frac{1}{m} = \frac 12 $$ Define  $$ \Delta x = \cos(\arctan(m')) =
> \frac{1}{\sqrt{(m')^2 + 1}} = \frac{2}{\sqrt{5}}\\ \Delta y =
> \sin(\arctan(m')) = \frac{m'}{\sqrt{(m')^2 + 1}} = \frac{1}{\sqrt{5}}
> $$ The coordinates of the points you want is given by $$ (x_1,y_1) =
 (11 - 5 \Delta x, 3 - 5 \Delta y)\\ (x_2,y_2) = (11 + 5 \Delta x, 3 +
> 5 \Delta y) $$

It seems like that answer solved the thread's author's problem, but it did not solve mine because it is unable to draw a perpendicular line from all angles. Let's take the following points for example: 
$$
(x_1 = 0,y_1 = 1), 
(x_2 = 5,y_2 = 1)
$$
Following the quoted solution will simply return the current position of point one and will not draw a line at all. This happens because a zero enters the solution, thus we end up with an arctangent of zero radians.
How could I approach drawing a solid perpendicular line from any position, including positions where both X or Y values are similar?
 A: This method should work for all other cases, except when $m=0$, i.e., $y_1=y_2$. In this case, you should get
$$(x_2, y_2+\frac{1}{2}L), (x_2,y_2-\frac{1}{2}L)$$
There is a method that works for all cases though. Use the rotation matrix
$$\begin{pmatrix}0&-1\\1&0\end{pmatrix}\begin{pmatrix}x_2-x_1\\y_2-y_1\end{pmatrix}$$ which give you the perpendicular vector. In your example,
$$\begin{pmatrix}0&-1\\1&0\end{pmatrix}\begin{pmatrix}5\\0\end{pmatrix}=\begin{pmatrix}0\\5\end{pmatrix}$$ 
Find the unit vector in this direction, which is 
$$\begin{pmatrix}0\\1\end{pmatrix}$$
Add or subtract $\frac{1}{2}L$ of this unit vector from $(x_2,y_2)$. 
$$(5,1)+\frac{1}{2}L(0,1)=(5,1+\frac{1}{2}L), (5,1)-\frac{1}{2}L(0,1)=(5,1-\frac{1}{2}L)$$
This works for all cases.
For your example $(1,3)$ and $(5,1)$:

The original line is rotated counterclockwisely by $90$ degrees. When you add the original line with the rotated line, you get the end point of the dashed line, together with the length adjustment, we get the right point. With a negative sign, we get the other. 
