Difference between revisions of "Trigonometry Functions"
Jump to navigation
Jump to search
Contents + alternative sin/cos/tan/arcsin routines
imported>DragoonWraith |
imported>JOG (Contents + alternative sin/cos/tan/arcsin routines) |
||
Line 6: | Line 6: | ||
= Sine, Cosine, and Tangent = | = Sine, Cosine, and Tangent = | ||
== Formulae== | |||
The taylor series for the base trigonometry functions are as follows: | |||
'''''sin(x) = x^1/1! - x^3/3! + x^5/5! - x^7/7! + ...''''' | |||
'''''cos(x) = x^0/0! - x^2/2! + x^4/4! - x^6/6! + ...''''' | |||
'''''tan(x) = x^1/1! + x^3/3! + x^5/5! + x^7/7! + ...''''' | |||
== Taylor Series Variant 1 == | |||
ScriptName SineCosineTangent | ScriptName SineCosineTangent | ||
; script originally by Galerion | ; script originally by Galerion | ||
Line 58: | Line 71: | ||
This script is more accurate than it needs to be. Galerion suggests that only the first seven steps are necessary. | This script is more accurate than it needs to be. Galerion suggests that only the first seven steps are necessary. | ||
== Taylor Series Variant 2 == | |||
ScriptName SineCosineTangent | |||
; script originally by JOG | |||
float angle ; Input | |||
float sina ; Output Sin | |||
float cosa ; Output Cos | |||
float cosa ; Output Tan | |||
float t1 | |||
float t2 | |||
float t5 | |||
float t6 | |||
Begin {appropriate blocktype} | |||
set angle to player.getangle z | |||
if angle < -180 | |||
set angle to angle + 360 | |||
elseif angle > 180 | |||
set angle to angle - 360 | |||
endif | |||
set t1 to angle / 57.29577951 ; precalculate powers of "angle" | |||
set t2 to t1*t1 | |||
set t5 to t2*t2*t1 | |||
set t6 to t5*t1 | |||
set sina to t1 - t1*t2/6 + t5/120 - t5*t2/5040 + t6*t2*t1/362880 | |||
set cosa to 1 - t2/2 + t2*t2/24 - t6/720 + t6*t2/40320 | |||
set tana to sina/cosa | |||
end | |||
= Arcsine, Arccosine, and Arctangent = | = Arcsine, Arccosine, and Arctangent = | ||
Sin=Opp/Hyp - The arcsine is the angle between two points, when the distance is the hypotenuse of the triangle and the opposing side is either the x- or the y-difference (whatever is shorter). To get the z-angle (left/right) between two objects in Oblivion you can use the function [[GetHeadingAngle]]. But when you need the x-angle (up/down) you still have to calculate the arcsin. | |||
== Arcsine - Abramowitz/Stegun Approximation == | |||
Abramowitz and Stegun found this polynomal approximation for arcsine: | |||
'''''a0=1.5707288 / a1=-0.2121144 / a2=0.0742610 / a3=-0.0187293''''' | |||
'''''arcsin(x) = 180/Pi * (pi/2 - sqrt(1 - x) * (a0 + a1*x + a2*x^2 + a3*x^3))''''' | |||
scriptname Arcsine | |||
; script originally by JOG | |||
float x ; Input (Sine) | |||
float arcsinx ; Output (Angle) | |||
float n | |||
Begin {appropriate blocktype} | |||
set x to (player.getpos z - NPC.getpos z)/player.getdistance NPC | |||
set n to 1 - x ; arcsinx = SquareRoot(1-x) | |||
set arcsinx to n/2 | |||
set arcsinx to (arcsinx+(n/arcsinx))/2 | |||
set arcsinx to (arcsinx+(n/arcsinx))/2 | |||
set arcsinx to (arcsinx+(n/arcsinx))/2 | |||
set n to (arcsinx+(n/arcsinx))/2 | |||
set arcsinx to 57.2957795*(1.5707963-n*(1.5707288-0.2121144*x+0.0742610*x*x-0.0187293*x*x*x)) | |||
End | |||
This approximation is very accurate (99.9% - 99.9999%) for angles of 45 degrees and higher. For smaller angles you can get a higher precision when you use a taylor series with 15+ iterations. | |||
== Arcsine - Approximation by Taylor Series == | |||
According to [http://www.wikipedia.org Wikipedia], arcsine can be found with this series: | According to [http://www.wikipedia.org Wikipedia], arcsine can be found with this series: | ||
Line 90: | Line 174: | ||
End | End | ||
More terms will increase accuracy. The larger the angle, the more terms are needed to get acceptable accuracy. As a rough guide use 3 terms for every 10° to get an accuracy of at least 1 decimal digit. | |||
= See Also = | |||
[http://www.elderscrolls.com/forums/index.php?showtopic=374963 Elder Scrolls Forums - Trigonometry Function Workarounds?] | [http://www.elderscrolls.com/forums/index.php?showtopic=374963 Elder Scrolls Forums - Trigonometry Function Workarounds?] | ||
[http://www.elderscrolls.com/forums/index.php?showtopic=410923 Elder Scrolls Forums - Is there an actor for the target you are attacking currently?] | |||
[[Category: Extra_Math_Functions]] | [[Category: Extra_Math_Functions]] | ||
[[Category: Useful_Code]] | [[Category: Useful_Code]] |