Skip to content

Instantly share code, notes, and snippets.

@darrinholst
Created October 17, 2008 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrinholst/17498 to your computer and use it in GitHub Desktop.
Save darrinholst/17498 to your computer and use it in GitHub Desktop.
Math.calculateViewerLength = function(start, end, xPixelRatio, yPixelRatio, minimumX, minimumY) {
var x1 = (start.x * xPixelRatio) + minimumX
var y1 = (start.y * yPixelRatio) + minimumY
var x2 = (end.x * xPixelRatio) + minimumX
var y2 = (end.y * yPixelRatio) + minimumY
var majorAxis = 6378137
var minorAxis = 6356752.3142
//Determines the true angle of the point with respect to the earth
var trueAnglept1 = (Math.atan(Math.pow(minorAxis, 2) / (Math.pow(majorAxis, 2)) * Math.tan(y1 * Math.PI / 180))) * 180 / Math.PI
var trueAnglept2 = (Math.atan(Math.pow(minorAxis, 2) / (Math.pow(majorAxis, 2)) * Math.tan(y2 * Math.PI / 180))) * 180 / Math.PI
//Determines the radius of each point to the earth's center
var radiuspt1 = Math.pow(1/((Math.pow(Math.cos(trueAnglept1 * Math.PI / 180),2)/Math.pow(majorAxis,2)) + (Math.pow(Math.sin(trueAnglept1 * Math.PI / 180),2)/Math.pow(minorAxis,2))),.5) + 334.9
var radiuspt2 = Math.pow(1/((Math.pow(Math.cos(trueAnglept2 * Math.PI / 180),2)/Math.pow(majorAxis,2)) + (Math.pow(Math.sin(trueAnglept2 * Math.PI / 180),2)/Math.pow(minorAxis,2))),.5) + 334.9
//Determines the x and y coordinates of the each point with respect to the earth
var xEarthCoorpt1 = radiuspt1 * Math.cos(trueAnglept1 * Math.PI/180)
var yEarthCoorpt1 = radiuspt1 * Math.sin(trueAnglept1 * Math.PI/180)
var xEarthCoorpt2 = radiuspt2 * Math.cos(trueAnglept2 * Math.PI/180)
var yEarthCoorpt2 = radiuspt2 * Math.sin(trueAnglept2 * Math.PI/180)
//Creates a single x and y based on the coordinates above
var xCoor = Math.pow(Math.pow((xEarthCoorpt1 - xEarthCoorpt2),2) + Math.pow((yEarthCoorpt1 - yEarthCoorpt2),2),.5)
var yCoor = 2 * Math.PI * ((((xEarthCoorpt1 + xEarthCoorpt2) / 2)) / 360) * (x1 - x2)
//Computes the distance in feet
var distance = Math.floor(Math.pow(Math.pow(xCoor,2) + Math.pow(yCoor,2), .5) * 100 + .5) / 100
distance = Math.floor(ConvertMeters.toFeet(distance) * 100 + .5) / 100;
return distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment