javascript 围绕一个点旋转矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4465931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:46:21  来源:igfitidea点击:

Rotate rectangle around a point

javascript2dtrigonometry

提问by Anonymous

How would I get 4 points rotated a certain degrees around a pointer to form a rectangle? I can rotate a point around a point, but I can't offset it to make a rectangle that isn't distorted.

如何让 4 个点围绕指针旋转一定度数以形成矩形?我可以围绕一个点旋转一个点,但我不能偏移它来制作一个不变形的矩形。

回答by Felix Eve

If you can rotate a point around a point then it should be easy to rotate a rectangle - you just rotate 4 points.

如果您可以围绕一个点旋转一个点,那么旋转一个矩形应该很容易——您只需旋转 4 个点。

Here is a js function to rotate a point around an origin:

这是一个围绕原点旋转点的js函数:

function rotate_point(pointX, pointY, originX, originY, angle) {
    angle = angle * Math.PI / 180.0;
    return {
        x: Math.cos(angle) * (pointX-originX) - Math.sin(angle) * (pointY-originY) + originX,
        y: Math.sin(angle) * (pointX-originX) + Math.cos(angle) * (pointY-originY) + originY
    };
}

And then you can do this to each point. Here is an example: http://jsfiddle.net/dahousecat/4TtvU/

然后你可以对每个点都这样做。这是一个例子:http: //jsfiddle.net/dahousecat/4TtvU/

Change the angle and hit run to see the result...

改变角度并点击运行以查看结果......