[Math] Calculating an angle adjacent to hypotenuse given two points

geometrytrianglestrigonometry

I'm working on a chapter in my book dealing with touch input, and my memory of high school trig (from circa 1988) is failing me. My search here has not yielded anything that I'm capable of applying to this particular problem.

Description

  • I have a ship on the screen. If the user touches the screen, I need
    to turn the ship to align with that point (but over several steps, so
    I'd do it in increments). To do this, I need to calculate the angle shown in the diagram below.

Knowns

  • The ship's current X and Y location.
  • The touch point's X and Y location

Can potentially calculate in case it helps

  • Arbitrary points along the line starting at the current location and going out to the current heading, given a distance.

  • Length of hypotenuse (I know the start and end points)

What I need to calculate

  • The Angle between the imaginary line which represents the current heading, and the touch point, with the vertex at the current location. This image shows what I'm looking for:

Need to solve for this angle

I've been searching all day for anything which will (ahem) trigger a memory as to how to solve this, but I've been hitting nothing useful. Trig was never a strong skill for me in any case.

Sin/Cos/Tan functions need more data than I currently have. I thought about doing something with line intersection to get the length of the opposite side so I could use the Sin function, but I couldn't figure out how to calculate the line perpendicular to the heading and passing through the known touch point.

I'll take anything which works, but I'm doing this calculation frequently, so efficiency (for things which can be represented in code) is a plus.

Thanks for your time here.

Best Answer

My answer is purely from a developer perspective (no mathematician here):

First find the difference between the end point and the start point:

var deltaX = touchPoint_x - shipCurrent_x;
var deltaY = touchPoint_y - shipCurrent_y;

Then get the angle in degrees:
var angle = Math.atan2(deltaY, deltaX) * 180 / PI;

I'm using javascript with CSS3 transform rotate, so I can rotate the "ship" to the desired angle.

You can check it out at: http://jsfiddle.net/dotnetricardo/W2VCu/17/

Hope it helps also! :)