Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Slope of a perpendicular line  (Read 395 times) 0 Members and 1 Guest are viewing this topic.
saucymeatman
 « Posted 2013-12-10 21:25:47 »

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!
phu004

JGO Knight

Medals: 12
Projects: 10
Exp: 10 years

NoSuchPersonException

 « Reply #1 - Posted 2013-12-10 22:08:00 »

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

kramin42
 « Reply #2 - Posted 2013-12-11 02:46:31 »

perpLineA.slope =  -1/lineA.slope

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala