最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Specify origin of Math.atan2 - Stack Overflow

programmeradmin2浏览0评论

Math.atan2() is a very useful function for calculating angles. However, I cannot wrap my head around one thing:

$(document).mousemove(function(event){
    r = Math.atan2(event.pageY, event.pageX);
    deg = r * 180/Math.PI;
    console.log(deg);   
})

console.log indicates that 0,0 from where the angle is being calculated is at the upper left corner of the screen. How would I go about calculating the angle from a different origin, say the centre of the screen?

Math.atan2() is a very useful function for calculating angles. However, I cannot wrap my head around one thing:

$(document).mousemove(function(event){
    r = Math.atan2(event.pageY, event.pageX);
    deg = r * 180/Math.PI;
    console.log(deg);   
})

console.log indicates that 0,0 from where the angle is being calculated is at the upper left corner of the screen. How would I go about calculating the angle from a different origin, say the centre of the screen?

Share Improve this question asked Jun 28, 2013 at 19:34 stykestyke 2,1742 gold badges27 silver badges54 bronze badges 1
  • Why did someone down vote this question? – AppleGrew Commented Mar 8, 2014 at 10:46
Add a ment  | 

1 Answer 1

Reset to default 8

You would subtract the coordinates of your origin from the coordinates that you want to find the angle of:

r = Math.atan2(event.pageY - originY, event.pageX - originX);
发布评论

评论列表(0)

  1. 暂无评论