开发者

Rotate rectangle around a point

开发者 https://www.devze.com 2023-01-30 19:29 出处:网络
How would I get 4 points rotated a certain degrees around a pointer to form a rectangle? I can ro开发者_JS百科tate a point around a point, but I can\'t offset it to make a rectangle that isn\'t distor

How would I get 4 points rotated a certain degrees around a pointer to form a rectangle? I can ro开发者_JS百科tate a point around a point, but I can't offset it to make a rectangle that isn't distorted.


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

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
    };
}

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

0

精彩评论

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