private float HighPrecisionTerrainHeight(float meterx, float meterz, bool isLatlon) { double finalvalue = 0.0; Vector2 c; if (!isLatlon) { Geography proj = new Geography(); c = proj.meterstoLatLonDouble(meterx, meterz); } else { c = new Vector2(meterx, meterz); } double ratioX = Math.Ceiling(scenebbox.top) - c.x; double ratioZ = c.y - Math.Floor(scenebbox.left); double Xindex = (1200.0 * ratioX); double Zindex = (1200.0 * ratioZ); //Upper Triangle if ((Xindex - Math.Floor(Xindex)) + (Zindex - Math.Floor(Zindex)) < 1.0f) { int n = heightmap.heightmap[(int)Math.Floor(Xindex), (int)Math.Ceiling(Zindex)]; int m = heightmap.heightmap[(int)Math.Floor(Xindex), (int)Math.Floor(Zindex)]; int k = heightmap.heightmap[(int)Math.Ceiling(Xindex), (int)Math.Floor(Zindex)]; double interpolationUp = m + (n - m) * (Zindex - Math.Floor(Zindex)); double interpolationHypothenus = k + (n - k) * (Zindex - Math.Floor(Zindex)); double a = Xindex - Math.Floor(Xindex); double aplusb = Math.Ceiling(Zindex) - Zindex; finalvalue = interpolationUp + (interpolationHypothenus - interpolationUp) * (a / aplusb); } //Lower Triangle else if ((Xindex - Math.Floor(Xindex)) + (Zindex - Math.Floor(Zindex)) >= 1.0f) { int n = heightmap.heightmap[(int)Math.Ceiling(Xindex), (int)Math.Ceiling(Zindex)]; int m = heightmap.heightmap[(int)Math.Ceiling(Xindex), (int)Math.Floor(Zindex)]; int k = heightmap.heightmap[(int)Math.Floor(Xindex), (int)Math.Ceiling(Zindex)]; double interpolationDown = m + (n - m) * (Zindex - Math.Floor(Zindex)); double interpolationHypothenus = m + (k - m) * (Zindex - Math.Floor(Zindex)); double a = Math.Ceiling(Xindex) - Xindex; double aplusb = Zindex - Math.Floor(Zindex); finalvalue = interpolationDown + (interpolationHypothenus - interpolationDown) * (a / aplusb); if ((Xindex - Math.Floor(Xindex)) + (Zindex - Math.Floor(Zindex)) > 0.99 && (Xindex - Math.Floor(Xindex)) + (Zindex - Math.Floor(Zindex)) < 1.01) { return((float)interpolationHypothenus); } } return((float)finalvalue); }