【发布时间】:2014-05-29 15:41:24
【问题描述】:
在获取旋转正方形的角时,任何人都可以知道我的公式有什么问题吗?当我将正方形从 1 度旋转到 10 度时似乎是正确的,但是当你给它 10 度以上的旋转时,我得到了错误的角位置。
这段代码来自我的 Square 类的 getCorners() 方法:
var hx = 0,
hy = 0;
hx = hy = this.size / 2;
var cos = Math.cos(this.rotation * Math.PI / 180),
sin = Math.sin(this.rotation * Math.PI / 180);
var corners = {
a: { x: this.x - hx, y: this.y - hy },
b: { x: this.x + hx, y: this.y - hy },
c: { x: this.x + hx, y: this.y + hy },
d: { x: this.x - hx, y: this.y + hy }
};
var tl = corners.a,
tr = corners.b,
br = corners.c,
bl = corners.d;
corners.a.x = (tl.x - this.x) * cos - (tl.y - this.y) * sin + this.x;
corners.a.y = (tl.x - this.x) * sin + (tl.y - this.y) * cos + this.y;
corners.b.x = (tr.x - this.x) * cos - (tr.y - this.y) * sin + this.x;
corners.b.y = (tr.x - this.x) * sin + (tr.y - this.y) * cos + this.y;
corners.c.x = (br.x - this.x) * cos - (br.y - this.y) * sin + this.x;
corners.c.y = (br.x - this.x) * sin + (br.y - this.y) * cos + this.y;
corners.d.x = (bl.x - this.x) * cos - (bl.y - this.y) * sin + this.x;
corners.d.y = (bl.x - this.x) * sin + (bl.y - this.y) * cos + this.y;
完整源代码和运行程序的链接:http://jsfiddle.net/css3guy/8B2eT/
【问题讨论】:
标签: html math canvas trigonometry