/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Anyone Good At Trigonometry?

Here’s my problem in a nutshell… I’m trying to get an angle based on the position of the mouse in relation to an element on the page, but I’m running into an issue with the Math.tan() function.

Here’s my equation for getting the angle to the X and Y coordinates:

[CODE]
newAngle = Math.round(Math.pow(Math.tan( y / x ), -1) * (180 / 3.141))
[/CODE]

I get the tan of y over x (opposite over adjacent), raise to the -1 to get the angle (I know this works in degrees, seems like it would have to with radians),
then multiply it by (180 / 3.141) to convert from radians to degrees. And then I round it.

Seems like it should work fine to me however, if
y = 200
x = 20.5

The result in JavaScript is 167, but if I punch it into my calculator (set on degrees: tan( 200 / 20.5 ) ^ -1) I get 5.815948

So there’s got to be something wrong with my formula. I’d appreciate the help!

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERNov 29.2010 — Why not use the built in functions atan() [or better yet, atan2()] to get the angle in radians

and then convert to radians to degrees?
Copy linkTweet thisAlerts:
@empireapathyauthorNov 29.2010 — Worked perfect! I tried that earlier and it didn't work but that must have been due to other bugs. Thanks!
Copy linkTweet thisAlerts:
@JMRKERNov 29.2010 — Worked perfect! I tried that earlier and it didn't work but that must have been due to other bugs. Thanks![/QUOTE]

Hard for me to debug code that you didn't present :rolleyes:

but I'm glad your problem is solved.

Happy to help!

Good Luck!

?
×

Success!

Help @empireapathy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...