2

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?

Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
William
  • 8,630
  • 23
  • 77
  • 110
  • I'm not quite sure I understand the question. What information are you given to work with? Just the points A and B, or something else? – e.James Feb 24 '09 at 19:59
  • If you had one of the angles or one of the sides, you'd get to use more trig or Pythagoras. But knowing only the length of the hypotenuse doesn't tell you much. – Jon W Feb 24 '09 at 20:00

5 Answers5

3

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
  • 12,445
  • 14
  • 77
  • 112
2

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
  • 40,059
  • 14
  • 91
  • 135
1

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
  • 25,136
  • 3
  • 52
  • 71
  • A simple problem if you think about it in terms of vectors. – duffymo Feb 24 '09 at 20:05
  • Strangly I had to use (Bx - Ax, -(By - Ay)) but it still worked. Thanks! – William Feb 24 '09 at 20:19
  • @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 Feb 24 '09 at 20:46
  • 1
    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 Feb 24 '09 at 22:00
1

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • 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 Feb 24 '09 at 20:11
0

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.

Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96