admin管理员组文章数量:1429882
So I admit I didn't really know how to phrase the question. But a full explanation should help shed some light. Here is what I know. I have a rectanlge drawn on an HTML5 Canvas. I know the points of all 4 corners and the width and height. From this I can calculate the mid-point. What I want to know is if I rotate the rectangle n degress, what the new points will be. So for example I want to rotate it 45deg from the center point. What will the new vertices of the four corners be? Hopefully that fully explains what I'm looking for. Code examples, preferably in JavaScript, would be great. Thanks in advance.
So I admit I didn't really know how to phrase the question. But a full explanation should help shed some light. Here is what I know. I have a rectanlge drawn on an HTML5 Canvas. I know the points of all 4 corners and the width and height. From this I can calculate the mid-point. What I want to know is if I rotate the rectangle n degress, what the new points will be. So for example I want to rotate it 45deg from the center point. What will the new vertices of the four corners be? Hopefully that fully explains what I'm looking for. Code examples, preferably in JavaScript, would be great. Thanks in advance.
Share Improve this question asked Mar 7, 2014 at 21:51 selanac82selanac82 3,0105 gold badges28 silver badges37 bronze badges1 Answer
Reset to default 8This function calculates what you need. `pivot' is the origin of the rotation (the center of the rectangle in your case).
function rotatePoint(pivot, point, angle) {
// Rotate clockwise, angle in radians
var x = Math.round((Math.cos(angle) * (point[0] - pivot[0])) -
(Math.sin(angle) * (point[1] - pivot[1])) +
pivot[0]),
y = Math.round((Math.sin(angle) * (point[0] - pivot[0])) +
(Math.cos(angle) * (point[1] - pivot[1])) +
pivot[1]);
return [x, y];
};
To convert the degrees to randians:
function degToRad(deg) {
return deg * Math.PI / 180;
}
本文标签: javascriptFind vertices of rectangle after rotating itStack Overflow
版权声明:本文标题:javascript - Find vertices of rectangle after rotating it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745537615a2662342.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论