Activity › Forums › Adobe After Effects Expressions › sin^1 available in expressions?

sin^1 available in expressions?
Posted by Mark Walczak on June 4, 2009 at 4:23 pmHi everyone,
I’m doing a personal exercise in expression writing. I am creating a triangle in AE and have been able to write expressions that are able to find the length of each side, given the (x,y) coordinates of each vertex of the triangle. I am now looking to further complicate things by being able to find the angle of each vertex. Given this, I am wondering if there is a sin^1, cos^1, tan^1 etc. function within AE javascript. Basically, I am trying to use the Law of Cosines to find each angle, but cannot find an inverted cosine function in order to find the angle, given the length of each side.
Make sense? I hope so, ’cause I’m pretty sure I can’t repeat it 😛
Any help would be greatly appreciated!!!
Thanks in advance,
Mark
Jim Lefevre replied 2 years, 8 months ago 4 Members · 10 Replies 
10 Replies

Mark Walczak
June 4, 2009 at 5:03 pmThanks everyone!
Dan, that site looks amazing.
I’m still, however having some issues getting it all to work. Perhaps I’ll post the scene if I can.
Thanks again!
What makes you explode?
http://www.explosivegraffix.com 
Dan Ebberts
June 4, 2009 at 5:03 pmMath.asin(), Math.acos(), math.atan(), and math.atan2() are what you’re looking for. This is a good reference:
https://www.w3schools.com/jsref/jsref_obj_math.asp
Dan

Koby Goldberg
June 4, 2009 at 5:07 pmYes, they exist. Use:
Math.asin(x)
Math.acos(x)
Math.atan(x)Koby.

Koby Goldberg
June 4, 2009 at 8:40 pmMark,
Note thatasin, acos
,… return the angle in Radians!
If you want to convert it to Degrees, you should multiply the result by 180 and divide by pi (Math.PI
).
However, if you are usingsin(x)
orcos(x)
,x
should be in Radians.Koby.

Mark Walczak
June 5, 2009 at 2:51 amThanks for the tip! Fortunately, I was able to find the radianstodegrees command, which seemed to do the trick. Thanks for all of your help!
What makes you explode?
http://www.explosivegraffix.com 
Jim Lefevre
March 31, 2021 at 8:14 amMark (and everyone),
THANK YOU! I’m doing exactly the same thing and have been hitting my head against a wall for a day or so.
You guys are all so great.
Jim

Jim Lefevre
March 31, 2021 at 8:47 amAh… spoke too soon!
As Mark was doing, I’m trying to figure out the angle of a right angled triangle from the lengths of the sides using inverse sin.
I’ve followed all of the replies here and am getting somewhere all up until I try and call the inverse sin using …
Math.asin().
…where the expression gives me an error. Here is the code…
//half the base length
m = thisComp.layer("DRIVER_all_sides").effect("length  X")("Slider")/2;
//pythagorus to find the length of the hypotenuse
a = thisComp.layer("DRIVER_all_sides").effect("length  X")("Slider")/2;
b = thisComp.layer("DRIVER_all_sides").effect("HEIGHT_TRIANGLE")("Slider");
x = Math.sqrt(((a*a) + (b*b)));
//angle via inverse sin
q = (x/m);
p = Math.asin(q);
pThe error is the “Error: couldn’t turn the result into a numeric value” one
I’ve yet to get it to the stage of turning it the Radians into degrees as it’s falling over as soon as Math.asin() is in there.
Does anyone have any suggestions? (and I’ve had a trawl through that wonderful Math resource you posted Dan)

Jim Lefevre
March 31, 2021 at 12:33 pmHA!!
Done it I think.
For some reason my equation to get the angle via inverse sin had the values dividing the wrong way – I was dividing the base length by the hypotenuse or the other way round – which meant that I was coming out with a value greater than 1 which, it appears, an inverse sine can’t do (I have no idea how this works btw).
I swapped them round and came to a result which worked and then had to take that value away from 90 degrees so that it was rotating in the direction I needed.
Works perfectly now…
//I have two slide values that control the Base length and the height of the triangle (the altitude)
//half the base length to get me a value that creates a right angle triangle essentially
m = thisComp.layer("DRIVER_all_sides").effect("length  X")("Slider")/2;//pythagorus to find the length of the hypotenuse
a = thisComp.layer("DRIVER_all_sides").effect("length  X")("Slider")/2;
b = thisComp.layer("DRIVER_all_sides").effect("HEIGHT_TRIANGLE")("Slider");
x = Math.sqrt(((a*a) + (b*b)));//get the angle via inverse sin
q = Math.asin(m/x);
//convert this to degrees
p = (radiansToDegrees(q));
//final result needs offsetting so it rotates the correct way when I change the base triangle value
90p

Koby Goldberg
March 31, 2021 at 5:06 pmHi Jim,
You should be able to do the calculation slightly simpler by using Math.atan2 instead of Math.asin, and thus you will not need the calculation of x (you won’t need the sqrt calculation). Simpler expressions could make your render faster, because expressions are being calculated every frame.
Try this, I think it should give you the same result as above:
a = thisComp.layer(“DRIVER_all_sides”).effect(“length – X”)(“Slider”)/2;
b = thisComp.layer(“DRIVER_all_sides”).effect(“HEIGHT_TRIANGLE”)(“Slider”);Math.atan2(b,a)
If you still need to do the (90p) conversion, then just swap a and b in the Math.atan2, i.e.: Math.atan2(a,b), but I think atan2(b,a) should give you the final angle you wanted.
Cheers,
Koby

Jim Lefevre
March 31, 2021 at 7:31 pmOh wow. I think this is all fresh enough in my mind that I can restitch it with that and see if it works. I absolutely agree that making things more efficient will help in the long run.
Thank you
Reply to this Discussion! Login or Sign Up