views:

1687

answers:

5

Okay, so I need to make C go the shortest path from A to B. A to B is the hypotenuse of my right triangle, and I need to give C the arctan of said triangle. How do I do this, and does the formula have a name?

+1  A: 

Arctan would result in degrees or radians so your A and B most likely have coordinates like (x, y). Then you do arctan((By - Ay) / (Bx - Ax)) if I remember correctly, here Bx is the x coordinate of B etc.

If A and B do not have coordinates, you cannot get degrees out meaningfully.

antti.huima
A simple problem if you think about it in terms of vectors.
duffymo
Strangly I had to use (Bx - Ax, -(By - Ay)) but it still worked. Thanks!
William
@William...the fact that you had to reverse the signs...that's what I was trying to point out in my edit of my answer. Guess I should have made that a bit more clear.
Beska
Some libraries have arctan that takes two parameters, some others only one. If you have 1-parameter version, you must handle the case Bx==Ax separately in order to avoid division by zero. It seems that you have rotated the coordinates by 90 degrees, though.
antti.huima
+3  A: 

It's not clear exactly what you're asking, but I think you're trying to find the angle of the A-B line. I'm going to make the assumption that you know, or can figure out the (x,y) coordinates of both A and B, because otherwise you won't be able to solve the issue.

It sounds like you've outlined the majority of the solution...the angle will be equal to the arctan of the (y/x) distance. So if we consider A(y) to be the y coordinate of A, then you're looking at something like:

arctan ((A(y) - B(y)) / (A(x) - B(x)))

Does that help? Or are you looking for something slightly different?

EDIT: One thing to be aware of is the order in which you consider the terms (whether you're going from A to B or vice versa), etc. You will have to be thoughtful about this or you could end up with some sign problems.

Beska
A: 

If A to B is the hypotenuse of your right triangle, A to B will also be the shortest path from A to B because it is a straight line between the points.

You can calculate the arctangent of either non-right angle by dividing the length of the adjacent side by the length of the opposite side because it's the inverse of the tangent. But, with the information you've described, you will be lacking either the numerator or the denominator.

There are an infinite number of right triangles with a hypotenuse of a given length.

Jekke
+1  A: 

If you only have one length and there is no hidden assumption here (like say, one side of the triangle has been normalized): you can't.

An interesting hidden assumption might be:

  • All distances are integers
  • The triangle is at least as long as it is tall.

Then the problem is merely hard.


If A and B are points, then the angle you want is presumable the one taken to the x-axis, and you get it by (using the fortranish names):

atan((B.y - A.y)/(B.x - A.x))

or if you have it in your library

atan2((B.y - A.y),(B.x - A.x))

which handles the divide by zero cases neatly...

dmckee
Um, I see what you're trying to do with the formula, but it's a bit weird. arctan is a function...it doesn't equal anything. (Judging by your post, you understand this.)
Beska
Um....D'oh? Thanks.
dmckee
and atan2 resolves the +-PI ambiguity.
Mike Dunlavey
And atan2 puts the result in the correct quadrant.
Nosredna
+1  A: 

Most systems have Arctan2(dy, dx) which gives you an angle in a full circle (and takes care of verticals), so you would say Arctan2((By - Ay), (Bx - Ax)) to get the direction in radians (counterclockwise from East). For degrees multiply by 360/(2*PI).

Just make sure A != B.

Mike Dunlavey