将一个库换成另一个

swapping one library out for another

这里的任务相对简单,但看到我刚刚开始掌握面向对象的编程,这让我感到困惑。我目前正在使用 lon_lat_to_cartesian:

的第一个函数
function lonLatToVector3( lng, lat, out )
{
out = out || new THREE.Vector3();

//flips the Y axis
lat = PI / 2 - lat;

//distribute to sphere
out.set(
            Math.sin( lat ) * Math.sin( lng ),
            Math.cos( lat ),
            Math.sin( lat ) * Math.cos( lng )
);

return out;

}

我在 glmain.js 文件中使用以下行调用它:

position = lonLatToVector3(data.latitude, data.longitude);

(即给它的经纬度点转成向量)

我现在希望将此库换成 latlon-vectors.js。我要使用以下行 (50-60):

LatLon.prototype.toVector = function() {
var φ = this.lat.toRadians();
var λ = this.lon.toRadians();

// right-handed vector: x -> 0°E,0°N; y -> 90°E,0°N, z -> 90°N
var x = Math.cos(φ) * Math.cos(λ);
var y = Math.cos(φ) * Math.sin(λ);
var z = Math.sin(φ);

return new Vector3d(x, y, z);
};

据我有限的 newby 知识,这似乎是主要对象的一个​​方法:

function LatLon(lat, lon) {
// allow instantiation without 'new'
if (!(this instanceof LatLon)) return new LatLon(lat, lon);

this.lat = Number(lat);
this.lon = Number(lon);
}

我调用这个没有问题,我可以这样做:

position = LatLon(data.latitude, data.longitude);

但这无法实现我的目标并将我的 Lat、Lon 点转换为矢量。我如何继续拨打上述线路 (50-60)?

查看 latlon-vectors.js 中的代码,为了将 lat/lon 转化为向量,您需要调用:

var position = LatLon(data.latitude, data.longitude);
var vector = position.toVector();