Javascript 从角度矩形的尺寸和位置查找顶点
我正在尝试实现转换矩阵公式: cos()-sin() sin()cos() 在下面的Javascript代码中,this.rot.cos=角度的cos,this.rot.sin是角度的sinJavascript 从角度矩形的尺寸和位置查找顶点,javascript,html,canvas,vector,Javascript,Html,Canvas,Vector,我正在尝试实现转换矩阵公式: cos()-sin() sin()cos() 在下面的Javascript代码中,this.rot.cos=角度的cos,this.rot.sin是角度的sin var h1 = this.rot.cos * this.dimension.x, h2 = this.rot.sin * this.dimension.x, h3 = this.rot.cos * this.dimension.y, h4 = this.rot.sin * this.dimension.y
var h1 = this.rot.cos * this.dimension.x,
h2 = this.rot.sin * this.dimension.x,
h3 = this.rot.cos * this.dimension.y,
h4 = this.rot.sin * this.dimension.y;
v = [
{
x: h1 - h4,
y: h2 + h3
},
{
x: -(h1 - h4),
y: h2 + h3
},
{
x: -(h1 - h4),
y: -(h2 + h3)
},
{
x: h1 - h4,
y: -(h2 + h3)
}
];
tankBattle.ctx.beginPath();
tankBattle.ctx.moveTo(v[0].x, v[0].y);
tankBattle.ctx.lineTo(v[1].x, v[1].y);
tankBattle.ctx.lineTo(v[2].x, v[2].y);
tankBattle.ctx.lineTo(v[3].x, v[3].y);
tankBattle.ctx.lineTo(v[0].x, v[0].y);
我得到的形状以与正弦波和余弦波相同的模式移动,并在一定程度上变成0(我相信是90和270)。这里的顶点计算有什么不对吗?您的公式似乎有误,以下是有效的:
我不确定你想在哪个轴上旋转,但这个轴可能会有帮助。他们为我工作,不幸的是我不再有我写的脚本。在x轴和y轴上?这是正确的,非常感谢。我想知道自己哪里出了错,真是疯了。
function drawTank(x, y, rot) {
var cosrot = Math.cos(rot);
var sinrot = Math.sin(rot);
var h1 = cosrot * dimension.x,
h2 = sinrot * dimension.x,
h3 = cosrot * dimension.y,
h4 = sinrot * dimension.y;
v = [{
x: h1 - h4,
y: h2 + h3
},
{
x: h1 + h4,
y: h2 - h3
},
{
x: -h1 + h4,
y: -h2 - h3
},
{
x: -h1 - h4,
y: -h2 + h3
}];
ctx.save();
ctx.translate(x, y);
ctx.beginPath();
ctx.moveTo(v[0].x, v[0].y);
ctx.lineTo(v[1].x, v[1].y);
ctx.lineTo(v[2].x, v[2].y);
ctx.lineTo(v[3].x, v[3].y);
ctx.lineTo(v[0].x, v[0].y);
ctx.stroke();
ctx.restore();
}