Difference between revisions of "ESM Math Library"

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search
imported>JustTim
(fixed a bug in stage 21)
imported>JustTim
(YALU - Yet Another Layout Update)
Line 3: Line 3:
If you've written such a function and you think it might be helpful to others, don't hesitate to contribute to this repository.
If you've written such a function and you think it might be helpful to others, don't hesitate to contribute to this repository.


=== First Steps ===
=== Step by Step ===
To get this up and running follow there simple setup steps:
To get this up and running follow there simple setup steps:
* Open any Plugin you wish or create a new one with the Construction Set
* Open any Plugin you wish or create a new one with the Construction Set
Line 87: Line 87:
   endif
   endif
End</pre>
End</pre>
This Quest Script already contains everything needed for the following functions.


=== Stage 5: Square Root ===
=== Stage 5: Square Root ===
Based on [[Square_Root|Square Root]] Article
Based on [[Square_Root|Square Root]] Article
Code to include in FunctionQuestScript:
<pre>;S5 FUNCTION sqrt
float sqr</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float sqrt(float input)
<pre>;FUNCTION float sqrt(float input)
if (f.fin1 <= 0)
if (f.fin1 <= 0)
Line 102: Line 111:
   set f.fout to f.sqr
   set f.fout to f.sqr
endif</pre>
endif</pre>
Usage in another script:
<pre>;CALL float sqrt(float input)
set f.fin1 to myVarForInput
setStage f 5
set myResult to f.fout</pre>


=== Stage 10: Hypotenuse ===
=== Stage 10: Hypotenuse ===
Code to include in FunctionQuestScript:
<pre>;S10 FUNCTION Hypotenuse
float n</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float Hypotenuse(float CathetusA, float CathetusB)
<pre>;FUNCTION float Hypotenuse(float CathetusA, float CathetusB)


set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
;FUNCTION float sqrt(float input)
;CALL float sqrt(float input)
set f.fin1 to f.n
set f.fin1 to f.n
setStage f 5
setStage f 5
;fout is already the result</pre>
;fout is already the result</pre>
Usage in another script:
<pre>;CALL float Hypotenuse(float CathetusA, float CathetusB)
set f.fin1 to myVarForCathetusA
set f.fin2 to myVarForCathetusB
setStage f 10
set myResult to f.fout</pre>


=== Stage 15: Sin Cos Tan ===
=== Stage 15: Sin Cos Tan ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code to include in FunctionQuestScript:
<pre>;S15 FUNCTION SinCosTan
float ang
float sin
float cos
float exp</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float,float,float SinCosTan(float Angle)
<pre>;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 1 - script by Galerion
;Taylor Series Variant 1 - script by Galerion
Line 150: Line 187:
set f.fout2 to f.cos
set f.fout2 to f.cos
set f.fout3 to (f.sin/f.cos) ;tan</pre>
set f.fout3 to (f.sin/f.cos) ;tan</pre>
Usage in another script:
<pre>;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 15
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3</pre>


=== Stage 16: Sin Cos Tan 2 ===
=== Stage 16: Sin Cos Tan 2 ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code to include in FunctionQuestScript:
<pre>;S16 FUNCTION SinCodTan 2
float ang
float sin
float cos
float t1
float t2
float t5
float t6</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float,float,float SinCosTan(float Angle)
<pre>;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 2 - script by JOG
;Taylor Series Variant 2 - script by JOG
Line 174: Line 232:
set f.fout2 to f.cos
set f.fout2 to f.cos
set f.fout3 to f.sin/f.cos ;tan</pre>
set f.fout3 to f.sin/f.cos ;tan</pre>
Usage in another script:
<pre>;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 16
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3</pre>


=== Stage 20: Arcsine ===
=== Stage 20: Arcsine ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code to include in FunctionQuestScript:
<pre>;S20 FUNCTION Arcsine
float sin
float ang
float n</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float Arcsine(float sin)
<pre>;FUNCTION float Arcsine(float sin)
;Abramowitz/Stegun Approximation - script by JOG
;Abramowitz/Stegun Approximation - script by JOG
Line 189: Line 264:


