Radians Versus Degrees

A radian is the ratio of an arc's length to its radius. See the wiki diagram below


Radians tend to be used in most trig formulas related to distance calculation, so we are definitely going to need a couple of functions to convert back and forwards to degrees, since mapping software is typically concerned with degrees of latitude and longtitude. Luckily the formula is very simple.

VBA

Public Function toRadians(deg)
    toRadians = Application.WorksheetFunction.Pi / 180 * deg
End Function
Public Function fromRadians(rad) As Double
    'convert radians to degress
    fromRadians = 180 / Application.WorksheetFunction.Pi * rad
End Function

Google Apps Script

/**
 * convert degrees to Radians
 * @param {number} deg degrees to convert
 * @return {number} eqivalent radians
 */
function toRadians(deg) {
      return Math.PI() / 180 * deg;
}
/**
 * convert  Radians to degrees
 * @param {rad} rad radians to convert
 * @return {number} eqivalent degrees
 */
function fromRadians(rad) {
      return 180/Math.PI() * rad;
}

Earth Radius

Another thing we are going to need to know is the earth's radius, so lets create a function to return that consistently. The earth is not completely spherical, so I use the mean radius here - this is the value usually used for these kind of calculations.

VBA
Public Function earthRadius() As Double
    ' earth radius in km.
    earthRadius = 6371
End Function

Google Apps Script
 function earthRadius() {
   return 6371.0;
 }

Take a look at One Liners for more tips like this. In the meantime why not join our forum,follow the blog or follow me on twitter to ensure you get updates when they are available. You can download this example and all other code in the cDataSet.xlsm from Downloads 


You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, available All formats are available now from O'Reilly,Amazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.




Comments