I am trying to create a program that will iterate through all the possible combinations for a given array once and only once, and use those combinations to color a grid of dots. I’ve simplified the problem to a one-row, two-column grid with only two colors.
I think I could be able to color the dots if I can programmatically create these four different configurations of an array…
int dots = { colors[1], colors[0] };
int dots = { colors[1], colors[1] };
int dots = { colors[0], colors[0] };
int dots = { colors[0], colors[1] };
…assuming I have another array that is:
int colors = {#FFFFFF, #000000};
I’m relatively familiar with creating arrays, I know they can be modified, and I’m thinking I should use an if, else statement to check if a particular configuration of the array has already been drawn. But I have no idea how to do that check. Also possible this is a stupid way of trying to achieve my ultimate goal which is is 3x3 grid (with 512 possible combinations) similar to this. Anyone have any ideas? Thanks in advance for your help.
Thanks @josephh, I appreciate the pointer. I read that article a few times but couldn’t quite wrap my head around what to do next. I’m a bit of a noob and probably in over my head. I’ve been able to get every dot to color with either white or black, but I can’t figure out how to (1) make sure I get all the possible combinations, and (2) make sure I get no duplicates. I gather I create a data[] array in which to produce all the possible combinations but I can’t figure out how to do that. If anyone has further advice I definitely welcome it. Here’s what I’ve got so far (with a 2 row, 2 column grid instead of one row):
import processing.pdf.*;
boolean savePDF = true;
int colorset;
color [] colorsetarray = {#FFFFFF, #000000};
void setup () {
size(400, 400);
noLoop(); //prevents video playback
}
void draw () {
beginRecord(PDF, "Arabesque"+year()+"-"+"0"+month()+"-"+day()+"_"+hour()+minute()+"0"+second()+".pdf" );
for (int y=20; y<height; y += 50) {
for (int x=20; x<width; x += 50) {
drawGrid(x,y);
}
}
// saves the image when you play the sketch with the date and seconds in the sketch folder
endRecord();
}
void drawGrid (float x, float y) {
noStroke();
fill(colorsetarray[int(random(2))]);
circle(x,y,6);
fill(colorsetarray[int(random(2))]);
circle(x+9,y,6);
fill(colorsetarray[int(random(2))]);
circle(x,y+9,6);
fill(colorsetarray[int(random(2))]);
circle(x+9,y+9,6);
}
start of with 2 empty ArrayLists (for flexibility). In the first one you will assign a new random variable and the add it to your first arraylist after checking if the ArrayList contains the new value or not.
Now if you know how many combinations to expect you know the maximum size the first ArrayList needs to be, so we need an if statement to increment a counter that tells us when we are trying to add a value that is already in the array, but we need to make sure we only count it once so we get an accurate idea of the number of combinations we have explored. Thats why I suggest using a second Arraylist. This one shall also only be populated after a second test to see if the second array contains your new value, then finally add a condition to your loop that compares your counter to how big the first or second ArrayList is, if its the size of the predetermined max for the combinations you’ve explored all combinations and the job is done.
to work out total combinations you need to understand what operations you are going to do. Say you are picking from a set value which can be reused then you would calculate x^y, x will be the number of choices you can make and y being the number of times you are allowed to make a choice. The number of choices remains the same as all probabilities are still available each time you choose. ( Think putting a card back into the deck, throwing a dice or flipping a coin).
If the choices cannot be reused then you would use the factorial to find out your max combinations, ie;
pick a card from a set of cards and then do not put your choice back in the deck.
52 total cards so 52! max combinations if you decide that you have t make 52 choices.
If you are making less than 52 choices then you have to amend the factorial calculation. We know that to calculate factorial we have to do totalChoices * (maximumChoices - currentChoice) currentChoice
being the number of times we have already chosen. And finally we multiply our last answer with any new choices, giving us.
I’m not going to be the most efficient, but I think I’m on my way. I may report back if I have more questions, but thanks a ton for the really helpful guidance!
I my Sketch (that I didn’t post, didn’t want to spoil the fun for you) it just produces the 512 permutations without random. That’s what the 9 nested for loops do. There is also a check for duplicates.
In my Sketch (that I didn’t post, didn’t want to spoil the fun for you) it just produces the 512 permutations without random.
And thanks for explaining what the for loops do. I’ve got some ideas for how to proceed, and this is a fun challenge, so I’ll keep following the path I’m on. If I lose my path though I might cry for help.
Thanks for the tip. I thought I had formatted it right, but apparently not, so I edited it to make it better (but it includes arrays instead of ArrayLists). I’m watching Shiffman videos to figure out ArrayLists, classes, etc.
This may not answer your questions but will give you something to think about.
You already have all the possible combinations… you just have to think in binary (and hexadecimal) and extract the bits.
Then wield these bits as you desire to unleash your creative vision.
for(int i=0; i<64; i++)
{
println(binary(i));
}
Partial code to get you started:
void setup()
{
size(100, 160);
colorMode(RGB, 1); // Just using black 0 and white 1
for(int i=0; i<16; i++)
{
println(binary(i));
}
for(int i=0; i<16; i++)
{
int a, b;
a = i & 0x01;
b = (a > 0) ? 1 : 0;
print(b);
fill(b);
circle(20, i*10+5, 10);
a = i & 0x02;
b = (a > 0) ? 1 : 0;
print(b);
fill(b);
circle(30, i*10+5, 10);
a = i & 0x04;
b = (a > 0) ? 1 : 0;
print(b);
fill(b);
circle(40, i*10+5, 10);
a = i & 0x08;
b = (a > 0) ? 1 : 0;
println(b);
fill(b);
circle(50, i*10+5, 10);
}
}
It’s gonna take me a bit to understand exactly what’s going on, but I think I’m getting the gist. Thanks also for pointing me to the reference. I love the way Processing lays out its documentation; so easy to read and understand (at least eventually if not at first).
You may be right. Because the question said “I’ve simplified the problem” it wasn’t clear to me what the ultimate goal was. There are two values in the simplified case, but it may scale up to permutation with repetition…?
Ah, no, I see, I missed the goal link. That is combinations, not permutations. In fact, it is counting – base 2, but if there were 3 colors, it would be base 3…