Slope of a perpendicular line

Hello!

I need to calculate the slope of a perpendicular line, which I can do on a piece of paper :
LineA’s slope = 1/3
PerpendicularToLineA’s slope = -3/1
(-Run/Rise)

But how do I do this programatically?
lineA.slope = 2;
perpLineA.slope = ???; (It would equal -1/2 in this case)

I cant find an actual formula anywhere and my line/linesegment classes do not contain rise’s/run’s only a numerical slope.

Any help is greatly appreciated!

If LineA’s slope is x1/y1, and the slope for the line that is perpendicular to A is x2/y2,

Then it must satisfy the following:

x1 * x2 + y1 * y2 = 0 //the dot product of 2 vectors (in any dimensions) that are perpendicular to each other is always zero

=>

x1 * x2 = -y1*y2

=>

x2/y2 = -y1/x1

Thus if the slop for line A is 2 (i.e x1/y1 = 2), the slop for the perpendicular line will be -1/2

perpLineA.slope = -1/lineA.slope

Don’t use slope…use direction.