admin管理员组文章数量:1435859
I've only found a few solutions searching, and they're either language-specific or too plex. I simply need to get a point n distance from another point rotated n amount. I'm using this for a project in HTML5 canvas. I know that there's a rotate function, but I'm working with paths and I don't believe that works with it. Regardless, I'd just like a formula where I could plug in x, y, distance, and direction.
I've only found a few solutions searching, and they're either language-specific or too plex. I simply need to get a point n distance from another point rotated n amount. I'm using this for a project in HTML5 canvas. I know that there's a rotate function, but I'm working with paths and I don't believe that works with it. Regardless, I'd just like a formula where I could plug in x, y, distance, and direction.
Share Improve this question asked Dec 16, 2010 at 4:14 AnonymousAnonymous 1,8937 gold badges23 silver badges29 bronze badges 3- 2 Break out the pencil and paper and work out the math. Then turn it into code. – Anon. Commented Dec 16, 2010 at 4:16
-
this is a simple trigonometric calculation. is
"n"
in radians or degrees? – bcosca Commented Dec 16, 2010 at 4:16 - 1 I used to deal with all these trigonometric calculations way back in the day when developing games. It is fun to do this math when you are programming, even though I used to hate the very same math at college when I had to learn it without knowing where it is applicable. @Ruffian you should go through some math text and learn this stuff, it is interesting and very rewarding once you do it properly and get something to rotate on the screen :) – user529141 Commented Dec 16, 2010 at 4:22
2 Answers
Reset to default 4newx = distance * Math.cos(direction) + x
newy = distance * Math.sin(direction) + y
By calculating the sine and cosine of the angle, you can get the point one unit away from the origin at the given rotation. Just multiply the coordinates by whatever distance you need. Cosine corresponds to X and sine corresponds to Y.
In other words:
- Decide on the angle.
- Calculate X by getting the cosine of the angle.
- Calculate Y by getting the sine of the angle.
- Multiply X and Y by the distance.
- Offset X and Y by your "source" point.
本文标签: javascriptGet X and Y of a point rotation a certain distance from another pointStack Overflow
版权声明:本文标题:javascript - Get X and Y of a point rotation a certain distance from another point - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745668875a2669448.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论