set f.fout to f.rad*(1.5707963 - f.n*(1.5707288 - 0.2121144*f.sin+0.0742610*f.sin*f.sin - 0.0187293*f.sin*f.sin*f.sin))</pre>
set f.fout to f.rad*(1.5707963 - f.n*(1.5707288 - 0.2121144*f.sin+0.0742610*f.sin*f.sin - 0.0187293*f.sin*f.sin*f.sin))</pre>
Usage in another script:
<pre>;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 20
set myResult to f.fout</pre>


=== Stage 21: Arcsine 2 ===
=== Stage 21: Arcsine 2 ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code to include in FunctionQuestScript:
<pre>;S21 FUNCTION Arcsine 2
float t3
float t5
float t7</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float Arcsine(float sin)
<pre>;FUNCTION float Arcsine(float sin)
;Approximation by Taylor Series - script by DragoonWraith
;Approximation by Taylor Series - script by DragoonWraith
Line 200: Line 290:


set f.fout to (f.fin1 + (1/2)*(f.t3/3) + (3/8)*(f.t5/5) + (15/48)*(f.t7/7) )</pre>
set f.fout to (f.fin1 + (1/2)*(f.t3/3) + (3/8)*(f.t5/5) + (15/48)*(f.t7/7) )</pre>
Usage in another script:
<pre>;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 21
set myResult to f.fout</pre>


=== Stage 22: Aroccosine ===
=== Stage 22: Aroccosine ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code for the Stage Result Script:
<pre>;FUNCTION float Arccosine(float cos)
<pre>;FUNCTION float Arccosine(float cos)
setStage f 20 ;Call Arcsine with same input
setStage f 20 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)</pre>
set f.fout to ((f.pi / 2) - f.fout)</pre>
Usage in another script:
<pre>;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 22
set myResult to f.fout</pre>


=== Stage 23: Arccosine 2 ===
=== Stage 23: Arccosine 2 ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code for the Stage Result Script:
<pre>;FUNCTION float Arccosine(float cos)
<pre>;FUNCTION float Arccosine(float cos)
setStage f 21 ;Call Arcsine with same input
setStage f 21 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)</pre>
set f.fout to ((f.pi / 2) - f.fout)</pre>
Usage in another script:
<pre>;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 23
set myResult to f.fout</pre>


=== Stage 24: Arctan ===
=== Stage 24: Arctan ===
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Based on [[Trigonometry_Functions|Trigonometry Functions]] Article
Code to include in FunctionQuestScript:
<pre>;S24 FUNCTION Arctan
float t3
float t5
float t7</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float Arctan(float tan)
<pre>;FUNCTION float Arctan(float tan)
;Approximation by Taylor Series - script by DragoonWraith
;Approximation by Taylor Series - script by DragoonWraith
Line 223: Line 345:


set f.fout to (f.fin1 - (f.t3/3) + (f.t5/5) - (f.t7/7))</pre>
set f.fout to (f.fin1 - (f.t3/3) + (f.t5/5) - (f.t7/7))</pre>
Usage in another script:
<pre>;CALL float Arctan(float tan)
set f.fin1 to myVarForTan
setStage f 24
set myResult to f.fout</pre>


=== Stage 30: getAngle ===
=== Stage 30: getAngle ===
Code to include in FunctionQuestScript:
<pre>;S30 FUNCTION getAngle
float tan
float ang</pre>
Code for the Stage Result Script:
<pre>;FUNCTION float getAngle(float x, float y)
<pre>;FUNCTION float getAngle(float x, float y)


Line 243: Line 377:
endif
endif


;CALL float Arctan(float tan)
set f.fin1 to f.tan
set f.fin1 to f.tan
setStage f 24 ;Call Arctan
setStage f 24


if f.fout > (f.pi/2)
if f.fout > (f.pi/2)
Line 252: Line 387:
endif
endif
set f.fout to ((f.fout*f.rad) + f.ang)</pre>
set f.fout to ((f.fout*f.rad) + f.ang)</pre>
Usage in another script:
<pre>;CALL float getAngle(float x, float y)
set f.fin1 to myVarForX
set f.fin2 to myVarForY
setStage f 30
set myResultAngle to f.fout</pre>


[[Category: Scripting]]
[[Category: Scripting]]
[[Category: Useful Code]]
[[Category: Useful Code]]

Revision as of 16:43, 9 May 2006

Welcome

to the Stage Function Repository! The goal of the Repository is, to create a huge database of available functions all using the method described in the article Simulating new functions. If you've written such a function and you think it might be helpful to others, don't hesitate to contribute to this repository.

