旋转函数的内在 [英] The Innards of a Rotation Function

查看:64
本文介绍了旋转函数的内在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关修改2d矩阵的旋转函数中发生的情况的信息.我发现了这一点,但是将旋转度指定为90、180或270不会产生任何变化.这是我的代码:

I'm looking for information on what goes on in a rotation function that modifies a 2d matrix. I found this, but assigning the rotation to 90, 180, or 270 renders no change. Here's my code:

Matrix.prototype.rotate = function(deg) {
    var radians = deg * Math.PI / 180,
        sin = Math.sin(radians),
        cos = Math.cos(radians),
        a = this.a,
        b = this.b,
        c = this.c,
        d = this.d,
        tx = this.tx,
        ty = this.ty;

    this.a = a*cos - b*sin;
    this.b = a*sin + b*cos;
    this.c = c*cos - d*sin;
    this.d = c*sin + d*cos;
    this.tx = tx*cos - ty*sin;
    this.ty = tx*sin + ty*cos;
}

我确实设法找到了一些有关为什么它不起作用的信息,但是没有找到有关如何解决它的信息.据我了解,旋转90度会导致sin和/或cos如此之小,以至于应用于矩阵时,它不会改变任何东西.如果我对此有误,请告诉我.

I did manage to find some info on why it doesn't work, but nothing on how to fix it. From what I understand, rotating by 90 degrees causes sin and/or cos to be such a small number, that when applied to the matrix, it doesn't change anything. Please let me know if I'm wrong about this.

要使其正常工作,我将其添加到函数的开头:

To get it to work I added this to the beginning of the function:

if(!(deg % 90)) {
    deg -= .0001;
}

它可以工作,但是我确定在旋转数千次之后,它并不是很准确.有谁知道更好的解决方案?

It works, but I'm sure after rotating several thousands times it isn't going to be very accurate. Does anyone know of a better solution?

实际旋转值存储在其他位置,并且仅在有帮助的情况下才将差值传递给函数.

The actual rotation value is stored elsewhere and only passes the difference to the function, if that helps.

编辑:我忘了提到当sin或cos等于1或-1时什么也不会发生.

Edit : I forgot to mention that nothing happens when sin or cos equals 1 or -1 either.

推荐答案

不确定a,b,c和d的用途,但是只要有可能,就应保留所有原始值并使用绝对角度来计算旋转值.否则,就像您提到的那样,不正确的总和将变得越来越明显.

Not sure what your a, b, c and d are used for, but whenever possible you should keep all the original values and use the absolute angle to compute the rotated values. Otherwise, as you mentioned as well, the summation of inaccuracies will become more and more obvious.

我会摆脱90度时的-= 0.001 hack,sin应该是1,而cos应该是0(或者至少非常接近),实现以下转换:

I would get rid of the -= 0.001 hack, at 90 degrees, sin should be 1 and cos should be 0 (or at least very close to it), effecting the following transformation:

a' := -b
b' := a
c' := -d
d' := c
tx' := -ty
ty' := tx

这篇关于旋转函数的内在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