discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Function to return the Opposite value

BL
Bryan Lee
Thu, Oct 31, 2019 7:18 AM

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?

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?
M
MichaelAtOz
Thu, Oct 31, 2019 7:39 AM

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/

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/
T
Troberg
Thu, Oct 31, 2019 8:45 AM

This will do it:

oppositething=1-thing;

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

This will do it: oppositething=1-thing; -- Sent from: http://forum.openscad.org/
AG
Alex Gibson
Thu, Oct 31, 2019 2:51 PM

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

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
BL
Bryan Lee
Thu, Oct 31, 2019 6:16 PM

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.  ;-)

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. ;-)
TP
Torsten Paul
Thu, Oct 31, 2019 7:08 PM

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.

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.