Showing posts with label Trigonometry. Show all posts
Showing posts with label Trigonometry. Show all posts

Saturday, May 31, 2008

Degrees and Radians

When you were learning trigonometry, you probably measured angles in degrees. But in calculus, angles are measured in radians (search for "degrees vs. radians" on Google for the reasons). Being able to convert between these two units might be handy. PHP provides two functions for this purpose, but no such luck in ASP. As usual, we're going to write our own.


Radians is heavily based on my favorite number, pi. We're going to need this number in our calculations, so make sure to define a constant for it.


Const M_PI = 3.14159265358979323846


And now the functions themselves...


  1. function deg2rad(x)
  2.     deg2rad = x * M_PI / 180
  3. end function
  4. function rad2deg(x)
  5.     rad2deg = x * 180 / M_PI
  6. end function

View this code on Snipplr

Monday, February 18, 2008

Trigonometry, Part 3

I was hoping to publish every Saturday, but this past Saturday I got sidetracked. So without further ado, I bring you the next installment in the trigonometry series of articles.


Each of the six basic trigonometric functions has an inverse function. The inverse functions for sine, cosine, and tangent (arcsine, arcosine, and arctangent, respectively) are already defined natively in PHP. In ASP, only the inverse function for tangent (arctangent) is defined, but we will create an adapter for it to be consistent with the abbreviation in PHP. We also need to define some constants that are already present in PHP but not in ASP.


ASP

  1. Const M_PI = 3.14159265358979323846
  2. Const M_PI_2 = M_PI / 2
  3. function asin(x)
  4.     asin = atn(x / sqr(1 - x ^ 2))
  5. end function
  6. function acos(x)
  7.     acos = M_PI_2 - asin(x)
  8. end function
  9. function atan(x)
  10.     atan = atn(x)
  11. end function

As you can probably guess, the inverse functions for cosecant, secant, and cotangent are arccosecant, arcsecant, and arccotangent.


ASP

  1. function acsc(x)
  2.     acsc = asin(1 / x)
  3. end function
  4. function asec(x)
  5.     asec = M_PI_2 - acsc(x)
  6. end function
  7. function acot(x)
  8.     acot = M_PI_2 - atn(x)
  9. end function

PHP

  1. function acsc($x)
  2. {
  3.     return asin(1 / $x);
  4. }
  5. function asec($x)
  6. {
  7.     return (M_PI_2 - acsc($x));
  8. }
  9. function acot($x)
  10. {
  11.     return (M_PI_2 - atan($x));
  12. }

Next time we'll do something different for a change. I promise.

Saturday, February 9, 2008

Trigonometry, Part 2

As promised, this week we're building some special trigonometry functions using the functions we wrote last week: versed sine (or versine), coversed sine (or coversine), haversed sine (or haversine), hacoversed sine (or hacoversine), exsecant, and excosecant. Actually, to be honest, only two of those build on the functions from last week.


ASP

  1. function versin(x)
  2.     versin = 1 - cos(x)
  3. end function
  4. function coversin(x)
  5.     coversin = 1 - sin(x)
  6. end function
  7. function haversin(x)
  8.     haversin = versin(x) / 2
  9. end function
  10. function hacoversin(x)
  11.     hacoversin = coversin(x) / 2
  12. end function
  13. function exsec(x)
  14.     exsec = sec(x) - 1
  15. end function
  16. function excsc(x)
  17.     excsc = csc(x) - 1
  18. end function

PHP

  1. function versin($x)
  2. {
  3.     return (1 - cos($x));
  4. }
  5. function coversin($x)
  6. {
  7.     return (1 - sin($x));
  8. }
  9. function haversin($x)
  10. {
  11.     return (versin($x) / 2);
  12. }
  13. function hacoversin($x)
  14. {
  15.     return (coversin($x) / 2);
  16. }
  17. function exsec($x)
  18. {
  19.     return (sec($x) - 1);
  20. }
  21. function excsc($x)
  22. {
  23.     return (csc($x) - 1);
  24. }

Next week, the fun continues with inverse trigonometric functions.

Saturday, February 2, 2008

Trigonometry, Part 1

Both ASP and PHP contain three basic trigonometry functions: sin(), cos(), and tan(). You may remember from school that there are three more basic trigonometry functions: cosecant, secant, and cotangent. It might be handy to have some functions for these too.


ASP

  1. function csc(x)
  2.     csc = 1 / sin(x)
  3. end function
  4. function sec(x)
  5.     sec = 1 / cos(x)
  6. end function
  7. function cot(x)
  8.     cot = 1 / tan(x)
  9. end function

PHP

  1. function csc($x)
  2. {
  3.     return (1 / sin($x));
  4. }
  5. function sec($x)
  6. {
  7.     return (1 / cos($x));
  8. }
  9. function cot($x)
  10. {
  11.     return (1 / tan($x));
  12. }

Next week we'll build off some of these basic functions to make some special functions.