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?
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...
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.
Sent from: http://forum.openscad.org/
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:discuss-bounces@lists.openscad.org] On Behalf Of Bryan
Lee
Sent: 31 October 2019 07:18
To: discuss@lists.openscad.org
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
Discuss@lists.openscad.org
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
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. ;-)
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.