Trigonometry Functions

Revision as of 17:44, 6 May 2006 by imported>DragoonWraith

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

There are many methods of obtaining any or all of these, and each has its own benefits and drawbacks in terms of length, efficiency, and speed. This page is a work in progress - please add any scripts you have for approximating these functions. Please remember to comment them thoroughly and to use generic variable names.


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.

Arcsine, Arccosine, and Arctangent

According to Wikipedia, arcsine can be found with this series:

 

This can be approximated by Oblivion using the following script:

scriptname Arcsine
;script originally supplied by DragoonWraith

float z
float z3
float z5
float z7
float arcsinz

Begin {appropriate blocktype}

;z can be set depending on implementation
;here it is set to the number of places discovered by the player
;(a completely bizarre thing to find the arcsine of)
  set z to GetPCMiscStat 7
  set z3 to ( z * z * z )
  set z5 to ( z3 * z * z )
  set z7 to ( z5 * z * z )

  set arcsinz to ( z + (1/2)*(z3/3) + (3/8)*(z5/5) + (15/48)*(z7/7) )

End

More terms will increase accuracy. Remember than sin=opp/hyp - the only way you are actually going to need this is if you know the opposite side of the triangle and the hypotenuse.

See Also

Elder Scrolls Forums - Trigonometry Function Workarounds?