Fog Creek Software
Discussion Board

Math wizards - look in here!

I'm a self admitted Math 'tard (TM)

I have 2 points with x,y coords.  How can I find the slope of the line formed by the 2 points in degrees ?

i.e. the line formed by 2 points could be a 45 degree angle, or a 40 degree angle , etc. But I only know the x,y coords, how do I calculate the degrees ?

Captain Offshore
Thursday, April 29, 2004


I forget how, though.

Thursday, April 29, 2004

Has this been offshored to you Captain Offshore?

Matthew Lock
Thursday, April 29, 2004

First find the slope of the line.  Then the angle that the line makes with the x-axis (you weren't clear about what angle you wanted, but I think you probably meant the angle with the x-axis) is the inverse tangent of the slope.  If your two points are (x1,y1) and (x2-y2), then the angle is given by


Thursday, April 29, 2004

Kent - Thanks!

Matthew - No, I'm an American citizen working in Orlando FL.  The great offshoring debate is one of my favorite topics.

Captain Offshore
Thursday, April 29, 2004

The line L is defined by two points (x1, y1), (x2, y2).

The slope of L is (y2 - y1) / (x2 - x1)

The length, |L|, is sqrt((x2 - x1)**2 + (y2 - y1)**2)

If you interpret L to be the hypotenuse of a right triangle whose right angle is 'below' L, the lower angle 't' (what I assume you mean by slope angle) is:

cos(t) = A / |L|
cos(t) = (x2 - x1) / |L|
cos(t) = (x2 - x1) / ((x2 - x1)**2 + (y2 - y1)**2)
t = acos((x2 - x1) / ((x2 - x1)**2 + (y2 - y1)**2))

That'll be in radians, so if you want it in degrees just multiply 't' by 180 / pi.

Thursday, April 29, 2004

There I went and took too long to post while you got an answer.  Go ahead and use the atan method, it's cheaper.  Just remember SOHCAHTOA for the trig functions (Sine: Opposite / Hypotenuse, Cosine: Adjacent / Hypotenuse, Tangent: Opposite / Adjacent).

Thursday, April 29, 2004

Kalani , Thanks for your input - I appreciate your detailed response.

Captain Offshore
Thursday, April 29, 2004

If I remember correctly (ha -- trig was just last term, and already I've forgotten this), the arctan function is only defined for half of the unit it may not give accurate results for all sets of two points.

Anybody else want to back me up (or smack me down) on this?  I'm too lazy to go get my book from the office... (=

Sam Livingston-Gray
Thursday, April 29, 2004

The basic problem with arctan is that it returns a value in the range -90 to +90, and can't distiguish between angles 180 degrees apart - eg if y2-y1 = -1, and x2-x1=-1, it will return a value of 45 degrees instead of the true answer of 235. Also you have to be careful if x1=x2, when the division will fail.

In C, a better solution is the library function atan2(y2-y1, x2-x1), although this returns a value in radians between -pi & pi, which you'll need to convert to degrees by multiplying by 180/pi.

Thursday, April 29, 2004


injun chief
Friday, April 30, 2004

The arctan function is the inverse of the tan function.

  tan x = sin x / cos x

tan is a periodic function with a period of of Pi

Math Geek
Friday, April 30, 2004

If you're coding in C, use the atan2 function, not atan. It does most of the hard work for you.

Tom Payne
Friday, April 30, 2004

Use atan, just be careful in the case that x1 == x2 (you'll end up dividing by zero, you naughty boy).

Friday, April 30, 2004

*  Recent Topics

*  Fog Creek Home