## 精彩人生

｛总结,是为了更好的开始｝专注于web技术，每天进步一点点｛学无止境,积累创造财富｝...

# 基础类

(function(window) {

    var Vector2 = function(x, y) {

        this.x = x || 0;

        this.y = y || 0;

    };

    Vector2.prototype = {

        set: function(x, y) {

            this.x = x;

            this.y = y;

            return this;

        },

        sub: function(v) {

            return new Vector2(this.x - v.x, this.y - v.y);

        },

        multiplyScalar: function(s) {

            this.x *= s;

            this.y *= s;

            return this;

        },

        divideScalar: function(s) {

            if (s) {

                this.x /= s;

                this.y /= s;

            } else {

                this.set(0, 0);

            }

            return this;

        },

        length: function() {

            return Math.sqrt(this.lengthSq());

        },

        normalize: function() {

            return this.divideScalar(this.length());

        },

        lengthSq: function() {

            return this.x * this.x + this.y * this.y;

        },

        distanceToSquared: function(v) {

            var dx = this.x - v.x,

            dy = this.y - v.y;

            return dx * dx + dy * dy;

        },

        distanceTo: function(v) {

            return Math.sqrt(this.distanceToSquared(v));

        },

        setLength: function(l) {

            return this.normalize().multiplyScalar(l);

        }

    };

    window.Vector2 = Vector2;

} (window));

(function (window) {

    var Ball = function (r, v, p, cp) {

        this.radius = r;

        this.velocity = v;

        this.position = p;

        this.collectionPosition = cp;

    }

    Ball.prototype = {

        collection: function (v) {

            this.velocity = this.collectionPosition.sub(this.position).setLength(v);

        },

        disband: function () {

            this.velocity = new Vector2(MathHelp.getRandomNumber(-230, 230), MathHelp.getRandomNumber(-230, 230));

        }

    }

    window.Ball = Ball;

} (window));

collection: function (v) {

     this.velocity = this.collectionPosition.sub(this.position).setLength(v);

},

setLength: function (l) {

      return this.normalize().multiplyScalar(l);

 }

this.velocity = this.collectionPosition.sub(this.position).normalize().multiplyScalar(v);

normalize是获取单位向量，也可以改成：

this.collectionPosition.sub(this.position).divideScalar(this.length()).multiplyScalar(v);

this.collectionPosition

.sub(this.position)                获取小球所在位置指向小球的向量；

.divideScalar(this.length()) 获取小球所在位置指向小球的向量的单位向量；
.multiplyScalar(v);               设置该向量的长度。

(function (window) {

    var MathHelp = {};

    MathHelp.getRandomNumber = function (min, max) {

        return (min + Math.floor(Math.random() * (max - min + 1)));

    }

    window.MathHelp = MathHelp;

} (window));

# 粒子生成

var ps = [], balls = [];

function init(tex) {

    balls.length = 0;

    ps.length = 0;

    cxt.clearRect(0, 0, canvas.width, canvas.height);

    cxt.fillStyle = "rgba(0,0,0,1)";

    cxt.fillRect(0, 0, canvas.width, canvas.height);

    cxt.fillStyle = "rgba(255,255,255,1)";

    cxt.font = "bolder 160px 宋体";

    cxt.textBaseline = 'top';

    cxt.fillText(tex, 20, 20);

    //收集所有像素

    for (y = 1; y < canvas.height; y += 7) {

        for (x = 1; x < canvas.width; x += 7) {

            imageData = cxt.getImageData(20 + x, 20 + y, 1, 1);

            if (imageData.data[0] > 170) {

                ps.push({

                    px: 20 + x,

                    py: 20 + y

                })

            }

        }

    };

    cxt.fillStyle = "rgba(0,0,0,1)";

    cxt.fillRect(20, 20, canvas.width, canvas.height);

    //像素点和小球转换

    for (var i in ps) {

        var ball = new Ball(2, new Vector2(0, 0), new Vector2(ps[i].px, ps[i].py), new Vector2(ps[i].px, ps[i].py));

        balls.push(ball);

    };

    cxt.fillStyle = "#fff";

    for (i in balls) {

        cxt.beginPath();

        cxt.arc(balls[i].position.x, balls[i].position.y, balls[i].radius, 0, Math.PI * 2, true);

        cxt.closePath();

        cxt.fill();

    }

    //解散：生成随机速度

    for (var i in balls) {

        balls[i].disband();

    }

}

var time = 0;

var cyc = 15;

var a = 80;

var collectionCMD = false;

setInterval(function () {

    cxt.fillStyle = "rgba(0, 0, 0, .3)";

    cxt.fillRect(0, 0, canvas.width, canvas.height);

    cxt.fillStyle = "#fff";

    time += cyc;

    for (var i in balls) {

        if (collectionCMD === true && balls[i].position.distanceTo(balls[i].collectionPosition) < 2) {

            balls[i].velocity.y = 0;

            balls[i].velocity.x = 0;

        }

    }

    if (time === 3000) {

        collectionCMD = true;

        for (var i in balls) {

            balls[i].collection(230);

        }

    }

    if (time === 7500) {

        time = 0;

        collectionCMD = false;

        for (var i in balls) {

            balls[i].disband();

        }

    }

    for (var i in balls) {

        cxt.beginPath();

        cxt.arc(balls[i].position.x, balls[i].position.y, balls[i].radius, 0, Math.PI * 2, true);

        cxt.closePath();

        cxt.fill();

        balls[i].position.y += balls[i].velocity.y * cyc / 1000;

        balls[i].position.x += balls[i].velocity.x * cyc / 1000;

    }

},

cyc);

# 这你也敢叫人工智能？ok，未完待续......

#### 人工智能实践教程（一）

2017-10-20 09:47:50

#### 我的人工智能之路1-Python编程：从入门到实践-1

2017-11-20 20:22:48

#### OOA&D实践之路——真实案例解析OO理论与实践(一)

2014-09-01 09:41:25

#### 面向对象（OO）基础和原则

2012-03-28 21:22:35

#### 人工智能课程总结(一)数学基础 一些值得看的博客链接

2017-04-28 14:13:43

#### [学习笔记]人工智能-感知器分类算法

2018-01-14 16:31:50

#### 人工智能实践：TensorFlow笔记学习（一）—— 人工智能概述

2018-03-30 19:37:41

#### 机器学习人工智能，数学基础知识

2017-08-21 14:58:27

#### 设计模式:oo基础、原则及模式

2008-04-23 00:07:00

#### Excel VBA+Access财务系统开发从基础到实践.rar

2010年05月25日 3.5MB 下载