提交 e4a451a8 编写于 作者: D Drew Noakes

Improved implementation and unit test for Vector3.angleTo to account for...

Improved implementation and unit test for Vector3.angleTo to account for numerical problems outside of full [-1,1] range.
上级 791a11dd
......@@ -564,7 +564,17 @@ THREE.extend( THREE.Vector3.prototype, {
angleTo: function ( v ) {
return Math.acos( Math.min ( 1, this.dot( v ) / this.length() / v.length() ) );
// clamp, to handle numerical problems
function clamp( x ) {
return Math.min( Math.max( x, -1 ), 1 );
}
var theta = this.dot( v ) / ( this.length() * v.length() );
return Math.acos( clamp ( theta ) );
},
......
......@@ -292,8 +292,10 @@ test( "reflect", function() {
test( "angleTo", function() {
var a = new THREE.Vector3( 0, -0.18851655680720186, 0.9820700116639124 );
var b = new THREE.Vector3( 0, 0.18851655680720186, -0.9820700116639124 );
equal( a.angleTo( a ), 0 );
equal( a.angleTo( a ), 0 );
equal( a.angleTo( b ), Math.PI );
var x = new THREE.Vector3( 1, 0, 0 );
var y = new THREE.Vector3( 0, 1, 0 );
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册