将一个图书馆换成另一个图书馆

时间:2015-04-27 08:08:08

标签: javascript three.js

这里相对简单的任务,但看到我正在处理面向对象的编程,这让我很困惑。我目前正在使用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);
};

对于我有限的新知识,这似乎是主要对象的一种方法:

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)?

1 个答案:

答案 0 :(得分:0)

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

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