围绕一个点旋转矩形 [英] Rotate rectangle around a point
问题描述
如何让 4 个点围绕指针旋转一定度数以形成矩形?我可以围绕一个点旋转一个点,但我不能通过偏移它来制作一个不变形的矩形.
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 个点.
If you can rotate a point around a point then it should be easy to rotate a rectangle - you just rotate 4 points.
这是一个绕原点旋转一个点的js函数:
Here is a js function to rotate a point around an origin:
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
};
}
然后你可以对每个点都这样做.这是一个例子:http://jsfiddle.net/dahousecat/4TtvU/
And then you can do this to each point. Here is an example: http://jsfiddle.net/dahousecat/4TtvU/
改变角度并点击运行以查看结果...
Change the angle and hit run to see the result...
这篇关于围绕一个点旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!