Files
three.js/src/core/Vector3.js
T
2011-01-03 19:42:47 +00:00

237 lines
3.1 KiB
JavaScript

/**
* @author mr.doob / http://mrdoob.com/
* @author kile / http://kile.stravaganza.org/
* @author philogb / http://blog.thejit.org/
*/
THREE.Vector3 = function ( x, y, z ) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
};
THREE.Vector3.prototype = {
set: function ( x, y, z ) {
this.x = x;
this.y = y;
this.z = z;
return this;
},
copy: function ( v ) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
},
add: function ( a, b ) {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
},
addSelf: function ( v ) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
},
addScalar: function ( s ) {
this.x += s;
this.y += s;
this.z += s;
return this;
},
sub: function( a, b ) {
this.x = a.x - b.x;
this.y = a.y - b.y;
this.z = a.z - b.z;
return this;
},
subSelf: function ( v ) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
},
cross: function ( a, b ) {
this.x = a.y * b.z - a.z * b.y;
this.y = a.z * b.x - a.x * b.z;
this.z = a.x * b.y - a.y * b.x;
return this;
},
crossSelf: function ( v ) {
var tx = this.x, ty = this.y, tz = this.z;
this.x = ty * v.z - tz * v.y;
this.y = tz * v.x - tx * v.z;
this.z = tx * v.y - ty * v.x;
return this;
},
multiply: function ( a, b ) {
this.x = a.x * b.x;
this.y = a.y * b.y;
this.z = a.z * b.z;
return this;
},
multiplySelf: function ( v ) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
},
multiplyScalar: function ( s ) {
this.x *= s;
this.y *= s;
this.z *= s;
return this;
},
divideSelf: function ( v ) {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z;
return this;
},
divideScalar: function ( s ) {
this.x /= s;
this.y /= s;
this.z /= s;
return this;
},
dot: function ( v ) {
return this.x * v.x + this.y * v.y + this.z * v.z;
},
distanceTo: function ( v ) {
var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
return Math.sqrt( dx * dx + dy * dy + dz * dz );
},
distanceToSquared: function ( v ) {
var dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
return dx * dx + dy * dy + dz * dz;
},
length: function () {
return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
},
lengthSq: function () {
return this.x * this.x + this.y * this.y + this.z * this.z;
},
negate: function () {
this.x = - this.x;
this.y = - this.y;
this.z = - this.z;
return this;
},
normalize: function () {
var length = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
length > 0 ? this.multiplyScalar( 1 / length ) : this.set( 0, 0, 0 );
return this;
},
setLength: function( len ) {
return this.normalize().multiplyScalar( len );
},
isZero: function () {
var almostZero = 0.0001;
return ( Math.abs( this.x ) < almostZero ) && ( Math.abs( this.y ) < almostZero ) && ( Math.abs( this.z ) < almostZero );
},
clone: function () {
return new THREE.Vector3( this.x, this.y, this.z );
},
toString: function () {
return 'THREE.Vector3 ( ' + this.x + ', ' + this.y + ', ' + this.z + ' )';
}
};