Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplifing if statement?

Tags:

logic

Is it possible to simplify this if statement?

and if so what's the answer?

    if (type)
    {
        if(NdotL >= 0.0)
        {
            color   += Idiff + Ispec;
        }
    }
    else
    {
        color   += Idiff + Ispec;
    }
like image 580
Joshua Barnett Avatar asked Nov 22 '25 12:11

Joshua Barnett


1 Answers

Think about this in terms of Boolean algebra. You have two conditions

A = (type)
B = (NdotL >= 0.0 )

And you execute your statement when

A * B
/A

( I use /A to indicate "NOT A", and * to indicate "AND" )

So the only time you don't execute is

A * /B

This means your statement should be

if (!((type) && NdotL < 0.0 )) {
  // do your thing
}

Or, using Boolean identity

(A * B) = /(/A + /B)

you can rewrite your condition as

( /A + B )

if ( !(type) || ( NdotL >= 0 ) ) {
   // do your thing
}
like image 194
Floris Avatar answered Nov 25 '25 00:11

Floris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!