Skip to content

Commit

Permalink
call Math.asin() and Math.acos() safely,#20
Browse files Browse the repository at this point in the history
  • Loading branch information
iSpring committed Nov 30, 2016
1 parent 035e241 commit 0ce8a06
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/world/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const EventModule = {
var pickResult = Kernel.globe.camera.getPickCartesianCoordInEarthByCanvas(this.previousX, this.previousY);
if (pickResult.length > 0) {
this.dragGeo = MathUtils.cartesianCoordToGeographic(pickResult[0]);
console.log("单击点三维坐标:(" + pickResult[0].x + "," + pickResult[0].y + "," + pickResult[0].z + ");经纬度坐标:[" + this.dragGeo[0] + "," + this.dragGeo[1] + "]");
//console.log("单击点三维坐标:(" + pickResult[0].x + "," + pickResult[0].y + "," + pickResult[0].z + ");经纬度坐标:[" + this.dragGeo[0] + "," + this.dragGeo[1] + "]");
}
this.canvas.addEventListener("mousemove", this.onMouseMoveListener, false);
},
Expand Down
21 changes: 12 additions & 9 deletions src/world/math/Math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ const MathUtils = {
if(value > 1){
value = 1;
}
if(value < -1){
value = -1;
}
return Math.asin(value);
},

acosSafely(value: number){
if(value > 1){
value = 1;
}
if(value < -1){
value = -1;
}
return Math.acos(value);
},

Expand Down Expand Up @@ -375,26 +381,23 @@ const MathUtils = {
var y = verticeCopy.y;
var z = verticeCopy.z;
var sin2 = y / Kernel.EARTH_RADIUS;
if(sin2 > 1){
sin2 = 2;
}else if(sin2 < -1){
sin2 = -1;
}
var radianLat = Math.asin(sin2);
var radianLat = this.asinSafely(sin2);
var cos2 = Math.cos(radianLat);
var sin1 = x / (Kernel.EARTH_RADIUS * cos2);
if(sin1 > 1){
sin1 = 1;
}else if(sin1 < -1){
}
if(sin1 < -1){
sin1 = -1;
}
var cos1 = z / (Kernel.EARTH_RADIUS * cos2);
if(cos1 > 1){
cos1 = 1;
}else if(cos1 < -1){
}
if(cos1 < -1){
cos1 = -1;
}
var radianLog = Math.asin(sin1);
var radianLog = this.asinSafely(sin1);
if(sin1 >= 0){
//经度在[0,π]
if(cos1 >= 0){
Expand Down

0 comments on commit 0ce8a06

Please sign in to comment.