Is there a way to find the perpendicular of parallel lines in standard form without computing slope I'm trying to write a program to convert a line segment into a rectangle of a given thickness centered on that line (for computer graphics)
To do that I need to find the perpendiculars of the line segment that go through its endpoints.
I'd like to use a universal approach that works in all cases, including vertical and horizontal lines.
The standard form for a line, ax + by = c, works for any line, including horizontal and vertical lines.
Slope-intercept and point-slope form equations don't, because for vertical lines, there is a divide-by-zero. You have to write special case x = value forms.
I'd like to be able to feed a line equation in standard form in as input, plus a point, and output a perpendicular line equation in standard form, without special case "if the line is vertical or horizontal, do something different" logic. (More specifically, I'd like to be able to take 2 endpoints of a line segment and compute the endpoints of the 2 "end cap" line segments that let me convert the source line segment into a rectangle of a given thickness centered on the source line segment.)
(Eventually I'd like to adapt the algorithm I'm designing to SIMD for GPU processing. SIMD requires that all the computations follow the exact same path without branching based on input values.)
 A: If you have a vector representing the line orientation and we are working in $2D$, just flip the numbers and change one sign, for example, if:
$$v_1 = [1,2]^T$$
then
$$v_2 = [2,-1]^T$$ will be perpendicular to it.
You can get $v_1$ from the end point coordinates by just subtracting them element wise. For example if the end points are $p_1 = (4,2)$ and $p_2 = (3,0)$ then $v_1 = [4-3,2-0]^T = [1,2]^T$
Now what remains is to use $v_2$ and $p_1$ and $p_2$ to calculate the box corners. We will now need to normalize $v_2$ to make it length $1$. We do this by dividing by the square root of the sum of squares:
$$\hat v_2 = \frac{1}{\sqrt{2^2+(-1)^2}}\cdot[2,-1]^T = \frac{1}{\sqrt{5}}\cdot [2,-1]^T$$
Now you can find the corner points of you box by reversing the points-to-vector procedure we did above.
A: The perpendicular will be of the form $-bx + ay = k$, for any value $k$. You can choose $k$ as needed to make the line pass through a given point.
Alternatively, and probably a better approach, store or calculate the difference vector $(\Delta x, \Delta y)$ between the start and end points. Then the vector pointing in the (counterclockwise) perpendicular direction is $(\Delta y, -\Delta x)$, which you can then normalize.
A: Consider the line $ax+by=c$ in question, and assume it is parameterized by some parameter $t$. The derivative of this equation is
$$a\frac{dx}{dt}+b\frac{dy}{dt}=0$$
The significance here is that the vector $\langle dx/dt,dy/dt\rangle$ is the tangent vector of this line, so if we define $\bf T$ as the tangent vector of the curve then this reads
$$\langle a,b\rangle\cdot{\bf T}(t)=0$$
In other words, the vector $\langle a,b\rangle$ is always perpendicular to the curve. In other words, the perpendicular to this line has a tangent vector of $\langle a,b\rangle$. We could treat this as the tangent vector to our new curve, giving us the equations
$$x^*(t)=at+c_1,\;\;\;\;y^*(t)=bt+c_2$$
We multiply the first equation by $b$, the second equation by $a$, then subtract them to get
$$bx^*-ay^*=c_1-c_2$$
Therefore, given a linear $ax+by=c$, any perpendicular line satisfies $bx-ay=d$, where $d$ is chosen based on the point the line must pass through.
