Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 163 additions & 14 deletions lib/_math.js
Original file line number Diff line number Diff line change
Expand Up @@ -4180,6 +4180,11 @@ define('vector/vector2-api',['require','common/not-implemented','vector/v2'],fun

return v;
}

function distance( v1, v2 ) {
return Math.sqrt((v1[0] - v2[0]) * (v1[0] - v2[0]) +
(v1[1] - v2[1]) * (v1[1] - v2[1]));
}

function dot( v1, v2 ) {
var r = 0;
Expand Down Expand Up @@ -4216,7 +4221,24 @@ define('vector/vector2-api',['require','common/not-implemented','vector/v2'],fun

return Math.sqrt( r );
}


function limit(v, max, result){
result = result || new V2();
var length;
length = Math.sqrt( v[0] * v[0] +
v[1] * v[1]);

if (length > max){
var ratio = max/length;
result[0] = v[0] * ratio;
result[1] = v[1] * ratio;
}else{
result[0] = v[0];
result[1] = v[1];
}
return result;
}

function multiply( v, s, result ) {
result = result || new V2();

Expand Down Expand Up @@ -4282,11 +4304,11 @@ define('vector/vector2-api',['require','common/not-implemented','vector/v2'],fun
add: add,
angle: angle,
clear: clear,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: notImplemented,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down Expand Up @@ -4388,6 +4410,16 @@ define('vector/vector2',['require','../../lib/lodash','common/not-implemented','
return new Vector2( this );
}

function distance(arg) {
var other;
if( arg instanceof Vector2 ) {
other = arg.buffer;
} else {
other = arg;
}
return vector2.distance(this.buffer, other);
}

function dot( arg ) {
var other;
if( arg instanceof Vector2 ) {
Expand All @@ -4414,6 +4446,19 @@ define('vector/vector2',['require','../../lib/lodash','common/not-implemented','
return vector2.length( this.buffer );
}

function limit(max, result) {
result = result || this;
var other;
if( result instanceof Vector2 ) {
other = result.buffer;
result.modified = true;
} else {
other = result;
}
vector2.limit(this.buffer, max, other);
return result;
}

function multiply( arg, result ) {
result = result || this;
vector2.multiply( this.buffer, arg, result.buffer );
Expand Down Expand Up @@ -4496,10 +4541,11 @@ define('vector/vector2',['require','../../lib/lodash','common/not-implemented','
angle: angle,
clear: clear,
clone: clone,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down Expand Up @@ -4620,6 +4666,12 @@ define('vector/vector3-api',['require','common/not-implemented','vector/v3'],fun
return result;
}

function distance( v1, v2 ) {
return Math.sqrt((v1[0] - v2[0]) * (v1[0] - v2[0]) +
(v1[1] - v2[1]) * (v1[1] - v2[1]) +
(v1[2] - v2[2]) * (v1[2] - v2[2]));
}

function dot( v1, v2 ) {
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}
Expand Down Expand Up @@ -4654,6 +4706,26 @@ define('vector/vector3-api',['require','common/not-implemented','vector/v3'],fun
return Math.sqrt( r );
}

function limit(v, max, result){
result = result || new V3();
var length;
length = Math.sqrt( v[0] * v[0] +
v[1] * v[1] +
v[2] * v[2]);

if (length > max){
var ratio = max/length;
result[0] = v[0] * ratio;
result[1] = v[1] * ratio;
result[2] = v[2] * ratio;
}else{
result[0] = v[0];
result[1] = v[1];
result[2] = v[2];
}
return result;
}

function multiply( v, s, result ) {
result = result || new V3();

Expand Down Expand Up @@ -4729,11 +4801,11 @@ define('vector/vector3-api',['require','common/not-implemented','vector/v3'],fun
angle: angle,
clear: clear,
cross: cross,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: notImplemented,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down Expand Up @@ -4855,6 +4927,16 @@ define('vector/vector3',['require','../../lib/lodash','common/not-implemented','
return this;
}

function distance(arg) {
var other;
if( arg instanceof Vector3 ) {
other = arg.buffer;
} else {
other = arg;
}
return vector3.distance(this.buffer, other);
}

function dot( arg ) {
var other;
if( arg instanceof Vector3 ) {
Expand All @@ -4881,6 +4963,19 @@ define('vector/vector3',['require','../../lib/lodash','common/not-implemented','
return vector3.length( this.buffer );
}

function limit(max, result) {
result = result || this;
var other;
if( result instanceof Vector3 ) {
other = result.buffer;
result.modified = true;
} else {
other = result;
}
vector3.limit(this.buffer, max, other);
return result;
}

function multiply( arg, result ) {
result = result || this;
vector3.multiply( this.buffer, arg, result.buffer );
Expand Down Expand Up @@ -4967,10 +5062,11 @@ define('vector/vector3',['require','../../lib/lodash','common/not-implemented','
clear: clear,
clone: clone,
cross: cross,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down Expand Up @@ -5068,6 +5164,13 @@ define('vector/vector4-api',['require','common/not-implemented','vector/v4'],fun
return v;
}

function distance( v1, v2 ) {
return Math.sqrt((v1[0] - v2[0]) * (v1[0] - v2[0]) +
(v1[1] - v2[1]) * (v1[1] - v2[1]) +
(v1[2] - v2[2]) * (v1[2] - v2[2]) +
(v1[3] - v2[3]) * (v1[3] - v2[3]));
}

function dot( v1, v2 ) {
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2] + v1[3] * v2[3];
}
Expand Down Expand Up @@ -5096,13 +5199,35 @@ define('vector/vector4-api',['require','common/not-implemented','vector/v4'],fun

function length( v ) {
var r = 0;

r += v[0] * v[0];
r += v[1] * v[1];
r += v[2] * v[2];
r += v[2] * v[2];

return Math.sqrt( r );
r += v[3] * v[3];

return Math.sqrt( r );
}

function limit(v, max, result){
result = result || new V4();
var length;
length = Math.sqrt( v[0] * v[0] +
v[1] * v[1] +
v[2] * v[2] +
v[3] * v[3]);
if (length > max){
var ratio = max/length;
result[0] = v[0] * ratio;
result[1] = v[1] * ratio;
result[2] = v[2] * ratio;
result[3] = v[3] * ratio;
}else{
result[0] = v[0];
result[1] = v[1];
result[2] = v[2];
result[3] = v[3];
}
return result;
}

function multiply( v, s, result ) {
Expand Down Expand Up @@ -5186,11 +5311,11 @@ define('vector/vector4-api',['require','common/not-implemented','vector/v4'],fun
add: add,
angle: angle,
clear: clear,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: notImplemented,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down Expand Up @@ -5303,6 +5428,16 @@ define('vector/vector4',['require','../../lib/lodash','common/not-implemented','
return new Vector4( this );
}

function distance(arg) {
var other;
if( arg instanceof Vector4 ) {
other = arg.buffer;
} else {
other = arg;
}
return vector4.distance(this.buffer, other);
}

function dot( arg ) {
var other;
if( arg instanceof Vector4 ) {
Expand All @@ -5329,6 +5464,19 @@ define('vector/vector4',['require','../../lib/lodash','common/not-implemented','
return vector4.length( this.buffer );
}

function limit(max, result) {
result = result || this;
var other;
if( result instanceof Vector4 ) {
other = result.buffer;
result.modified = true;
} else {
other = result;
}
vector4.limit(this.buffer, max, other);
return result;
}

function multiply( arg, result ) {
result = result || this;
vector4.multiply( this.buffer, arg, result.buffer );
Expand Down Expand Up @@ -5417,10 +5565,11 @@ define('vector/vector4',['require','../../lib/lodash','common/not-implemented','
angle: angle,
clear: clear,
clone: clone,
distance: notImplemented,
distance: distance,
dot: dot,
equal: equal,
length: length,
limit: limit,
multiply: multiply,
negate: negate,
normalize: normalize,
Expand Down
Loading