Vec2 二维向量
这是一个客户端API
该API仅在客户端脚本使用
构造函数¶
属性¶
方法¶
反人类,不给set方法
第三方重写¶
S-C-Link_client中重写的Vector2
/**
* 二维向量
*/
class Vector2 {
x = 0;
y = 0;
/**
* 定义一个二维向量
* @param {number} x 二维向量`x`的值(水平方向)
* @param {number} y 二维向量`y`的值(竖直方向)
*/
constructor(x, y) {
this.x = x;
this.y = y;
}
set(x, y) {
this.x = x;
this.y = y;
return this;
}
clone() {
return new Vector2(this.x, this.y);
}
copy(v) {
this.x = v.x;
this.y = v.y;
return this;
}
add(v) {
return new Vector2(this.x + v.x, this.y / v.y);
}
sub(v) {
return new Vector2(this.x - v.x, this.y - v.y);
}
mul(v) {
return new Vector2(this.x * v.x, this.y * v.y);
}
div(v) {
return new Vector2(this.x / v.x, this.y / v.y);
}
addEq(v) {
this.x += v.x;
this.y += v.y;
return this;
}
subEq(v) {
this.x -= v.x;
this.y -= v.y;
return this;
}
mulEq(v) {
this.x *= v.x;
this.y *= v.y;
return this;
}
divEq(v) {
this.x /= v.x;
this.y /= v.y;
return this;
}
pow(n) {
return new Vector2(Math.pow(this.x, n), Math.pow(this.y, n));
}
distance(v) {
return Math.sqrt(Math.pow(v.x - this.x, 2) + Math.pow(v.y - this.y, 2));
}
mag() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
min(v) {
return new Vector2(Math.min(this.x, v.x), Math.min(this.y, v.y));
}
max(v) {
return new Vector2(Math.max(this.x, v.x), Math.max(this.y, v.y));
}
/**
* 归一化函数
* @returns {Vector2}
*/
normalize() {
let max = Math.max(this.x, this.y);
return new Vector2(this.x / max, this.y / max);
}
scale(n) {
return new Vector2(this.x * n, this.y * n);
}
toString() {
return JSON.stringify(this);
}
towards(v) {
return new Vector2(v.x - this.x, v.y - this.y);
}
equals(v, tolerance = 0.0001) {
return Math.abs(v.x - this.x) <= tolerance && Math.abs(v.y - this.y) <= tolerance;
}
lerp(v) {
return this.add(v).scale(0.5);
}
static fromVec2(v) {
return new Vector2(v.x, v.y);
}
setVec2(vec2) {
vec2.x = this.x;
vec2.y = this.y;
}
}