Creative COW SIGN IN :: SPONSORS :: ADVERTISING :: ABOUT US :: CONTACT US
Creative COW's LinkedIn GroupCreative COW's Facebook PageCreative COW on TwitterCreative COW's Google+ PageCreative COW on YouTube
ADOBE AFTER EFFECTS:HomeForumBasicsExpressionsTutorialsPodcastsAE TechniquesTrainingCreative Cloud DebateFAQ

Find the Largest of 3 Values

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Share on Facebook
Jon SmithFind the Largest of 3 Values
by on Jun 26, 2012 at 8:53:00 pm

I 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
}




Return to posts index
Reply   Like  

Jon SmithRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:03:02 pm

Okay, 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
}
}




Return to posts index
Reply   Like  

Kevin CampRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:16:08 pm

i 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


Return to posts index
Reply   Like  


Dan EbbertsRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:34:02 pm

Math.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



Return to posts index
Reply   Like  

Kevin CampRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:47:22 pm

you 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


Return to posts index
Reply   Like  

Kevin CampRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:50:22 pm

well... 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


Return to posts index
Reply   Like  


Dan EbbertsRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:59:01 pm

Yeah, it looks like it didn't get fixed until CS6. Safer to do it your way.

Dan



Return to posts index
Reply   Like  

Jon SmithRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 9:50:44 pm

Thanks 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?



Return to posts index
Reply   Like  

Kevin CampRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 10:06:22 pm

i 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];
}
mid


Kevin Camp
Senior Designer
KCPQ, KMYQ & KRCW


Return to posts index
Reply   Like  


Jon SmithRe: Find the Largest of 3 Values
by on Jun 26, 2012 at 10:27:41 pm

Seems to work unless there are equal values then the expression breaks. Is there a way around that?



Return to posts index
Reply   Like  

Xavier GomezRe: Find the Largest of 3 Values
by on Jun 27, 2012 at 12:50:42 pm

Try 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.


Return to posts index
Reply   Like  

Dan EbbertsRe: Find the Largest of 3 Values
by on Jun 27, 2012 at 1:18:17 pm

I think this works too:

Math.min(Math.min(Math.max(a,b),Math.max(a,c)),Math.max(b,c))


Dan



Return to posts index
Reply   Like  


Walter SoykaRe: Find the Largest of 3 Values
by on Jun 30, 2012 at 1:14:09 am

Here's an alternate approach, using arrays and the sort() method. First, stuff each of the values into an unsorted array. Next, sort the array. Finally, return the value of the last item in the array, which will be the highest value because we just sorted it.

unsortedArray = [a,b,c];
sortedArray = unsortedArray.sort();
sortedArray[sortedArray.length-1];


To get the middle value, replace the final line with:sortedArray[Math.round(sortedArray.length/2)-1];

To get the lowest value, replace the final line with:sortedArray[0];


You can shorten it, too, removing the assignment:
myArray = [a,b,c];
myArray.sort()[sortedArray.length-1];


Walter Soyka
Principal & Designer at Keen Live
Motion Graphics, Widescreen Events, Presentation Design, and Consulting
RenderBreak Blog - What I'm thinking when my workstation's thinking
Creative Cow Forum Host: Live & Stage Events


Return to posts index
Reply   Like  

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
Share on Facebook


FORUMSTUTORIALSMAGAZINESTOCKYARDVIDEOSPODCASTSEVENTSSERVICESNEWSLETTERNEWSBLOGS

Creative COW LinkedIn Group Creative COW Facebook Page Creative COW on Twitter
© 2013 CreativeCOW.net All rights are reserved. - Privacy Policy

[Top]