Step by Step

To get this up and running follow there simple setup steps:

  • Open any Plugin you wish or create a new one with the Construction Set
  • Create a new Quest called "f" (yes, just the letter "f", nothing more)
  • Activate the Checkbox "Allow repeated stages". (This is VERY important!!)
  • Create a new Quest Script and copy the whole content of the "Quest Script" section from this article into this script. Don't forget to attach it to your f-Quest!
  • Create a new stage in this quest for each function you want to use from this repository and copy all code from it's section in this article to the related Result Script textbox. Make sure to select the right stage number for the function.
  • That's it! You should be able to use them now!

Quest Script

ScriptName FunctionQuestScript

; Internals
short doOnce
float fQuestDelayTime

; Constants
float pi
float rad

; Function In- and Output
float fin1
float fin2
float fin3
float fout
float fout2
float fout3

;S5 FUNCTION sqrt
float sqr

;S10 FUNCTION Hypotenuse
;float sqr
float n

;S15 FUNCTION SinCosTan 1
float ang
float sin
float cos
float tan
float exp

;S16 FUNCTION SinCodTan 2
;float ang
;float sin
;float cos
float t1
float t2
float t5
float t6

;S20 FUNCTION Arcsine 1
;float sin
;float ang
;float n

;S21 FUNCTION Arcsine 2
float t3
;float t5
float t7

;S22 FUNCTION Arccosine 1

;S23 FUNCTION Arccosine 2

;S24 FUNCTION Arctan
;float t3
;float t5
;float t7

;S30 FUNCTION getAngle
float tan
;float ang

; Set constants first
Begin Gamemode
  if doOnce == 0
    set pi to 3.1415927
    set rad to 180.0/pi

    set fQuestDelayTime to 30
    set doOnce to 1
  endif
End

This Quest Script already contains everything needed for the following functions.


Stage 5: Square Root

Based on Square Root Article


Code to include in FunctionQuestScript:

;S5 FUNCTION sqrt
float sqr

Code for the Stage Result Script:

;FUNCTION float sqrt(float input)
if (f.fin1 <= 0)
  set f.fout to 0
else
  set f.sqr to f.fin1/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.fout to f.sqr
endif

Usage in another script:

;CALL float sqrt(float input)
set f.fin1 to myVarForInput
setStage f 5
set myResult to f.fout

Stage 10: Hypotenuse

Code to include in FunctionQuestScript:

;S10 FUNCTION Hypotenuse
float n

Code for the Stage Result Script:

;FUNCTION float Hypotenuse(float CathetusA, float CathetusB)

set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
;CALL float sqrt(float input)
set f.fin1 to f.n
setStage f 5
;fout is already the result

Usage in another script:

;CALL float Hypotenuse(float CathetusA, float CathetusB)
set f.fin1 to myVarForCathetusA
set f.fin2 to myVarForCathetusB
setStage f 10
set myResult to f.fout

Stage 15: Sin Cos Tan

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S15 FUNCTION SinCosTan
float ang
float sin
float cos
float exp

Code for the Stage Result Script:

;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 1 - script by Galerion

set f.ang to f.fin1

if f.ang < -180
  set f.ang to (f.ang + 360)
elseif f.ang > 180
  set f.ang to (f.ang - 360)
endif

;approximate
set f.ang to (f.ang/f.rad)
set f.cos to 1
set f.exp to f.ang
set f.sin to f.exp
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos - f.exp / 2)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin - f.exp / 6)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos + f.exp / 24)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin + f.exp / 120)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos - f.exp / 720)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin - f.exp / 5040)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos + f.exp / 40320)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin + f.exp / 362880)

set f.fout to f.sin
set f.fout2 to f.cos
set f.fout3 to (f.sin/f.cos) ;tan

Usage in another script:

;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 15
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3

Stage 16: Sin Cos Tan 2

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S16 FUNCTION SinCodTan 2
float ang
float sin
float cos
float t1
float t2
float t5
float t6

Code for the Stage Result Script:

;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 2 - script by JOG

set f.ang to f.fin1

if f.ang < -180
  set f.ang to f.ang + 360
elseif f.ang > 180
  set f.ang to f.ang - 360
endif

