Multi_diff_list

florisdejonge's Avatar

florisdejonge

29 Mar, 2025 07:39 AM

Hi all,

I find myself quite often making this network (or similar) again and again. It uses the diff_list node from the Cartan library, but I want to compare an A, B, C, D list (and maybe more). Is there a way to expand on this principle and turn in into a more general purpose subnetwork?

Floris

  1. Support Staff 1 Posted by john on 29 Mar, 2025 08:10 AM

    john's Avatar

    Interesting.

    Can you say a little more what you are trying to do here (at a high level)? Are you trying to distribute an equal amount of four colors randomly across a set?

  2. 2 Posted by florisdejonge on 29 Mar, 2025 08:19 AM

    florisdejonge's Avatar

    I often want to split a collection of shapes into subsets for either futher manipulation (with a different treatment per subset) or colorization for penplotting. In that case one would need a selection (in this case random), which excludes another portion. The diff_list leads only to two subsets, but sometimes I want to have multiple: it could also be 3 of 5. Hopefully this clarifies it somewhat.

  3. 3 Posted by florisdejonge on 29 Mar, 2025 08:21 AM

    florisdejonge's Avatar

    In addition, an equal distribution sounds logical. But I can imagine the situation when it's nice that one color is dominant or a subset is more prominent (like A: 2/3; B: 1/6; C: 1/6).

  4. Support Staff 4 Posted by john on 29 Mar, 2025 08:40 AM

    john's Avatar

    Would something like this do the trick (for the case of 25% each of four colors)?

    I understand that you want to form the subsets first and then apply colors (so that you could do other manipulations). But in the case of simply coloring the sets, it would be easier to let the colors define the subsets. If that suffices, you might not even need to define the subsets first.

    But if you insist on creating the subsets first, would these always be subsets of shapes or paths (that you could group)? Or would you want subsets of something you can't group, like points or strings or numbers? In the latter case, could you say more about the use case? I could use my new multi_list node to handle that.

  5. Support Staff 5 Posted by john on 29 Mar, 2025 01:17 PM

    john's Avatar

    Floris,

    I went ahead and made a node that can make proportional subsets as either a multi-list of a set of groups:

    Subsets

    Subsets takes 4 inputs:

    • Items. The list to be divided up. Can be anything (paths, geometries, strings, etc.)
    • Proportions. A list of numbers that define how many subsets you want and the relative size of each one.
    • Seed. A random seed.
    • Output as groups. If checked, will output a list of separate groups instead of a multi-list. Ignored if Items contain anything other than a path or geometry.

    For proportions you can provide percentages or any other list of numbers. The number of proportions determines the number of subsets. The relative size of each subset is determined by dividing each proportion by the sum of all the proportions.

    For example, if you set proportions to 50,30,20, 50% of the items will go into the first subset, 30% into the second subset, and 20% into the third subset. 5,3,2 would do the same thing.

    Fractional proportion values are rounded to the nearest integer. If the subsets contain less than the number of items due to rounding, one extra item will be added to each subset starting with the first. If subsets contain more then the number of items, one item will be removed from each subset starting with the last subset. This ensures that the final distribution is as evenly balanced and close to the desired proportions as possible.

    If you don't check the groups option, or the items contain something that can't be grouped, subsets will output a multi-list. This is simply a table which includes all the items, each under a list number starting with 0. You can either filter this table to extract a particular subset, or use my multi_switch node. For more about multi-lists see:

    http://support.nodebox.net/discussions/show-your-work/886-multi-lists

    The attached demo shows FOUR different ways to use the subsets node.

    • In the upper left, a grid of 100 squares is divided into 3 subsets: two thirds (four sixths) in subset 0, one sixth in subset 1, and one sixth in subset 2. The subsets are then separated out with multi-switches and colored blue, yellow, and red. For 100 squares this works out to 67 blue, 17 yellow, and 16 red.
    • Below this I show 45 squares divided into 9 subsets. The relative sizes are 1,2,3,4,5,6,7,8,9 (which sum to 45). For this example I choose the "Output as groups" option to output 9 groups of squares; the first group has only one square, the next has two, and so on. I then use my color_brewer node to assign each subset its own color and feed each group into an "add_labels" network to label each square in each subset with the number of squares in that subset. So there is one light blue square labeled 1, two dark blue squares labeled 2, etc.
    • The third example in the middle column divides a list of 26 animal names into four subsets: 10% are used to label hearts, 40% are used to label spades, 25% are used to label diamonds, and 25% are used to label clubs.
    • The final example on the right divides 20 balloons into 3 subsets: 40% on the bottom, 30% in the middle, and the final 30% on top. To make them float upwards I simply stack them. This shows that you can group geometries as well as paths.

    Things to try:

    • Change the total number of squares in the first example by changing the limit number in the grid_plus node. Notice that the 4/1/1 proportions are maintained even as the number of squares change.
    • Change the seed value on any of the four subsets node to change the random allotments.
    • Alter the proportions in any of the four subsets by changing the comma-separated list in the make_numbers nodes just above each subsets node.

    Floris - and anyone else reading this: please give subsets a try and let me know if it does what you were asking for. If you approve, this will be the first node added to the next release of my library,

  6. 6 Posted by florisdejonge on 30 Mar, 2025 12:45 PM

    florisdejonge's Avatar

    That's really great John! It works like I hoped. The proportions work really nice. Also, the output of either groups or a list is useful.

    I would think that would be once again a great addition to your library. In addition, just thinking further along, I mostly use randomization, waves or noise values as input to make things look interesting. But I am wondering wether there are more things possible in Nodebox. Do you have thoughts or ideas on that matter?

    I saw that the node that causes the randomizaton in the subsets lists is a shuffle-node, is that correct? Could that one also be replaced by a noise or wave value? See attached example.

    Floris

  7. Support Staff 7 Posted by john on 31 Mar, 2025 04:18 AM

    john's Avatar

    Floris,

    It is a pleasure collaborating with you. Not only do you come up with great ideas for new nodes, you also help me make them better. In the process of following your latest suggestion I've come up with several other good nodes as well (see demo).

    IMPROVED SUBSETS NODE

    I have added a new input to the subsets node: random numbers.

    Random numbers is a list of numbers which can be used to shuffle the subsets in a more controlled way. It is optional. If you don't supply it, subsets will just shuffle the items as before using the seed.

    But if you DO supply such a list, it will sort the items using that list. For example, you can use my noise node to generate Perlin noise values and feed that into the random numbers port. Subsets will make a table matching each item to a random number, sort by those numbers, then retrieve the items in this new order.

    The supplied proportion list will then be applied to this sorted list as before. I find this works well with Perlin noise, but the system is very flexible; you can use any wacky system you want to shuffle the deck before dividing it into proportional subsets.

    The node that does this controlled shuffling is so useful I will probably release it as a separate node...

    SHUFFLE_FIXED NODE

    Shuffle_fixed takes any list and reorders it using a fixed set of numbers. These numbers could be random, or something as simple as 1,2,3. See below for an example.

    The supplied numbers do not have to contain the same number of items as the list you are shuffling. If you supply fewer or more numbers than items, the node will adapt using yet another new node...

    MATCH_LIST NODE

    Match_list takes a list (of any kind) and a reference list used only to determine the desired number of items. If your list has fewer items than the reference, the list will be repeated until there are enough. If your list has more items than the reference, the list will be trimmed to match the exact length.

    Some may object that Nodebox already does this automatically. If you feed 10 rectangles and 3 colors into a colorize node, it will repeat those 3 colors until all 10 rectangles are colored. But in practice, especially when you are working inside subnetworks with sub-subnetworks with a mix of feed by value and feed by list, sometimes Nodebox's automatic repeating behavior doesn't do what you want. To keep things safe you sometimes need to be sure that all lists feeding into a network node have exactly the same number of items.

    I have come across this issue MANY times over the years. It can make for some confusing behavior if you're not careful. I've become paranoid about this and constantly check the counts of things being fed into any new subnetwork I make. So I think this match_list node might be a keeper!

    I have already used it inside yet ANOTHER new node...

    MULTI_COLOR NODE

    Multi-color applies a list of colors to a list of groups OR a multi-list. This saves you from having to break subsets into different subtrees and apply colors separately to each. It takes the following parameters:

    • Shape List. A multi-list OR a list of paths or groups containing things that can be colored. If you supply a multi-list it will extract each list and combine every colorable item in that list into a group. So a multi-list with 3 lists will turn into 3 groups. If you instead supply a list of groups (or a list of paths) it will apply colors directly to those groups.
    • Fills. A list of fill colors. If you leave this empty, all fill colors will default to black. If you supply fewer (or more) colors than needed, the colors will repeat as needed to match the shape list.
    • Stroke Colors. A list of stroke colors. If you leave this empty, all stroke colors (if storkeWidth > 0) will default to black. If you supply fewer (or more) colors than needed, the colors will repeat as needed to match the shape list.
    • Stroke Widths. A list of stroke widths. If you leave this empty, all stroke widths will default to 0 (no strokes). If you supply a single value, that value will be used for all shapes.

    Unlike colorize, match_color will not apply fills to open paths. It will always output a list of groups, even if you supplied a multi-list. But if you supply a list of ungrouped paths, it will not put each each path into its own group, but will leave them as they were.

    DEMO

    All four of these nodes are demonstrated in the attached Nodebox file (see screenshot):

    • At top left I use the improved subsets node to apply Perlin noise to divide 400 squares into 3 subsets using 2,1,1 proportions. I then use multi-color to color the three sets with Sanzo colors. Notice that you can set subsets to output groups or not - multi-color doesn't care. The 2,1,1 proportions create something that looks like an archipelago with half the pixels colored tan to represent water and the remaining pixels divided to create islands with turquoise shores and dark blue mountains. See MOVIE below.
    • At bottom left I show the 26 letters of the alphabet shuffled using shuffle_fixed. Instead of supplying a truly random set of numbers for sorting, I use a range node to supply 1, 2, 3. This has the same effect as counting off by 3s as they do in the army. Everyone in the group counts off 1, 2, 3, 1, 2, 3, etc., then all the 1s go first, the 2s go next, and the 3s go last. When applied to the letters of the alphabet its like using a take_every node with N set to 3: first you get A, then D, then G, etc., then B, E, H, etc., then C, F, I, etc. I applied some coloring to make this pattern stand out a little more.
    • At top right I feed 20 balloons into a multi-color node with 3 fill colors and a stroke width of 1. Since I didn't supply a stroke color, all strokes default to black. Notice that the strings do not have fill colors (as it should be).
    • At bottom right I show how the match_list node (inside multi-color), builds a list of 20 fill colors to match the number of balloons. Without this extra step, Nodebox would have repeated the 20 groups 3 times to create 60 balloons.

    MOVIE

    As an added bonus I made a quick 10-second looping movie of the shifting colors in the subsets example. Perlin noise is wonderful for animations. I simply pass the frame node into the z port of noise to create a gradually shifting pattern. I then use a wave node to add a rising and falling scale, with create a zoom in and zoom out effect. If you don't like these colors, just change number setting on the sanzo_colors node.

    Floris, please try this improved subsets node and verify that it does everything you hoped for. You might also use my noise node instead of the old one you were using; mine does the same thing but saves you the bother of adding extra nodes to handle the scaling.

    Note that although this demo includes the NOISE.CLJ code library, that is only needed to demonstrate Perlin noise. None of these nodes require any code modules.

  8. Support Staff 8 Posted by john on 02 Apr, 2025 05:43 AM

    john's Avatar

    UPDATE

    I just caught a small bug in the version of subsets posted above.

    The bug causes the subsets not to shuffle if you don't provide a list of random numbers. I have fixed it here. So please use this demo (attached) to try the subsets node.

    This demo is simple but kind of fun. I scatter some points in a triangle, divide them into three sets (2/1/1 proportions) and turn them into 3 colored lines with little shapes at each point. You can get quite a wild assortment of lines by changing the seeds in the scatter or subsets nodes or changing the number of points.

    Enjoy!

  9. 9 Posted by florisdejonge on 02 Apr, 2025 06:28 PM

    florisdejonge's Avatar

    Hi John,
    Thanks for the improved improved subsets node. I was working through your posts and did a quick comparison - see attached screenshot. The third one is with a forced random node, because it did not work at first. I also found that the compare node within the subset node ought to be set to 1. Nevertheless, with the most recent update (which I read later), it works as expected and intuitively.

    I also like the match list as an addition. There are also a lot of situations where something has to be counted/divided first. So, definitely a keeper as far as I am concerned.

    In regard the animation: it's really cool. I am still struggling to find the right settings for controlling a simple infinite loop with either noise or wave values, with it visibly rewinding. Your animation both zooms and iterates with the noise values, but does it return to that same state somewhere? Do we have a seperate place on the forum for discussing animation/motion design? Or can we somehow group them?

    Kind regards,
    Floris

  10. Support Staff 10 Posted by john on 03 Apr, 2025 10:05 AM

    john's Avatar

    Hi Floris,

    I'm glad you found the compare node that should have been set to 1. That was indeed the bug I fixed with my subsequent post. I think everything is working now.

    I don't quite understand your animation question. Do you want to create an obvious rewind effect? Ot did you mean to ask how to create seamless looping with Perlin noise?

    I don't think we need a new channel; just start a new thread titled Animation Discussion and ask some more detailed questions. I think everyone would benefit from a deeper discussion about animation techniques.

  11. 11 Posted by florisdejonge on 03 Apr, 2025 04:44 PM

    florisdejonge's Avatar

    Sorry. That was a typo. I meant 'without' it visibly rewinding. So a seamless loop indeed. I'll start a new topic on that subject, maybe we can collect some other forum threads there.

Reply to this discussion

Internal reply

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

Attaching KB article:

»

Already uploaded files

  • multi_diff_list_29032025.ndbx.zip 2.08 KB
  • Schermafbeelding_2025-03-29_om_08.34.12.png 200 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 Apr, 2025 09:51 PM
17 Apr, 2025 07:02 PM
16 Apr, 2025 10:59 PM
14 Apr, 2025 10:55 PM
14 Apr, 2025 10:18 PM

 

13 Apr, 2025 11:09 PM
10 Apr, 2025 11:30 PM
10 Apr, 2025 10:16 PM
10 Apr, 2025 10:02 PM
03 Apr, 2025 04:44 PM
30 Mar, 2025 02:09 AM