The Java Script Math object provides the trigonometric functions cosine, sine, tangent, along with many others. All the trigonometric functions return an angular value in radians. Why use radians rather than degrees?
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

SurveyClub

Java Script Trigonometric Methods

The Java Script Math object provides the trigonometric functions cosine, sine, tangent, along with many others. All the trigonometric functions return an angular value in radians. Why use radians rather than degrees?

The 360 degree rotation angle of a circle is totally fabricated by man and is cumbersome to use in mathematical calculations. Radians are in tune with nature as 2 pi radians rotates an arc in a complete circle. Radians make it possible to use algebra to manipulate geometric shapes in Cartesian space (named after the French man Rene Descartes who came up with it while dabbling in, among other things, mathematics). To put it another way, we probably wouldn't have 3D games or computer generated movies without radians.

If you still need to work with degrees, the functions shown below can be used to convert radians to degrees and degrees to radians.

function rad2deg(radians)
{
   degrees = 360 * radians/(2 * Math.PI);
   return degrees;
}

function deg2rad(degrees)
{
   radians = (2 * Math.PI * degrees)/360;
   return radians;
}

As an example, lets use the Math..tan function to solve a common trigonometry problem. We want to determine the height of a structure. We setup a surveyors telescope 100 meters from the base of the structure and rotate the telescope 33.8 degrees to point at the peak of the structure. We can use the basic trigonometric equation shown below.

tan = opposite/adjacent

For our problem we need to rearrange the equation to that shown below.

height = tan 33.8 degrees * 100;

Shown below is the code required to solve the problem.

var radians = (2 * Math.PI * 33.8)/360;
var tan = Math.tan(radians);
var height = tan * 100;
document.write(height);

The first line of code converts degrees to radians for use with the Math.tan function. The second line returns the tangent of the angle. Since the tangent is the ratio of the height of the object to the distance between the surveyors telescope and the structure, multiplying this distance by the tangent of the angle will provide us with the height of the object. The last line of code displays the height of the structure (66.9 meters).

The JavaScript Math object gives you powerful routines that let you generate random numbers, convert numbers to strings and strings to numbers, and perform trigonometric functions and much more. After this introduction, you should be able to create some interesting JavaScript applications.

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269