set f.t1 to (f.ang/f.rad)
set f.t2 to (f.t1*f.t1)
set f.t5 to (f.t2*f.t2*f.t1)
set f.t6 to (f.t5*f.t1)
set f.sin to (f.t1 - (f.t1*f.t2/6) + (f.t5/120) - (f.t5*f.t2/5040) + (f.t6*f.t2*f.t1/362880))
set f.cos to (1 - (f.t2/2) + (f.t2*f.t2/24) - (f.t6/720) + (f.t6*f.t2/40320))

set f.fout to f.sin
set f.fout2 to f.cos
set f.fout3 to f.sin/f.cos ;tan

Usage in another script:

;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 16
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3

Stage 20: Arcsine

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S20 FUNCTION Arcsine
float sin
float ang
float n

Code for the Stage Result Script:

;FUNCTION float Arcsine(float sin)
;Abramowitz/Stegun Approximation - script by JOG

set f.sin to f.fin1
set f.n to 1 - f.sin
set f.ang to f.n/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.n to (f.ang+(f.n/f.ang))/2

set f.fout to f.rad*(1.5707963 - f.n*(1.5707288 - 0.2121144*f.sin+0.0742610*f.sin*f.sin - 0.0187293*f.sin*f.sin*f.sin))

Usage in another script:

;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 20
set myResult to f.fout

Stage 21: Arcsine 2

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S21 FUNCTION Arcsine 2
float t3
float t5
float t7

Code for the Stage Result Script:

;FUNCTION float Arcsine(float sin)
;Approximation by Taylor Series - script by DragoonWraith

set f.t3 to (f.fin1 * f.fin1 * f.fin1)
set f.t5 to (f.t3 * f.fin1 * f.fin1)
set f.t7 to (f.t5 * f.fin1 * f.fin1)

set f.fout to (f.fin1 + (1/2)*(f.t3/3) + (3/8)*(f.t5/5) + (15/48)*(f.t7/7) )

Usage in another script:

;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 21
set myResult to f.fout

Stage 22: Aroccosine

Based on Trigonometry Functions Article


Code for the Stage Result Script:

;FUNCTION float Arccosine(float cos)
setStage f 20 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)

Usage in another script:

;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 22
set myResult to f.fout

Stage 23: Arccosine 2

Based on Trigonometry Functions Article


Code for the Stage Result Script:

;FUNCTION float Arccosine(float cos)
setStage f 21 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)

Usage in another script:

;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 23
set myResult to f.fout

Stage 24: Arctan

Based on Trigonometry Functions Article

Code to include in FunctionQuestScript:

;S24 FUNCTION Arctan
float t3
float t5
float t7

Code for the Stage Result Script:

;FUNCTION float Arctan(float tan)
;Approximation by Taylor Series - script by DragoonWraith

set f.t3 to (f.fin1 * f.fin1 * f.fin1)
set f.t5 to (f.t3 * f.fin1 * f.fin1)
set f.t7 to (f.t5 * f.fin1 * f.fin1)

set f.fout to (f.fin1 - (f.t3/3) + (f.t5/5) - (f.t7/7))

Usage in another script:

;CALL float Arctan(float tan)
set f.fin1 to myVarForTan
setStage f 24
set myResult to f.fout

Stage 30: getAngle

Code to include in FunctionQuestScript:

;S30 FUNCTION getAngle
float tan
float ang

Code for the Stage Result Script:

;FUNCTION float getAngle(float x, float y)

set f.tan to (f.fin1/f.fin2)
if f.tan >1 || f.tan < -1
  set f.tan to (f.fin2/ -f.fin1)
  if f.fin1 >= 0
    set f.ang to 90
  else
    set f.ang to -90
  endif
else
  if f.fin2 >= 0
    set f.ang to 0
  else
    set f.ang to 180
  endif
endif

;CALL float Arctan(float tan)
set f.fin1 to f.tan
setStage f 24

if f.fout > (f.pi/2)
  set f.fout to (f.pi/2)
elseif f.fout < (f.pi/ -2)
  set f.fout to (f.pi/ -2)
endif
set f.fout to ((f.fout*f.rad) + f.ang)

Usage in another script:

;CALL float getAngle(float x, float y)
set f.fin1 to myVarForX
set f.fin2 to myVarForY
setStage f 30
set myResultAngle to f.fout