View Full Version : arccos function


jmac698
26th October 2011, 10:53
Hi,
I need an accurate arccos function in avisynth script. I tried the one from.. can't remember it's name, but it's not accurate enough and is limiting my application.
I'm not setup for plugin development either; I hate to get sidetracked for an hour just to write a plugin for one function - argh. If anyone is setup for development, would you consider writing a arcsin, arccos, and atan2 functions? It should be 5 minutes for you :) I really need it to finish researchiing my software TBC.

Anyhow, here's the inaccurate function:

Function __arc_sin(float x, float "__prev", int "__step", float "__pterm")
{
__prev = Default(__prev, x)
__pterm = Default(__pterm, x)
__step = Default(__step, 1) # step 0 is introduced by defaults
st2 = 2 * __step
t = st2 - 1
term = __pterm * (Float(t * t) / Float(st2 * (st2 + 1))) * (x * x)
return Abs(term) > 0.00001 \
? __arc_sin(x, __prev + term, __step + 1, term) \
: __prev + term
}

Function ArcSin(float x) { return __arc_sin(x) }

Function ArcCos(float x) { return (Pi/2) - ArcSin(x) }

Thanks.

Gavino
26th October 2011, 12:07
Here you go.

Includes the functions:
Acos(float)
Asin(float)
Atan(float)
Atan2(float, float)

They are simply wrappers for the corresponding C functions, so see the relevant C documentation for details.

jmac698
26th October 2011, 14:12
:thanks:
That template will be really useful. With the recent minmax plugin I should be able to quickly write a few things, when I can get vcexpress setup.
Wait until you see my upcoming "perfect TBC" it's quite amazing, subpixel accuracy to 1/5 pixel but with new arccos function, even 1/100 pixel :)

jmac698
26th October 2011, 14:16
For avisynth news:
New plugin ArcFuns
Name: ArcFuns
Version: 1.0
Author: Gavino
Description: This plugin expands the available numerical functions with missing inverse trig functions.
Usage:
acos(float arg)
asin(float arg)
atan(float arg)
atan2(float y, float x)
Each perfoms the calculation as in the C language and returns a float value.
Atan2 is the angle of the point (x,y) from the line y=0, and is positive for anticlockwise. It can be quite useful for calculating hue, for example, from U, V.

Gavino
26th October 2011, 23:22
atan2(float x, float y)
Should be atan2(float y, float x) to be consistent with the rest of the description:
Atan2 is the angle of the point (x,y) from the line x=0, and is positive for anticlockwise.

jmac698
27th October 2011, 00:05
To be consistent with C, yes. I don't see how the description implies any order to the arguments in the function in itself; it's really the C function that determined it. Anyhow I don't see any sense of making it y,x in the first place.

cretindesalpes
27th October 2011, 00:31
Same order as the quotient tan = sin / cos?

jmac698
27th October 2011, 01:47
It's in y, x order in math books, who knows why they have this convention? Something historical or maybe they have a pattern to it.

Gavino
27th October 2011, 10:03
Actually, it's still not right - I've just spotted something else....
[After editing]
atan2(float y, float x)
Atan2 is the angle of the point (x,y) from the line x=0, and is positive for anticlockwise.
It should be from the line y=0, ie the x-axis.

An alternative description which works is:
atan2(float x, float y)
Atan2 is the angle of the point (x,y) from the line x=0 (the y-axis), and is positive for clockwise.
But this is less conventional.

jmac698
27th October 2011, 10:22
You have a great eye for detail! Maybe that's why you're such a good programmer :)