用户:
萌新大佬查看:3 回复:11 评论:3 创建时间:2023-01-12T12:02:14
class Box3Vector3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
static fromPolar(mag, phi, theta) {
return new Box3Vector3(mag * Math.sin(theta) * Math.sin(phi), mag * Math.cos(theta), mag * Math.sin(theta) * Math.cos(phi));
}//极坐标系转换
set(x, y, z) {
this.x = +x;
this.y = +y;
this.z = +z;
return this;
}//字面意思
copy(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}//字面意思
add(v) {
return new Box3Vector3(this.x + v.x, this.y + v.y, this.z + v.z);
}//字面意思
sub(v) {
return new Box3Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
}//字面意思
mul(v) {
return new Box3Vector3(this.x * v.x, this.y * v.y, this.z * v.z);
}//字面意思
div(v) {
return new Box3Vector3(v.x === 0 ? 0 : this.x / v.x, v.y === 0 ? 0 : this.y / v.y, v.z === 0 ? 0 : this.z / v.z);
}//字面意思
addEq(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
}//字面意思,加到自己身上并返回自己
subEq(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}//字面意思,加到自己身上并返回自己
mulEq(v) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
}//字面意思,加到自己身上并返回自己
divEq(v) {
this.x = v.x === 0 ? 0 : this.x / v.x;
this.y = v.y === 0 ? 0 : this.y / v.y;
this.z = v.z === 0 ? 0 : this.z / v.z;
return this;
}//字面意思,加到自己身上并返回自己
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}//点乘
cross(v) {
return new Box3Vector3(this.y * v.z - this.z * v.y, this.z * v.x - this.x * v.z, this.x * v.y - this.y * v.x);
}//叉乘
scale(n) {
return new Box3Vector3(this.x * n, this.y * n, this.z * n);
}//字面意思
clone() {
return new Box3Vector3(this.x, this.y, this.z);
}//new出来的自己
lerp(v, n) {
return new Box3Vector3(this.x + (v.x - this.x) * n, this.y + (v.y - this.y) * n, this.z + (v.z - this.z) * n);
}//相当于this.add(this.towards(v).scale(n)),一般用于向v方向移动
mag() {
return Math.sqrt(this.dot(this));
}//模
sqrMag() {
return this.dot(this);
}//模的平方
towards(v) {
return v.sub(this);
}//v.sub(this)
distance(v) {
return this.sub(v).mag();
}//字面意思
normalize() {
const x = this.x;
const y = this.y;
const z = this.y;
let r = x * x + y * y + z * z;
if (r > 0) {
r = 1 / Math.sqrt(r);
}
return new Box3Vector3(x * r, y * r, z * r);
}//求模为一方向与自己相同的向量
angle(v) {
let r = v.sqrMag() * this.sqrMag();
if (r > 0) {
r = 1 / Math.sqrt(r);
}
const cos = r * this.dot(v);
if (1 < cos) {
return 0;
}
else if (cos < -1) {
return Math.PI;
}
else {
return Math.acos(cos);
}
}//字面意思
max(v) {
return new Box3Vector3(Math.max(this.x, v.x), Math.max(this.y, v.y), Math.max(this.z, v.z));
}//字面意思
min(v) {
return new Box3Vector3(Math.min(this.x, v.x), Math.min(this.y, v.y), Math.min(this.z, v.z));
}//字面意思
exactEquals(v) {
return this.x === v.x && this.y === v.y && this.z === v.z;
}//完全相等
equals(v) {
return (Math.abs(this.x - v.x) < EPSILON &&
Math.abs(this.y - v.y) < EPSILON &&
Math.abs(this.z - v.z) < EPSILON);
}//js小数减喵出问题,所以一般使用equals可以避免精度问题
toString() {
return `{ x:${this.x}, y:${this.y}, z:${this.z} }`;
}//字面意思
}
Nomen太强了,讲的很好,但是部分内容还是缺少一定的解释(说到底还是懒,字面意思是个什么意思啊喂
如果不是那么懒的话可以考虑用jsdoc写俩注释(好吧实际上api的返回值都有了,就差一些关于参数的解释
可惜帖子还是太水了,DDDDD
啊吧啊吧啊吧啊吧啊吧啊吧啊吧啊吧啊吧啊吧啊吧啊吧
点赞1
评论