We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello everyone,
I was making a little program to learn and for fun today, and I think I'm done with the first pass. The idea behind this program is to make a 4x4 array, and then create a rectangle depending on a given value. They're either white or black, and they create a little 4x4 grid.
My idea was to make a sketch that would display all the different possibilities (65536 in total). So I wrote the code and now I'm wondering : all are these objects actually unique? I'm not sure on how I can check.
If I find a way to display it in an interesting way I might print it out. I like how each form looks. So I want to make sure that they're all actually unique first.
Here's the code if you're curious and willing to help me out :P
ArrayList<Form> formCollection = new ArrayList<Form>();
int maxForms = 65536;
void setup() {
size(2560, 2560);
background(0);
frameRate(10);
for (int i=2; i<width-4; i+=width/256) {
for (int j=2; j<height-4; j+=height/256) {
if (formCollection.size () < maxForms) {
Form form = new Form(i,j);
if (!formCollection.contains(form)) {
formCollection.add(form);
counter++;
} else {
break;
}
}
}
}
println("Size of formCollection =", formCollection.size());
for (int i=0; i<formCollection.size (); i++) {
Form form = formCollection.get(i);
form.update();
}
}
void draw() {
}
class Form {
int[][] form = new int[4][4];
int xpos, ypos;
int s = 2;
color colour = color(100);
Form (int _x, int _y) {
xpos = _x;
ypos = _y;
}
void update() {
populate();
display(xpos,ypos);
}
void populate() {
for (int x=0; x<4; x++) {
for (int y=0; y<4; y++) {
form[x][y] = int(random(0, 2));
}
}
}
void display(int xpos,int ypos) {
for (int x=0; x<8; x+=2) {
for (int y=0; y<8; y+=2) {
for (int x2=0; x2<4; x2++) {
for (int y2=0; y2<4; y2++) {
form[x2][y2] = int(random(0, 2));
if (form[x2][y2] == 0) {
colour = color(255);
} else {
colour = color(0);
}
}
}
//stroke(255, 0, 0);
noStroke();
fill(colour);
rect(xpos+x, ypos+y, s, s);
}
}
}
}
Answers
null
elements: http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.htmlclass
we need to@Override
its equals() method: http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-@Override
hashCode() too: http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--You're choosing them randomly and random () can repeat.
I'd be tempted to create an array containing 0-65535, shuffle them, and then go through them using the number to generate the patterns. Guaranteed unique but still looks random.
Well, I do have line 14. Doesn't it check it the collection already contains it?
The point is that whilst 2 instances of a form object are each unique
form1 != form2
the pattern they draw can be exactly the same. I.e. Line 14 will always return false but this doesn't guarantee uniqueness.So +1 to @koogs suggestion.
@blindfish Thanks for the explanation, I get it now. I'll have to look @koogs suggestion and applying it.
You say there are 65536 possible patterns and you want to be able to display them all. In that case the solution provide by koogs is the way to go. Generating random patterns and seeing if they already been generated would be disastrous if you want to see them all
The problem is exponential which means the universe will probably come to an end before you generate all 65536 patterns
This 'random' generation approach will only work when selecting a small number of unique values from a large range.
Secondly the if statement doesn't work in your program consider your statement
Form form = new Form(i,j);
This will generate a new object of type Form. In this line
form
is called an object reference and is used to identify the location in memory where the object data i.e. the 2D array etc can be found. So every time the statement is executed a new object is created but because the pattern is randomly generated it can be the same as another Form object.The if statement searches the collection for an object with the same object reference which in your code is impossible so the
contains
method always returns false.I just noticed something in your code:
When the form object is created the pattern array elements are set to zero so why test for a match in the collection because they are all all-zero patterns. The pattern only changes when you call update(0 or display().