我无法理解使用JavasScript执行以下数学方程式

时间:2016-09-26 10:15:52

标签: javascript math three.js

我无法理解函数labelBox中使用的这行代码this.position = convertlatlonToVec3(cardinal.lat, cardinal.lon).multiplyScalar(radius);。 multiplyScalar(radius)如何工作。

function convertlatlonToVec3(lat, lon)
{
  var cosLat = Math.cos(circle.getCenter().lat() * degrees);
  var sinLat = Math.sin(circle.getCenter().lat() * degrees); 

  var xSphere = radiusEquator * cosLat;
  var ySphere = 0;
  var zSphere = radiusPoles * sinLat;
  var rSphere = Math.sqrt(xSphere*xSphere + ySphere*ySphere + zSphere*zSphere);

  var tmp = rSphere * Math.cos(lat * degrees);    

  xSphere = tmp * Math.cos((lon - circle.getCenter().lng()) * degrees);
  ySphere = tmp * Math.sin((lon - circle.getCenter().lng()) * degrees);
  zSphere = rSphere * Math.sin(lat * degrees);

  var x = -ySphere/circle.getRadius();
  var y = (zSphere*cosLat - xSphere*sinLat)/circle.getRadius();
  var z = 0;

  return new THREE.Vector3(x, y, z);

}

function labelBox(cardinal, radius, root)
{
  this.screenvector = new THREE.Vector3(0,0,0);
  this.labelID = 'MovingLabel'+ cardinal.ID;
  this.position = convertlatlonToVec3(cardinal.lat, cardinal.lon).multiplyScalar(radius);
}

1 个答案:

答案 0 :(得分:1)

Three.js文档 here
对于THREE.Vector3 multiplyScalar方法,文档 here

  

.multiplyScalar(s)this
  将此向量乘以标量s。

此方法的内容可以找到in the THREE.Vector3 class,如下所示:

multiplyScalar: function ( scalar ) {

    if ( isFinite( scalar ) ) {

        this.x *= scalar;
        this.y *= scalar;
        this.z *= scalar;

    } else {

        this.x = 0;
        this.y = 0;
        this.z = 0;

    }

    return this;

},