Function to return the Opposite value

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Function to return the Opposite value

leebc
I want to define a value at the top of my program:

        isThing=1;  // isThing can be 0 or 1

But later on I need to be able to use the opposite value.  
I.E.  
        1 if isThing==0 or
        0 if isThing==1

Note that this needs to be a number, not a boolean because I will be
multiplying vectors by it.

I am happy with either defining a variable or using a function to get this
opposite value.


How can I do this?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Function to return the Opposite value

MichaelAtOz
Administrator
Either

  notThing = ( (isThing == 0) ? 1 : 0);

or

  function notThing() = ( (isThing == 0) ? 1 : 0);

The former saves typing ()



-----
Admin - email* me if you need anything, or if I've done something stupid...

* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work. Obviously inclusion of works of previous authors is not included in the above.

The TPP is no simple “trade agreement.”   Fight it! http://www.ourfairdeal.org/   time is running out!
--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Admin - email* me if you need anything, or if I've done something stupid...
* click on my MichaelAtOz label, there is a link to email me.

Unless specifically shown otherwise above, my contribution is in the Public Domain; to the extent possible under law, I have waived all copyright and related or neighbouring rights to this work.
Obviously inclusion of works of previous authors is not included in the above.
Reply | Threaded
Open this post in threaded view
|

Re: Function to return the Opposite value

Troberg
In reply to this post by leebc
This will do it:

oppositething=1-thing;



--
Sent from: http://forum.openscad.org/

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Function to return the Opposite value

alexgibson
In reply to this post by leebc
Easy:

        oppositeThing = 1-isThing;

So if isThing is 1, oppositeThing is 0, and if isThing is 0, oppositeThing
is 1.


I sometimes also use this as a dirty way of enabling or disable whole chunks
of code, by putting it within a scale fubction - IE

module thingwithtwoversions()
        {
        scale([1,1,isThing])
                {
                Your module, primary version
                }
        scale([1,1,oppositeThing])
                {
                Your module, alternate version
                }
        }

This can be done more intelligently using logical if statements but this way
is quick to insert!

Alex Gibson

admg consulting

edumaker limited

. Project management
. Operations & Process improvement
. 3D Printing


-----Original Message-----
From: Discuss [mailto:[hidden email]] On Behalf Of Bryan
Lee
Sent: 31 October 2019 07:18
To: [hidden email]
Subject: [OpenSCAD] Function to return the Opposite value

I want to define a value at the top of my program:

        isThing=1;  // isThing can be 0 or 1

But later on I need to be able to use the opposite value.  
I.E.  
        1 if isThing==0 or
        0 if isThing==1

Note that this needs to be a number, not a boolean because I will be
multiplying vectors by it.

I am happy with either defining a variable or using a function to get this
opposite value.


How can I do this?

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Function to return the Opposite value

leebc
Thanks for the replies guys!

@MichaelAtOz
>   notThing = ( (isThing == 0) ? 1 : 0);

Unfortunately, this produces a booleen.


@Troberg
> oppositething=1-thing;

Brilliant!   I knew there had to be an easy math way to do it.


@Alex Gibson
>       oppositeThing = 1-isThing;
> I sometimes also use this as a dirty way of enabling or disable whole chunks
> of code, by putting it within a scale fubction

Still brilliant.  That's what I plan on doing with it, enable and disable
chunks of translation code.  ;-)

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
tp3
Reply | Threaded
Open this post in threaded view
|

Re: Function to return the Opposite value

tp3
On 31.10.19 19:16, Bryan Lee wrote:
> @MichaelAtOz
>>   notThing = ( (isThing == 0) ? 1 : 0);
>
> Unfortunately, this produces a booleen.

isThing = 0;

notThing = ( (isThing == 0) ? 1 : 0);

echo(notThing); // ECHO: 1

echo(num = is_num(notThing));   // ECHO: num = true
echo(bool = is_bool(notThing)); // ECHO: bool = false

ciao,
  Torsten.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
-- Torsten