I'm getting some strange behavior using COS() and SIN() libraries in Fortran 77. It picks what number to perform it's operation on like here :
DATA V , THETA , PI / 100 , 45 , 3.1416 /
THETA = THETA * PI/ 180.0
PRINT *, "THETA = " , THETA
VX = V * COS ( THETA )
VY = V * SIN ( THETA )
PRINT *, VX , VY
END
when i check it, i find that
COS(THETA) = COS(45)
not
COS(0.7853)
and the same goes to SIN() too. So why does this happen ?
This is my output :
THETA = 0.785400
70.7105 70.7108
Those are the correct results. You're taking the cos of 45 degrees (converted to radians) and multiplying it by 100, and the cos of 45 degrees (or pi/4 radians) is 0.7071067, and so the expected value to print out is 70.7106.
SIN and COS functions take the arguments in radians. You use the correct formula to convert the input values from degrees to radians, and it's giving you the correct results for those converted values.
I'm curious how you could do the right conversion, and not realize that you did the right thing and got the right answer. Did you take this code from somewhere else?
Related
For example I have a value 0.70, and it is a sin() for 45 degrees. I need a function, that will calculate me an angle from a sin. What function from C++ can help me?
You can use std::asin. This returns a value between [-PI/2,+PI/2], to get degrees you multiply by 180 and divide by PI.
result = std::asin(value) * 180 / PI
PI = 3.1415926535
Is there any code in C++ to converts from Cartesian (x,y,z) to Cylindrical (ρ,θ,z) coordinates in 2-dimensions and 3-dimensions!!
Thanks
If you are asking about a standard library function that will do this conversion for you, I do not believe there are any. However, there are some simple equations that relate the two. These equations are:
x = p cos ( theta )
y = p sin ( theta )
z = z
where
p = sqrt( x^2 + y^2 )
Standard C++ has the sin and cos functions. It also has asin and acos for the arc sin and the arc cos functions. These functions work in radians. It should be simple for you to write your own code to do this.
I'm building a small Physics engine and I'm having trouble converting my Radian value to Degrees using atan, as I need an angle to output in Degrees only.
Firstly, I have an x and y value, and I need to find an angle using atan, so I divide y by x like so:
angleDivide = yN / xN;
Then, before putting this value into tan, I attempt to convert it to Degrees like this:
angleToDegrees = angleDivide * (3.14 / 180);
Then I place angleToDegrees into atan:
angle = atan(angleToDegrees);
But when I'm displaying angle, I'm, still getting radian values.
Please could you tell me what is wrong with my code and how to fix this?
You want to calculate radians=tan(y/x) first.
Then you can convert it to degrees:
radians = atan(y/x)
degrees = radians * (180.0/3.141592653589793238463)
See the reference here for atan:
On a side note, you also have to take into account what quadrant you are in to get the correct answer (since -y/x is the same number as y/-x)
I must be the worst person on the planet when it comes to math because i can't figure out how to change this circle radius:
from math import *
posx, posy = 0,0
sides = 32
glBegin(GL_POLYGON)
for i in range(100):
cosine=cos(i*2*pi/sides)+posx
sine=sin(i*2*pi/sides)+posy
glVertex2f(cosine,sine)
I'm not entirely sure how or why this becomes a circle because the *2 confuses me a bit.
Note that this is done in Pyglet under Python2.6 calling OpenGL libraries.
Followed Example 4-1: http://fly.cc.fer.hr/~unreal/theredbook/chapter04.html
Clarification: This works, i'm interested in why and how to modify the radius.
This should do the trick :)
from math import *
posx, posy = 0,0
sides = 32
radius = 1
glBegin(GL_POLYGON)
for i in range(100):
cosine= radius * cos(i*2*pi/sides) + posx
sine = radius * sin(i*2*pi/sides) + posy
glVertex2f(cosine,sine)
But I would pick another names for variables. cosine and sine is not exactly what these variables are.
And as far as I see, you son't need a loop from 1 to 100 (or from 0 to 99, I'm not too good at Python), you just need a loop from 1 to sides.
Explanation:
When you calculate
x = cos (angle)
y = sin(angle)
you get a point on a circle with radius = 1, and centre in the point (0; 0) (because sin^2(angle) + cos^2(angle) = 1).
If you want to change a radius to R, you simply multiply cos and sin by R.
x = R * cos (angle)
y = R * sin(angle)
If you want to transfer the circle to another location (for example, you want the circle to have it's centre at (X_centre, Y_centre), you add X_centre and Y_xentre to x and y accordingly:
x = R * cos (angle) + X_centre
y = R * sin(angle) + Y_centre
When you need to loop through N points (in your case N = sides) on your circle, you should change the angle on each iteration. All those angles should be equal and their sum should be 2 * pi. So each angle should be equal to 2 * pi/ N. And to get i-th angle you multiply this value by i: i * 2 * pi / N.
math : P=pr^2=p*r*r= p*r*2 programming i*2*pi/sides
together : i = p i*2, *2=r^2 this should help you
Hey so i'm doing some graphics math and inserting what i believe to be a float (i'm pretty sure the ways i manipulate the float beforehand is messing something up somehow...) into the function and getting strange, negative results back.
for instance when doing the following opperations when Angle initially equals 350.0, test ends up being
-.99. Why?
Angle= (float)(Angle-(int)Angle)+(float)((int)Angle%90);
// calculates x and y based on angle and Hypotenuse
float test= sin(Angle);
float test2= 1/(Speed*Time);
float test3= test/test2;
buffX= sin(Angle)/ (1.f/(Speed*Time));
buffY= sin(Angle-90)/ (1.f/(Speed*Time));
trying to keep Angle a float by putting (float) before everything didn't work... please help! Thanks!
That's because the C/C++ runtime function sin() expects the argument in radians, not degrees.
Convert to radians with:
float test= sin(Angle / 180 * M_PI);
etc.
sin takes its arguments in radians, not degrees. You need to take you number and multiply it pi/180