Finding the length of the opposite and adjacent sides of a triangle

741 Views Asked by At

I am writing a small game in javascript. It's been a while since I have done any basic maths and I can't get some of my positioning to work properly. Apologies if this question is too simple, but I can't spot my error.

I have the following code

var xPos = 0, yPos = 0, angle = 45, distance = 1;

var yDelta = Math.sin(angle) * distance,
    xDelta = Math.sqrt((distance * distance) -(yDelta * yDelta));
yPos += yDelta;
xPos += xDelta;
  • Starting at coordinate (0,0)
  • Move 1 step at 45 degrees
  • To get the new coordinates I need to get the distance moved up
    • As sin(theta) = opposite/hypotenuse this should be sin(theta) * hypotenuse
  • And the distance moved across
    • As this is a right angled triangle this should mean hypotenuse squared - opposite (that we calculated in the previous step) squared.

This gives me (0.5253, 0.8509), but this calculator tells me that it should be 0.7071 for each side. Can anyone explain what's wrong with my reasoning?

2

There are 2 best solutions below

0
On BEST ANSWER

put angle in radians, $sin(45) = sin(\pi /4) = 0.707$ (y coordinate)

$cos(\pi /4) = 0.707$ (x coordinate)

const $ \pi = 3.1415926$

0
On

The calculator is right. I guess the problem is that your script thinks $45$ is an angle in radians, where it is in degrees. To solve that problem, multiply by $\frac{2\pi}{360}$ to get the angle in radians.