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
Add a ment  | 

2 Answers 2

Reset to default 4
newx = 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