最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Get X and Y of a point rotation a certain distance from another point - Stack Overflow

matteradmin6PV0评论

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.
Post a comment

comment list (0)

  1. No comments so far