The easy way around this one is not to think in terms of slope m, but rather the change in x and y, which I call dx, dy (from the calculus notation).
The reason is for one thing, that dealing with a slope for a vertical line is infinite, and in any case, you don't need to use trig functions, this code will be faster and simpler.
dx = x2 - x1;
dy = y2 - y1;
I am assuming here that point 2 is the intersection of the desired line.
Ok, so the perpendicular line has a slope with the negative reciprocal of the first.
There are two ways to do that:
dx2 = -dy
dy2 = dx
or
dx2 = dy
dy2 = -dx
this corresponds to the two directions, one turning right, and the other left.
However, dx and dy are scaled to the length of the original line segment. Your perpendicular has a different length.
Here's the length between two points:
double length(double x1, double y1, double x2, double y2) {
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
}
Do what you want, to go to one side or the other, is:
double scale = length(whatever length you want to go)/sqrt(dx*dx+dy*dy);
double dx2 = -dy * scale;
double dy2 = dx * scale
and then the same again for the other side.
I just realized my example is somewhat c++, since I used sqrt, but the differences are trivial. Note that you can write the code more efficiently, combining the square roots.