Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Stupid Calculation Bug

This topic is 4360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to find the 3rd side of a triangle given 2 sides and an angle. However, I think I'm getting the wrong values. I don't see where the problem is located. Equation: c^2 = a^2 + b^2 - 2abcos(@) float r1, r2, ang, T1, T2, d; r1 = 3.0f; r2 = 5.0f; ang = 1.0f; T1 = (r1 * r1) + (r2 * r2); T2 = (2.0f * r1 * r2); d = sqrtf( T1 - (T2*cosf(ang)) ); I get d = 4.22. This value seems wrong to me. I think it should be around 2. What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
4.22 sounds correct. Imagine that this was a right triangle and you were trying to find the hypotenuse. Then the value of the third side would be sqrt(9 + 25) = sqrt(34) = ~5.83. Since the angle is a little smaller than 60 degrees (1 radian), we'd expect the actual value to be somewhat smaller than 5.83, which 4.22 certainly is.

There is a theorem called the triangle theorem which algebraically and graphically gives the range in which the length of a third side of a triangle may fall, given the other two sides. If the angle between sides a and b is small (~0), the length of c is close to |a - b|. If the angle is large (~pi), the length is close to |a + b|.

The angle between the two sides would have to be small (a couple of degrees or so) before it would approach 2, and it would never be exactly 2.

[edit]: I just thought of something; are you thinking that the "angle" parameter is in degrees? If so, that probably explains the confusion. A value of 1 degree would certainly produce a result close to 2, but the value of "1.0f" you're supplying to cosf() is actually specifying one radian, which is just about 57 degrees.

hope that helps,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!