This repository has been archived by the owner on Oct 8, 2022. It is now read-only.
/
vector2.js
63 lines (63 loc) · 1.48 KB
/
vector2.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
import Vector from './vector.js';
export default class Vector2 extends Vector {
constructor(x = 0, y = 0) {
super(x, y);
}
get x() {
return this.elements[0];
}
set x(x) {
this.elements[0] = x;
}
get y() {
return this.elements[1];
}
set y(y) {
this.elements[1] = y;
}
//旋转向量
rotate(rads) {
var s = Math.sin(rads);
var c = Math.cos(rads);
var xrot = c * this.x - s * this.y;
this.y = s * this.x + c * this.y;
this.x = xrot;
return this;
}
//距离计算
dist(x = 0, y = 0) {
var dx = this.x - x;
var dy = this.y - y;
return Math.sqrt(dx * dx + dy * dy);
}
distVector(vector2) {
return this.dist(vector2.x, vector2.y);
}
//计算夹角
angle(vector2) {
let mag = this.dist() * vector2.dist();
let dot = this.x * vector2.x + this.y * vector2.y;
return Math.acos(dot / mag);
}
//判断不规则碰撞
inPolygon(vs) {
if (arguments.length > 1) vs = arguments;
if (vs.length < 3) return false;
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
let xi = vs[i].x,
yi = vs[i].y,
xj = vs[j].x,
yj = vs[j].y,
intersect = yi > this.y != yj > this.y && this.x < ((xj - xi) * (this.y - yi)) / (yj - yi) + xi;
if (intersect) inside = !inside;
}
return inside;
}
multiplyMatrix4(matrix4) {
var e = matrix4.elements;
var p = this.elements;
var w = 1 / (e[3] * p[0] + e[7] * p[1] + e[15]);
return this.set((e[0] * p[0] + e[4] * p[1] + e[12]) * w, (e[1] * p[0] + e[5] * p[1] + e[13]) * w);
}
}