MultiLevelMatrix2D matrix;
int masterLevel = 0;
PImage tempFilter;
int tempThreshold = 60;
void setup() {
size(512, 512, JAVA2D);
matrix = new MultiLevelMatrix2D(8, 0, 0, 512, 512);
registerMouseEvent(this);
tempFilter = this.loadImage("ml.jpg");
for (int ix = 0; ix < matrix.matrices.get(7).length; ix++) {
for (int iy = 0; iy < matrix.matrices.get(7)[0].length; iy++) {
if (iy < tempFilter.height && ix < tempFilter.width) {
if (red(tempFilter.pixels[iy * tempFilter.width + ix]) < 60)
matrix.matrices.get(7)[ix][iy].divide();
}
}
}
}
void draw() {
background(255);
matrix.display();
}
void mouseEvent(MouseEvent event) {
tempThreshold = (int)((float)mouseX / 512f * 256f);
matrix.clear();
for (int ix = 0; ix < matrix.matrices.get(7).length; ix++) {
for (int iy = 0; iy < matrix.matrices.get(7)[0].length; iy++) {
if (iy < tempFilter.height && ix < tempFilter.width) {
if (red(tempFilter.pixels[iy * tempFilter.width + ix]) < tempThreshold)
matrix.matrices.get(7)[ix][iy].divide();
}
}
}
}
class MultiLevelMatrix2D {
float x1, x2, y1, y2;
int[][] neigbourSet = {{-1, 0},
{ 1, 0},
{ 0, -1},
{ 0, 1}};
class Cell2D {
int level = 0;
int show = 0;
Set<Cell2D> neighbours;
Cell2D parent = null;
Set<Cell2D> children;
private boolean active = false;
float x1, x2, y1, y2;
Cell2D(float xx1, float yy1, float xx2, float yy2, int lvl) {
x1 = xx1; x2 = xx2; y1 = yy1; y2 = yy2; level = lvl;
neighbours = new HashSet<Cell2D>();
children = new HashSet<Cell2D>();
}
void clear() {
show = 0;
active = false;
}
void divide() {
show = 1;
if (parent != null) parent.divide();
}
int myShow() {
int result = show;
for (Cell2D cell : children)
result *= cell.myShow();
return result;
}
public void display() {
boolean parentB = false;
if (parent != null)
if (parent.show == 1)
parentB = true;
if (show == 1 || parentB) {
if ((show == 1 && children.size() == 0) || myShow() != 0) fill(200);
rect(x1, y1, x2-x1, y2-y1);
noFill();
if (myShow() == 0) for (Cell2D cell : children) cell.display();
}
}
public void displayMe(int col, boolean reversive) {
fill(col);
rect(x1, y1, x2-x1, y2-y1);
if (parent != null && reversive) parent.displayMe(col, reversive);
}
}
public int levels = 1;
HashMap<Integer, Cell2D[][]> matrices;
public MultiLevelMatrix2D(int ll, float xx1, float yy1, float xx2, float yy2) {
levels = ll;
x1 = xx1; x2 = xx2; y1 = yy1; y2 = yy2;
matrices = new HashMap<Integer, Cell2D[][]>();
for (int l = 0; l < levels; l++) {
int lngth = (int)pow(2, l);
Cell2D[][] me = new Cell2D[lngth][lngth];
for (int ix = 0; ix < lngth; ix++) {
for (int iy = 0; iy < lngth; iy++) {
float xxx1 = ((float)ix / (float)lngth * (x2 - x1)) + x1;
float xxx2 = ((float)(ix + 1) / (float)lngth * (x2 - x1)) + x1;
float yyy1 = ((float)iy / (float)lngth * (y2 - y1)) + y1;
float yyy2 = ((float)(iy + 1) / (float)lngth * (y2 - y1)) + y1;
me[ix][iy] = new Cell2D(xxx1, yyy1, xxx2, yyy2, l);
me[ix][iy].level = l;
if (l == 0) me[ix][iy].show = 1;
}
}
for (int ix = 0; ix < lngth; ix++) {
for (int iy = 0; iy < lngth; iy++) {
for (int[] set : neigbourSet) {
if (exists(me, ix + set[0], iy + set[1]))
me[ix][iy].neighbours.add(me[ix + set[0]][iy + set[1]]);
}
}
}
matrices.put(l, me);
}
for (int l = 0; l < levels; l++) {
int lngth = (int)Math.pow(2, l);
for (int ix = 0; ix < lngth; ix++) {
for (int iy = 0; iy < lngth; iy++) {
if (l > 0) matrices.get(l)[ix][iy].parent = matrices.get(l - 1)[ix / 2][iy / 2];
if (l < levels - 1) {
matrices.get(l)[ix][iy].children.add(matrices.get(l + 1)[ix * 2 ][iy * 2 ]);
matrices.get(l)[ix][iy].children.add(matrices.get(l + 1)[ix * 2 +1][iy * 2 ]);
matrices.get(l)[ix][iy].children.add(matrices.get(l + 1)[ix * 2 ][iy * 2 +1]);
matrices.get(l)[ix][iy].children.add(matrices.get(l + 1)[ix * 2 +1][iy * 2 +1]);
}
}
}
}
}
public void clear() {
for (Cell2D[][] ccc : matrices.values())
for (Cell2D[] cc : ccc)
for (Cell2D cell : cc)
cell.clear();
}
public void display() {
matrices.get(0)[0][0].display();
}
public boolean exists(Object[][] obj, int ix, int iy) {
if (ix >= 0 && ix < obj.length &&
iy >= 0 && iy < obj[0].length) return true;
else return false;
}
}
Two dimensional multi level matrix.
move mouse to change threshold