Files
WebGPU-Neural-Network-Trainer/framework/Vector3.js
2025-11-19 10:42:46 +01:00

61 lines
783 B
JavaScript

export default class Vector3 {
x = 0;
y = 0;
z = 0;
constructor( x = 0, y = 0, z = 0 ) {
this.x = x;
this.y = y;
this.z = z;
}
static subtract( a, b ) {
return new Vector3( a.x - b.x, a.y - b.y, a.z - b.z );
}
length() {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
}
static cross( a, b ) {
return new Vector3(
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
);
}
static dot( a, b ) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
static normalize( v ) {
const length = Math.sqrt( v.x * v.x + v.y * v.y + v.z * v.z );
if ( length > 0.00001 ) {
return new Vector3( v.x / length, v.y / length, v.z / length );
} else {
return new Vector3( 0, 0, 0 );
}
}
}