spacer
Yehuda Shiran July 12, 2000
Animation with Math.cos()
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

JavaScript supports the important trigonometric methods: cos(), acos(), sin(), asin(), tang(), atan(), and atan2(). All angles in JavaScript are measured in radians. You should know how to convert an angle from degrees to radians and vice versa. Check your conversion with the following table:

Degrees

Radians

0

0

90

0.5*PI

180

PI

270

1.5*PI

360

2*PI

where PI is the mathematical constant equal to 3.14... and 360 degrees are equal to 6.28... This tip covers the two cosine-related methods.

cos()

The Math.cos() method accepts one argument (an angle in radians) and returns its cosine value. The following statement prints "-1":

document.write(Math.cos(Math.PI));

The Math.cos() method is useful in animation applications. If an object A revolves around object B in a constant-radius orbit, its y location can be derived by multiplying the orbiting radius by the cosine of the angle that the radius makes with the upright ray.

acos()

The Math.acos() method accepts one argument and returns the arc cosine (the angle that the given number is its cosine value) of the argument, in radians. The following statement prints the value of PI:

document.write(Math.acos(-1));


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark