# Stupid Calculation Bug

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

## 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 on other sites
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.

: 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 on other sites
Thanks for the help. It was a very silly bug. I forgot to convert to radians when finding cosine of the angle.

1. 1
Rutin
69
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633430
• Total Posts
3011831
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

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!