Activity › Forums › Adobe After Effects Expressions › Find the Largest of 3 Values
-
Find the Largest of 3 Values
Posted by Jon Smith on June 26, 2012 at 8:53 pmI trying to figure out how to find the largest of 3 values. I got fairly close using if/else statements but it always breaks somewhere. Ideally I would like to get the 3 values as small, med and large. Thanks.
Here’s where I left off:
if (a > b) {
if (a > c){
v = a
}
else {
if (b > c){
if (b > a)
v = b
}
else {
if (c > a)
v = c
}
}
}
else {
v = c
}Walter Soyka replied 14 years, 2 months ago 5 Members · 12 Replies -
12 Replies
-
Jon Smith
June 26, 2012 at 9:03 pmOkay, after taking a deep breath and diving back in, I’ve figured out how to get the largest value.
if (a > b) {
if (a > c){
v = a
}
else {
if (c > b){
v = c
}
}
}else {
if (b>c){
v = b
}
else{
v = c
}
} -
Kevin Camp
June 26, 2012 at 9:16 pmi think you could do it with this:
Math.max(Math.max(a,b),c);
if you needed more values, you’d need to embed them in more Math.max() functions, ex:
Math.max(Math.max(Math.max(a,b),c),d);
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW -
Dan Ebberts
June 26, 2012 at 9:34 pmMath.max is designed to take as many parameters as you want to throw at it. This should work:
Math.max(a,b,c)
I seem to recall there was a version of AE where this didn’t quite work as advertised, but I’d say try it and see if it works for you.
Dan
-
Kevin Camp
June 26, 2012 at 9:47 pmyou know, i’ve always thought it should work that way, but never tried it — though i’ve rarely needed it…
thanks.
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW -
Kevin Camp
June 26, 2012 at 9:50 pmwell… it doesn’t seem to work as advertised in cs4. it only seemed to compare the first 2 values.
Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW -
Jon Smith
June 26, 2012 at 9:50 pmThanks a bunch. That’s much simpler. Looks like you do need to nest it the way Kevin has it. Any thoughts on how to get the middle value?
-
Dan Ebberts
June 26, 2012 at 9:59 pmYeah, it looks like it didn’t get fixed until CS6. Safer to do it your way.
Dan
-
Kevin Camp
June 26, 2012 at 10:06 pmi think this would work…
vals = [a,b,c];
max = Math.max(Math.max(a,b),c);
min = Math.min(Math.min(a,b),c);
for (i = 0; i < vals.length; i++){
if (vals[i] > min && vals[i] < max) mid = vals[i];
}
midKevin Camp
Senior Designer
KCPQ, KMYQ & KRCW -
Jon Smith
June 26, 2012 at 10:27 pmSeems to work unless there are equal values then the expression breaks. Is there a way around that?
-
Xavier Gomez
June 27, 2012 at 12:50 pmTry this:
max = Math.max(Math.max(a,b),c);
min = Math.min(Math.min(a,b),c);
if (a < max){
if (a > min) mid=a else mid=Math.min(b,c);
}
else {mid = Math.max(b,c)};
mid
It will hardly generalize to higher number of parameters but for just 3 it is ok.
Reply to this Discussion! Login or Sign Up