vector class

High im making a vector class and i need to be able to convert from rectangular components to polar components and back again. The issue im having is that when i convert from polar to rectangular the numbers are not correct.

Here is the code for my polar to rectangular calculations
---------------------------------------------------------

//proper headers and such

void Vect :: calcX()
{
x=((mag)*cos(angl));
}

void Vect :: calcY()
{
y=mag*sin(angl);
}

--------------------------------------------------------
Heres a link to the output
--------------------------------------------------------
http://i556.photobucket.com/albums/ss2/otismp/vectRounding.jpg

cos() or sin() accept radians. Is your angl in degrees?
Well i was entering them as degrees. I just updated it to this

y=mag*sin(angl*180/3.14159265);

ran the code and i ended up with

http://i556.photobucket.com/albums/ss2/otismp/vectRounding-1.jpg

the problem here is i dont really know how to do the math im just using the functions and formulas i find online.
closed account (o1vk4iN6)
 
y=mag*sin( angl * (3.14159265 / 180) );


I don't see why you just work in radians, much easier.
xerzi im not sure what you mean?
The variable angl is entered as degrees by the user.
The formula to convert degrees to radians is: radians=angl*(180/pi)

so im converting a degree value into radians for the cos/sin math function then im calling
ither sin or cosin and multiplying by the magnitude to find an x or y value.

The problem is that when i enter (2,2) in rectangular mode i get 2.82843 ∠45

now if i set mag=2.82843 and angl=45, y=mag*sin(angl*180/3.14159265);
it should result in x=2 , y=2 but it does not and i dont know why.
closed account (o1vk4iN6)
The formula to convert degrees to radians is: radians=angl*(180/pi)


That is not the formula to convert degrees to radians, it's to convert radians to degrees which is why you aren't getting the values you want.

PI radians == 180 degrees, if you want to get the number of radians per degree:

Radians / Degree

so

Degree * (Radians / Degree)

Degree cancels out and you are left with radians



What you are calculating is this:

Degree * (Degree / Radians)

Which is:

Degree^2 / Radians

So therefore you are not actually inputting radians into your sin and cos functions which is why you don't get the result you want and which is why I posted this:

y=mag*sin( angl * (3.14159265 / 180) );


If you take in a value as a degree you should only convert it once and store it in radians, not store it in degrees than each time you need to use the value convert it to radians. Just a matter of opinion I guess, it's up to you though.
Last edited on
You just helped me so much you dont even know. This is what happens when you try to do math and dont know what your doing haha. Thanks so much : D
Topic archived. No new replies allowed.