Trigonometry Functions

Revision as of 16:54, 6 May 2006 by imported>DragoonWraith
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This is a repository of functions used in trigonometry - namely Sine, Cosine, Tangent, Arcsine, Arccosine, and Arctangent.

There are a number of methods for obtaining these, some more accurate and others faster. Here is one for Sine, Cosine, and Tangent.

ScriptName SineCosineTangent
; script originally by Galerion

float ang
float rad
float cos
float sin
float tan
float exp

Begin {appropriate blocktype}

; how one sets ang depends on implementation. Here it is the player's Z rotation
  set ang to Player.GetAngle Z

; normalize angle
  if ( ang < -180 )
    set ang to ( ang + 360 )
  elseif ( ang > 180 )
    set ang to ( ang - 360 )
  endif

; approximate sine and cosine of the angle
  set rad to ( ang * 0.0174533 )
  set cos to 1
  set exp to rad                     ; 1st
  set sin to exp
  set exp to ( exp * rad )           ; 2nd
  set cos to ( cos - exp / 2 )
  set exp to ( exp * rad )           ; 3rd
  set sin to ( sin - exp / 6 )
  set exp to ( exp * rad )           ; 4th
  set cos to ( cos + exp / 24 )
  set exp to ( exp * rad )           ; 5th
  set sin to ( sin + exp / 120 )
  set exp to ( exp * rad )           ; 6th
  set cos to ( cos - exp / 720 )
  set exp to ( exp * rad )           ; 7th
  set sin to ( sin - exp / 5040 )
  set exp to ( exp * rad )           ; 8th
  set cos to ( cos + exp / 40320 )
  set exp to ( exp * rad )           ; 9th
  set sin to ( sin + exp / 362880 )

; get tangent
  set tan to ( sin / cos )

End

This can be placed within your own code or run as a separate script and checked by yours (though the former is probably easier). The sin, cos, and tan variables are your outputs, ang is your input.

This script is more accurate than it needs to be. Galerion suggests that only the first seven steps are necessary.

Note: this is a work in progress. As stated, there are a number of methods for these. Please add any if you have them. Please make them well-commented and use generically named variables.