Conditionals in NodeBox

john's Avatar

john

31 Mar, 2024 02:42 AM

Nodebox artist Gabriel_Simi posed the following question on his Instagram account:

Nitty Gritty Conditional Programming. Paint with red or black if a number (sine function of frame number) is above or below a certain threshold. Right hand side is paint them in order, Left hand side is paint them shuffled (randomized). In text programming, this is an easy if/then statement. Here I could not figure out how to do this without 2 compare nodes, 2 combine nodes, and 2 slice nodes. The problem with this setup is how do I now add a third color? It would really be "if then else" in a text based language, but I don't know how to do that in Nodebox.

My response:

Gabriel,

I was able to recreate your two column animation using only 15 nodes (vs. your 30), with only one compare node, 1 combine node, and no slice nodes. See screenshot and sample network (attached).

The demo includes a separate network which adds a third color. I also attached MP4 animations for both the original 2 color and 3 color verisions.

The basic trick to doing conditionals in Nodebox is the switch node. Switch is similar to a case node in many textual languages, and a case statement with only two cases is equivalent to an IF/THEN/ELSE statement.

IF THEN x ELSE y

is handled in Nodbox by feeding x into input2 of the switch node, y into input1, and into the final index port. Note that the first port is the False (ELSE) condition and the second port is the True (THEN) condition

If your conditional is a boolean value, Nodebox will treat false as 0 and true as 1. An index of 0 will pass through whatever you feed into input 1; an index of 1 will pass through whatever you feed into input2.

You can also use switch to perform case statements with up to 6 cases. Instead of feeding a boolean value into the index port, just feed in any value from 0 to 5. That index will determine which of the 6 inputs will be output from the switch node.

Hope that clears things up. The switch node is fundamental. You can't do much serious coding without it.

You also asked about adding a third color. The basic idea there is to use a case statement instead of an IF THEN ELSE ELSE IF statement. That is, use the same switch node, but instead of feeding in TRUE or FALSE, feed in an index of 0, 1, or 2.

I can think of various ways to break your design into three colors. The demo includes one such way. Basically I just add the sine and sample values divide by 67, and round down; this yields a 0, 1, or 2 (see second animation).

One final note. There is nothing wrong with using a sin node as you did. But I found it easier to use a wave node set to sine waves. This lets you easily define min, max, and period without needing to do any extra math.

I will leave a note on Instagram pointing you to this note. Please reply here so I know you found it.

John

  1. 1 Posted by Gabriel Sim-Lar... on 31 Mar, 2024 02:56 PM

    Gabriel Sim-Laramee's Avatar

    Thank you, John! I always do my best to solve a problem on my own, search the forums, try everything I can think of before I post anything. When I briefly taught math I realized a very simple truth that you can shortchange your students by giving them answers, that the learning process is far more important than the achievement of the goals of learning. Because of this, I feel like asking you is sometimes looking to the cheat sheet. Your knowledge is so extensive and complete!
            All that being said, I will consider this fundamental and try to incorporate it in my work going forward.

  2. Support Staff 2 Posted by john on 05 Apr, 2024 12:30 AM

    john's Avatar

    Gabriel,

    I agree with you 100% about shortchanging students by giving them answers instead of helping them develop the habit of asking good questions. I have taught a little myself (long ago) and have had the privilege of being a guest lecturer for an old friend who is a truly gifted teacher. He didn't lecture but instead created an environment of curiosity.

    This is something I often worry about. By providing such clear and voluminous answers, complete with code, am I squelching a newbie's enthusiasm instead of empowering it?

    I hope not. There comes a time, especially in math or code-related studies, that you really do need a cheat sheet. A multiplication table is the required entrance fee for the universe of math beyond it, but must be drilled in by rote, not playfully discovered. Some things, as you say, are fundamental.

    There is also a long tradition that one of the best ways to learn how to code is to look at other people's code. I often go to Stack Overflow for help and deeply appreciate the code snippets I find there.

    That's how I see my role here. When someone comes to me on the forum, they are stuck, and need to get unstuck before any more growth or creativity can happen. And the ponderous pace of the forum, with hours or days between each exchange, do not allow for the luxury of true interaction.

    So I give them every thing I can, as clearly and completely as I can, until they are well and truly unstuck. I then expect them to sail forth and try new things, making and overcoming their own mistakes completely beyond my view.

    If this does not happen, if the newbie (or temporarily befuddled expert) takes my answer and runs, never to return, it makes me sad. But perhaps this would have been the result in any case. Nodebox is a VERY obscure obsession, encountered by few and loved by even fewer. TRUE students, like you, are rare indeed.

    So, if you wish, only use me when you need to get unstuck. But I hope you will occasionally drop by to share your latest experiment or engage in a coding conversation where we can find our way to new solutions together. I could learn a lot from you!

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Already uploaded files

  • conditional_boxes_screenshot.png 478 KB
  • 2color.mp4 32.1 KB
  • 3color.mp4 40.1 KB
  • conditional_boxes.ndbx.zip 1.29 KB

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

17 Jun, 2024 02:26 PM
15 Jun, 2024 01:44 PM
08 Jun, 2024 09:05 AM
08 Jun, 2024 09:00 AM
04 Jun, 2024 04:37 AM