开发者

Get X and Y of a point rotation a certain distance from another point

I've only found a few sol开发者_开发知识库utions searching, and they're either language-specific or too complex. 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.


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