I am trying to understand trigonometry and the short answer is that I do not.
I drew a little triangle to mess around with and I asked myself the question, "If I know the length of the hypotenuse and the angle, how do I find the length of the other edges?".
Then I started reading. Apparently, the sine of angle A is supposed to equal the length of the opposite side divided by the length of the hypotenuse. So I figured that, using a right triangle, multiplying the length of the hypotenuse by the sine of the angle would yield the length of the opposing side.
1.414 / 1 = .707blahblah * 1.414 = 1 on my calculator.
But in every programming language I try sin(45.0) equals .8somethingsomething. I tried c++, c#, java, php, and lua.
Is the input not being interpreted as degrees? What unit is being used and how do I convert it? I've been seeing the word Radians, it would be helpful if someone could explain what a Radian is.
Radians are units of angular measure, like degrees, except that while there are 360 degrees in a circle, there are 2*pi (about 6.28) radians in a circle. You can convert degrees to radians by multiplying by pi (3.14159) and dividing by 180.
The formula works if the triangle is a right triangle, and yes, most programming languages expect radians rather than degrees as arguments to functions like sin() and cos().
Regarding the argument in the comments below: if you fix angle <BAC, side AB, and side BC, you can see that there are two possible positions for point C which preserve the the length D2 for side BC. Therefore <BAC, D1, and D2 do not fully determine a triangle.
The input to sin functions generally is expected in radians, not degrees. For example, in the Java documentation for sin it's stated that:
Parameters:
a - an angle, in radians.
Convert the angle in degrees to radians first, by multiplying it by pi/180
A radian is the distance of the radius of a circle along its circumference. Since a circle's circumference is 2 times pi times its radius, there are 2 times pi radians in one complete circle.
Yes, you are correct. Those functions all take their input in radians, not degrees.
You can convert degrees to radians by multiplying the degrees by π/180.
Convert to radians: Radian = degree/180*Pi
In order to convert from degrees to radians, divide the number in degrees by 180 and multiply by pi.
Related
I am making a simple birds-eye 2D game where the character can face any direction, the direction the character is facing will be given in radians.
Given two sets of coordinates (point a and b) how do I find the angle between the line directly vertical from a and the line produced from a to b? This angle will be from 0 to 360 (although 360 will be treated as 0).
http://i.stack.imgur.com/J9TAU.png
In this diagram point a is the centre and point b is the one on the edge. The line extending from a is the 0 position and the line which you will work out the angle from. The point b could be anywhere on the circle and I need to find the radians of the angle to the right of the 0 line.
It's been a while since I studied it back in school, but if point B is on the unit circle, it should be a pretty trivial math problem.
A - If it's in radians, it won't be 0-360!
B - If Y >= 0, angle = arccos(x); else angle = TWOPI - arccos(x)
The usual formulation is that angle=0 is along +X, rather than +Y. You'll have to tinker a bit to get what you want. But that should be plenty to nudge you in the right direction.
You can use the atan function that many programming languages have. From vertical, the angle to point B will be:
pi/2 - atan(x, y)
Where x and y are the coordinates of point B with respect to A. Note that this might be negative, so you'll need to do some modular arithmetic to get it positive (if you care about that.)
Also, this isn't really an appropriate question for StackOverflow, since it's a math question and not a programming one.
Who is a math wiz and wants to check my math?
I'm trying to find the placement of a point(x,y) on the circumference of a circle in degrees. If I use x=radius*cos(Q)+xOrigin, I end up with arccos((x/2)-xOrigin) = Q which gives me the angle in radians. Then I go D = Q(180/Pi) to get degrees, but I keep coming up about 3-5 degrees off of target.
What's up with that?
Your formula for Q should be
Q = acos((x-xOrigin)/radius)
Note that acos returns an angle between 0 and pi; if the angle is in the third or fourth quadrant you'll need to subtract the angle from 2 * pi.
Without knowing the actual values that are coming up wrong, it's hard to tell if that's the only problem. Your conversion from radians to degrees is correct.
Most modern programming languages have atan2() somewhere, which takes a rise and a run and spits out the angle in radians in all four quadrants.
I have two 3D vectors called A and B that both only have a 3D position. I know how to find the angle along the unit circle ranging from 0-360 degrees with the atan2 function by doing:
EDIT: (my atan2 function made no sense, now it should find the "y-angle" between 2 vectors):
toDegrees(atan2(A.x-B.x,A.z-B.z))+180
But that gives me the Y angle between the 2 vectors.
I need to find the X angle between them. It has to do with using the x, y and z position values. Not the x and z only, because that gives the Y angle between the two vectors.
I need the X angle, I know it sounds vague but I don't know how to explain. Maybe for example you have a camera in 3D space, if you look up or down than you rotate the x-axis. But now I need to get the "up/down" angle between the 2 vectors. If I rotate that 3D camera along the y-axis, the x-axis doens't change. So with the 2 vectors, no matter what the "y-angle" is between them, the x-angle between the 2 vectors wil stay the same if y-angle changes because it's the "up/down" angle, like in the camara.
Please help? I just need a line of math/pseudocode, or explanation. :)
atan2(crossproduct.length,scalarproduct)
The reason for using atan2 instead of arccos or arcsin is accuracy. arccos behaves very badly close to 0 degrees. Small computation errors in argument will lead to disproportionally big errors in result. arcsin has same problem close to 90 degrees.
Computing the altitude angle
OK, it might be I finally understood your comment below about the result being independent of the y angle, and about how it relates to the two vectors. It seems you are not really interested in two vectors and the angle between these two, but instead you're interested in the difference vector and the angle that one forms against the horizontal plane. In a horizontal coordinate system (often used in astronomy), that angle would be called “altitude” or “elevation”, as opposed to the “azimuth” you compute with the formula in your (edited) question. “altitude” closely relates to the “tilt” of your camera, whereas “azimuth” relates to “panning”.
We still have a 2D problem. One coordinate of the 2D vector is the y coordinate of the difference vector. The other coordinate is the length of the vector after projecting it on the horizontal plane, i.e. sqrt(x*x + z*z). The final solution would be
x = A.x - B.x
y = A.y - B.y
z = A.z - B.z
alt = toDegrees(atan2(y, sqrt(x*x + z*z)))
az = toDegrees(atan2(-x, -z))
The order (A - B as opposed to B - A) was chosen such that “A above B” yields a positive y and therefore a positive altitude, in accordance with your comment below. The minus signs in the azimuth computation above should replace the + 180 in the code from your question, except that the range now is [-180, 180] instead of your [0, 360]. Just to give you an alternative, choose whichever you prefer. In effect you compute the azimuth of B - A either way. The fact that you use a different order for these two angles might be somewhat confusing, so think about whether this really is what you want, or whether you want to reverse the sign of the altitude or change the azimuth by 180°.
Orthogonal projection
For reference, I'll include my original answer below, for those who are actually looking for the angle of rotation around some fixed x axis, the way the original question suggested.
If this x angle you mention in your question is indeed the angle of rotation around the x axis, as the camera example suggests, then you might want to think about it this way: set the x coordinate to zero, and you will end up with 2D vectors in the y-z plane. You can think of this as an orthogonal projection onto said plain. Now you are back to a 2D problem and can tackle it there.
Personally I'd simply call atan2 twice, once for each vector, and subtract the resulting angles:
toDegrees(atan2(A.z, A.y) - atan2(B.z, B.y))
The x=0 is implicit in the above formula simply because I only operate on y and z.
I haven't fully understood the logic behind your single atan2 call yet, but the fact that I have to think about it this long indicates that I wouldn't want to maintain it, at least not without a good explanatory comment.
I hope I understood your question correctly, and this is the thing you're looking for.
Just like 2D Vectors , you calculate their angle by solving cos of their Dot Product
You don't need atan, you always go for the dot product since its a fundamental operation of vectors and then use acos to get the angle.
double angleInDegrees = acos ( cos(theta) ) * 180.0 / PI;
I noticed that translating radians to degrees and vice versa is like translating a percentage to a whole number and vice versa. For example, to get 60 percent of 345 you do the following
60 * 345/100
to convert 60 degrees to radians you do
60 * 3.14/180
There is a pattern there BUT... we use 100 to compare percentages to a number. So, why do we use 180 degrees instead of 360 degrees to compare degrees to radians?
%100 percent = a whole number
360 degrees represents a whole circle
using 180 degrees is like using 50% instead of 100%
I hope I am making some sense. Can anyone answer? Thanks
The reason you use 180 degrees instead of 360 is that there are 2*pi radians in a circle, not pi. Thus you divide both 360 and 2*pi by 2 and get pi and 180.
In Mathematica, I use the handy predefined Degree constant for conversions, which is defined as Pi/180 or 2 * Pi/360.
The reason there are 2 * Pi radians in a circle is that the size of an angle in radians is the length of the arc of a circle with radius 1 that subtends it. The circumference of a circle with radius 1 is 2 * Pi. In addition to providing a clear geometrical interpretation, using radians also makes a number of other relations much more convenient; cosine is the derivative of sine, and as a result the Maclaurin series for sines and cosines are much simpler than they would be for angles expressed in degrees.
360 degrees = 2 * Pi radians
1 degree = Pi / 180 radians
I guess your question is, why there 360 degrees in a circle (or 180 in a semicircle), and why not some other more tenable number like 100.
The answer to that is the origin of degree. If you'd like to use a round figure, check out the gradian unit of angles.
PS: SO is for programming questions only. This is not programming related.
I ask this question because my lack of paying attention in school. Programming actually is the reason I ask this question because it is now that I am actually paying attention. Every programming formula uses 180 and PI to translate back and forth instead of 360. Since I haven't came across any examples, I assumed that there was only one way. Of course if I was reading a regular math book, I would of known differently.
But I understand now. Actionscript uses 180 degrees for clock wise rotation. once 180 is reached, it uses -180 back down to 0 for a full rotation. Which makes alot more sense if you want your answer to fall in the 180 degree range. and depending on if its negative or positive determines whether or not it is traveling up on the x axis or down and y axis as well. As much as I appreciate the responses, I believe this is absolutely a suitable programming question. For programmers calculating in degrees is different from your average surveyor.
Given a real life scenario, measuring a distance is always considered a absolute value, where programming this is false. which also rationalizes why we use -180 degrees.
Math escapes me today.
How do I find the X speed and the Y speed of an object if it is going at a defined speed (say, 5 pixels/second) at a 45 degree angle?
So always 5 pixels/sec and always 45 degrees?
The general case is
velx=cos(a)*vel;
vely=sin(a)*vel;
a is angle, usually in radians, so convert from degrees, and the signs (positive/negative) will depend on your coordinate system.
Crazy fact from the 1980s: In the old days, we used lookup tables for sin and cos!
Edited: Made my axes more conventional thanks to comment below. x is positive to your right. y is positive up. 45 degrees is to the northeast. If you have something else, let me know.
It will be
Vx=VCos#
Vy=Vsin#
So in your case it will be Vx=5*cos45 and Vy=5*sin45
At 45 angle value of Cos & Sin is same i.e 1/root 2.
Note: If you are doing any math stuff in programming then have a look at Vecmath lib.
At a 45 degree angle, an object is going sqrt(2)/2 of the speed along each axis. Generally, you can do it with sin and cosine, but for specific angles like this you can do it just by knowing pythagorean triangles.
In a right triangle, the square of the hypotenuse is equal to the sum of the squares of the other two sides. You know the hypotenuse is V. You also know that the other two sides equal each other. That means that V^2 = Vx^2 * 2. This means that Vx = sqrt(V^2/2), which equals V * sqrt(1/2).