parseInt的坑和Matrix矩陣

1.parseInt的坑

parseInt是把字符串轉(zhuǎn)化為整數(shù),有時候我們直接這樣使用。

parseInt("12.23");
parseInt("0.00008");
parseInt(0.0000008);

但是,當(dāng)很小的數(shù)時候,比如parseInt(0.0000008);會得到8,這肯定不是我們要的結(jié)果,所以需要對數(shù)優(yōu)先判斷下是否小于1。

function myparseInt(x){
            var k=parseFloat(x);
            if(k<1){
                return 0;
            }
            return parseInt(x);
        }

2.Matrix矩陣

function Matrix(a, b, c, d, x, y) {
            this.a = (a != null) ? a : 1;
            this.b = b || 0;
            this.c = c || 0;
            this.d = (d != null) ? d : 1;
            this.x = x || 0;
            this.y = y || 0;
        }

        Matrix.temp = new Matrix();

        Matrix.prototype.toString = function() {
            return "matrix(" + this.a + "," + this.b + "," + this.c + "," + this.d + "," + this.x + "," + this.y + ")";
        };

        Matrix.prototype.equals = function(m) {
            if(this.a === m.a && this.b === m.b && this.c === m.c && this.d === m.d && this.x === m.x && this.y === m.y) {
                return true;
            }
            return false;
        };

        Matrix.prototype.identity = function() {
            this.a = 1;
            this.b = 0;
            this.c = 0;
            this.d = 1;
            this.x = 0;
            this.y = 0;
        };

        Matrix.prototype.clone = function() {
            return new Matrix(
                this.a,
                this.b,
                this.c,
                this.d,
                this.x,
                this.y
            );
        };

        Matrix.prototype.copyFrom = function(m) {
            this.a = m.a;
            this.b = m.b;
            this.c = m.c;
            this.d = m.d;
            this.x = m.x;
            this.y = m.y;
        };

        Matrix.prototype.rotate = function(angle) {
            var u = Math.cos(angle);
            var v = Math.sin(angle);

            var temp = this.a;
            this.a = u * this.a - v * this.b;
            this.b = v * temp + u * this.b;
            temp = this.c;
            this.c = u * this.c - v * this.d;
            this.d = v * temp + u * this.d;
            temp = this.x;
            this.x = u * this.x - v * this.y;
            this.y = v * temp + u * this.y;
        };

        Matrix.prototype.translate = function(x, y) {
            this.x += x;
            this.y += y;
        };

        Matrix.prototype.concat = function(m) {
            var a = this.a * m.a;
            var b = 0;
            var c = 0;
            var d = this.d * m.d;
            var x = this.x * m.a + m.x;
            var y = this.y * m.d + m.y;

            if(this.b !== 0 || this.c !== 0 || m.b !== 0 || m.c !== 0) {
                a += this.b * m.c;
                d += this.c * m.b;
                b += this.a * m.b + this.b * m.d;
                c += this.c * m.a + this.d * m.c;
                x += this.y * m.c;
                y += this.x * m.b;
            }

            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
            this.x = x;
            this.y = y;
        };

        Matrix.prototype.invert = function() {
            if(this.b === 0 && this.c === 0 && this.a !== 0 && this.d !== 0) {

                this.a = 1 / this.a;
                this.d = 1 / this.d;
                this.b = 0;
                this.c = 0;
                this.x = -this.a * this.x;
                this.y = -this.d * this.y;

            } else {

                var det = this.a * this.d - this.b * this.c;
                if(det === 0) {
                    this.identity();
                    return;
                }
                det = 1 / det;

                var temp = this.a;
                this.a = this.d * det;
                this.b = -this.b * det;
                this.c = -this.c * det;
                this.d = temp * det;

                temp = this.y;
                this.y = -(this.b * this.x + this.d * this.y);
                this.x = -(this.a * this.x + this.c * temp);
            }
        };

        Matrix.prototype.getRotationX = function() {
            return Math.atan2(this.b, this.a);
        };

        Matrix.prototype.getRotationY = function() {
            return Math.atan2(this.c, this.d);
        };

        Matrix.prototype.getTransformedX = function(x, y) {
            return this.x + this.a * x + this.c * y;
        };

        Matrix.prototype.getTransformedY = function(x, y) {
            return this.y + this.d * y + this.b * x;
        };

        Matrix.prototype.scale = function(x, y) {
            this.a *= x;
            this.b *= y;
            this.c *= x;
            this.d *= y;
            this.x *= x;
            this.y *= y;
        };

        Matrix.prototype.resolveFloat = function() {
            var mat = this.clone();
            var r = {};
            r.x = mat.x;
            r.y = mat.y;
            //      mat.x=0;
            //      mat.y=0;
            var hd = mat.getRotationX();
            r.radian = hd;
            var du = hd * 180 / Math.PI;
            if(du < 0) {
                du = du + 360;
            }
            r.rotation = du;
            mat.rotate(-hd);
            r.scaleX = mat.a;
            r.scaleY = mat.d;
            return r;
        };

Matrix矩陣表示:
| a | c | x|
| b | d | y|
| 0 | 0 | 1 |
單位矩陣時 a b c d 為 1,0,0,1

矩陣滿足

  1. 乘法結(jié)合律: (AB)C=A(BC)
  2. 乘法左分配律:(A+B)C=AC+BC
  3. 乘法右分配律:C(A+B)=CA+CB
  4. 對數(shù)乘的結(jié)合性k(AB)=(kA)B=A(kB**)
  5. 轉(zhuǎn)置 (AB)T=BTAT

在html顯示對象使用矩陣相乘
假如 AB=ab矩陣相等,求其中任意一個矩陣.

ABBT=abBT //同時乘一個BT
A=abBT;

ATAB=ATab; ///同時乘一個AT
B=ATab;
同理 a b .所以如下

A=abBT;

B=ATab;

a=ABbT

b=aTAB;

其他同理。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容