Advertisement

GLfloat's and math...

Started by March 08, 2001 06:12 PM
4 comments, last by Nibbles 23 years, 8 months ago
Hi, gotta a doosy of a question here (i think) prolly something stupid that i''m doing tho ahem.. ok. I have a object at P1 = (0.0f, 0.0f) and another at P2 = (-2.710859, -0.116912). Now i''m trying to find the angle from P2 to P1. So, what I did is I used Pythagora''s Theorum; c=root(a*a + b*b), to find the length of the hypotenus from P2 to P1, which turn out to be 2.713378. Then I used the Sin Rule; Sin A/a = Sin C/c which returns -.999071. Then i used asin(-.999071) to get the angle which when done on my caclulator returns -87.5 degres which makes sense just negative.. no big deal tho. But when I do this with GLfloat values, and after I convert the GLfloat value to degrees from Rads, I get a degree value of approx. 63 degrees. Now, hoping this all makes sense.. anyone have any idea?? i''ve already ripped out half my hair, anymore and my social life is over! Thanks, Scott
I have a feeling you were doing something wrong with your algorithm. Rather than using the Law of Sines to find the angle, you can just use basic right triangle trig to find the angle. Second, rather than using asinf, which returns -pi/2 to pi/2, you should use acosf because it returns a friendlier 0 to pi. Finally, take note of the f at the end of sqrtf, asinf, and acosf. This is used for floats rather than doubles, which should clear up any warning messages you get for conversions from float to double. Anyway, here is my version of the code.

#include
#include

int main() {
float x, y, angle, length;

cout << "Enter x value for line ";
cin >> x;

cout << "Enter y value for line ";
cin >> y;

length = sqrtf(x*x + y*y);
angle = acosf(x / length) * 180.0f/ 3.14159f ;

cout << "The angle between the line and the x-axis is " << angle << " degrees " <
return 0;
}

Advertisement
Doh.. I guess it interpreted my includes as HTML tags . You porbably would know it was math.h and iostream.h anyway.
heya!

Thanks for the tip, thought the angle is still wacked. So i so I tried asinf instead of acosf and it seemed to help a bit.. tho only if the angle was between 270 and 90. so in radians, PI works. so now is there anything else to add depending on whether P2 is above or below P1 (point whos angle i''m trying to find). because it works perfect if P2 is above P1.

Thanks,
Scott
I assume you are talking about your code and not mine. Taking the acosf( y /length) will be a big mistake . Anyway, post your code and I'll try to help you out. If you wanted to modify my code to work around the entire unit circle, all you would have to do is make angle = 360 - angle if the y value is negative.

int main() {
float x, y, angle, length;

cout << "Enter x value for line ";
cin >> x;

cout << "Enter y value for line ";
cin >> y;

length = sqrtf(x*x + y*y);
angle = acosf(x / length) * 180.0f/ 3.14159f ;
if (y < 0.0f)
angle = 360.0f - angle;

cout << "The angle between the line and the x-axis is " << angle << " degrees " << endl;

return 0;
}


Edited by - SilentStrike on March 9, 2001 10:36:35 PM
THANK YOU!!! It finally works! my headache is gone i just had to add 90.0f to the final result and it''s bang on.

thanks,
Scott

This topic is closed to new replies.

Advertisement