Design & Download
Your JS Library
in Seconds!!!

Snippet: Simple Trigonometric Functions

Written by Christopher West (cwest) on November 23, 2015.
Simple trigonometric functions with the option to specify the value in degrees or radians: cosine, sine, tangent, cosecant, contangent, and secant.
'seccos;cscsin;cottan;'.replace(/(...)(?=(...)?;)/g, function(m, name, base, fn) {
  YourJS[name] = Function('a','b',fn = 'return ' + (base ? '1/' : '') + 'Math.' + (base || name) + '(b?a*'+Math.PI/180+':a);');
});

In JavaScript we already have Math.cos(), Math.sin() and Math.tan(). What we don't have is Math.sec(), Math.csc() and Math.cot(). I'm guessing the reason those reciprocal functions are not included is because all that is required is to literally find the reciprocal of their identical functions. Still, it would be nice to have them, right? Also, it would be nice to specify for all of these functions whether or not to interpret the passed in argument as degrees or radians. Having all those in mind, I wrote this snippet which defines:

cos(...) API Documentation

Description

Determines the cosine of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose cosine value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the cosine of angle in radians by default, but if opt_usingDegrees is a true-ish value then the cosine of angle in degrees will be returned.

sin(...) API Documentation

Description

Determines the sine of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose sine value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the sine of angle in radians by default, but if opt_usingDegrees is a true-ish value then the sine of angle in degrees will be returned.

tan(...) API Documentation

Description

Determines the tangent of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose tangent value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the tangent of angle in radians by default, but if opt_usingDegrees is a true-ish value then the tangent of angle in degrees will be returned.

sec(...) API Documentation

Description

Determines the secant of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose secant value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the secant of angle in radians by default, but if opt_usingDegrees is a true-ish value then the secant of angle in degrees will be returned.

csc(...) API Documentation

Description

Determines the cosecant of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose cosecant value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the cosecant of angle in radians by default, but if opt_usingDegrees is a true-ish value then the cosecant of angle in degrees will be returned.

cot(...) API Documentation

Description

Determines the cotangent of a given number either in radians or in degrees.

Parameters

  1. angle {number}:
    The angle whose cotangent value should be returned.
  2. opt_usingDegrees {boolean}:
    Optional. Defaults to false. If true then angle will be interpreted in degrees, otherwise angle will be interpreted in radians.

Returns

Returns the cotangent of angle in radians by default, but if opt_usingDegrees is a true-ish value then the cotangent of angle in degrees will be returned.