Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

beyondtron

Calculating angles of right triangle with integer math

This topic is 5212 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

Ok, I''m having some serious trouble with this, and I''m certain there is a way around it (just have no idea what it is!)... I am trying to calculate the angles of a right triangle (or more specifically sin, cos of the angle). I have all 3 side lengths. The problem is I am using integer math! Here is an example: Side 1 (Adjacent) = 80 Side 2 (Opposite) = 416 Side 3 (Hypotenuse) = 423 If I try to calculate Sine of the angle (SOH) I get sin angle = 416/423 which in integer math equals 0. Is there any way around this problem? I know that if I can find the angle somehow independent of sine by using the lengths of the sides I can use a sine lookup table to calculate the sine/cos of the angle... So if anyone knows the best way to find the straight angle, or finding sin of the angle while using integer math they will be my hero! I think this is a tough problem, but maybe the answer is so obvious that i am just missing it... thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The sine/cosine of an angle is always in-between -1 and 1, inclusive. So integer math will only allow -1, 0, and 1, which isn't of much use. One solution is to store everything in terms of rational numbers. I.e store 416 as the numerator and 423 as the denominator, but don't actually divide them. But trigonometry is all about ratios, so eventually you'll have to use floating-point math, because 416/423 = 832/846 = 2080/2115 = (k * 416)/(k * 423), meaning it's impossible to have a finite lookup table using numerators and denominators.

Are you working on a platform with bad floating-point performance, or one that simply doesn't support it?

EDIT: Actually, it would be possible technically to create such a lookup table, but there would be a lot of work involved generating the table and then looking things up. For instance, you first find two values that divide out to the sine of what you want within a certain precision. For example 3602/9219 is approximately the sine of 23° to four places. When you lookup in the table, try to find the element that minimizes error. You could gain more accuracy by adding more elements to the table, or by increasing the table numerator/denominator values. But that's still a lot of work, when floating-point math would make life easier

P.S When I said "minimizes error", that's something you'll have to explore. I have a few ideas in mind but can't test them right now.

[edited by - Zipster on June 5, 2004 1:27:42 AM]

Share this post


Link to post
Share on other sites
You could use taylor polynomials, although the math on them gets pretty hairy if you are just wanting a simple approximation. Center it around a known value like asin(1), although chances are if you're asking this question you don't know what the heck I'm talking about. Heck I don't even know if the interval of convergence is for all x (looks it up)

EDIT: It looks like it converges for all relevant values of x (-1 to 1) but that's not inclusive unfortunately.

[edited by - uber_n00b on June 4, 2004 12:41:53 AM]

Share this post


Link to post
Share on other sites
Thanks for the help guys, I ended up using a workaround where I start with the angle at the origin, and use a sin lookup table to calculate the target position. Seems to work ok, but now i'm running into other problems! Time for another post!

Share this post


Link to post
Share on other sites
A triangle with sides of 80, 416, and 423 is NOT a right triangle (though it is close). 802 + 4162 = 179456, and 4232 = 178929. So, the sine of the angle between sides of 80 and 423 is NOT 416/423. You need to use the Law Of Cosines to get the angle's cosine, and then you need to use the fact that sin2 Θ + cos2 Θ = 1 for all Θ.

Share this post


Link to post
Share on other sites

  • 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!