-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.babel.js
120 lines (98 loc) · 2.65 KB
/
vector.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* 用于矢量计算的类
*/
var Vector = function () {
/**
* 构造函数
* @param x 横坐标
* @param y 纵坐标
*/
function Vector(x, y) {
_classCallCheck(this, Vector);
this.x = x || 0;
this.y = y || 0;
}
/**
* 矢量加
* @param vector
* @returns {Vector}
*/
_createClass(Vector, [{
key: "add",
value: function add(vector) {
return new Vector(this.x + vector.x, this.y + vector.y);
}
/**
* 矢量减
* @param vector
* @returns {Vector}
*/
}, {
key: "subtract",
value: function subtract(vector) {
return new Vector(this.x - vector.x, this.y - vector.y);
}
/**
* 矢量乘
* @param vector
* @returns {Vector}
*/
}, {
key: "multiply",
value: function multiply(vector) {
return new Vector(this.x * vector.x, this.y * vector.y);
}
/**
* 矢量乘以标量
* @param scalar
* @returns {Vector}
*/
}, {
key: "multiplyScalar",
value: function multiplyScalar(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
}
/**
* 矢量除
* @param vector
* @returns {Vector}
*/
}, {
key: "divide",
value: function divide(vector) {
return new Vector(this.x / vector.x, this.y / vector.y);
}
/**
* 矢量除以标量
* @param scalar
* @returns {Vector}
*/
}, {
key: "divideScalar",
value: function divideScalar(scalar) {
return new Vector(this.x / scalar, this.y / scalar);
}
/**
* 矢量长度
* @returns {number}
*/
}, {
key: "length",
value: function length() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2));
}
/**
* 矢量标准化
* @returns {Vector}
*/
}, {
key: "normalize",
value: function normalize() {
return this.divideScalar(this.length());
}
}]);
return Vector;
}();