// Seitenlänge des Quadrats in Pixeln, max: 500 int cfgDesiredSize = 200; // Anzahl an Feldern pro Reihe/Spalte int cfgPixels = 12; // ----- do not edit below this line ----- // Pixelgröße int cfgPixelSize; // Anzahl Pixel int cfgPixelCount; // Tatsächliche Quadratgröße int cfgSize; // Wahrscheinlichkeit für weiße Pixel float cfgProbability = 0.5; // Anzahl möglicher Bilder int cfgSquareCount; // Außenabstände int cfgPos; // Pixel-Array boolean[] pix = new boolean[1]; // Geschwindigkeit (alle X frames wird neu gezeichnet) int cfgSkipFrames = 1; // Counter (für systematisches zeichnen) int counter = 0; // Colors color colDark = color(67,75,72); color colMid = color(68,129,106); color colLight = color(0,255,159); // Button clicked indicator boolean click = false; // Werte setzen void updateConfig() { cfgPixelSize = cfgDesiredSize / cfgPixels; cfgPixelCount = cfgPixels*cfgPixels; cfgSize = cfgPixelSize * cfgPixels; cfgSquareCount = int(pow(2,cfgPixelCount)); cfgPos = (500 - cfgSize) / 2; if (pix.length != cfgPixelCount) { pix = new boolean[cfgPixelCount]; counter = 0; } } // Init vars HScrollbar hs1, hs2, hs3, hs4; ButtonGroup create, drift; Button create1, create2, create3, drift1, drift2, drift3; void setup() { size(900, 500); background(67,75,72); fill(40,45,43); noStroke(); rect(500,0,500,500); frameRate(40); updateConfig(); PFont font; font = loadFont("TeachersPetSansSerifBold-8.vlw"); textFont(font, 8); fill(colMid); text("Image Creation", 550, 60); text("Drift", 550, 135); create = new ButtonGroup(); create.add(550, 70, 90, 28, "Random"); create.add(655, 70, 90, 28, "Systematic"); // create.add(760, 70, 90, 28, "Automatic"); drift = new ButtonGroup(); drift.add(550, 145, 90, 28, "Row"); drift.add(655, 145, 90, 28, "Diagonal"); drift.add(760, 145, 90, 28, "Off"); hs1 = new HScrollbar(550, 220, 300, 10, 1*5+1, "Size"); hs2 = new HScrollbar(550, 270, 300, 10, 1*5+1, "Pixelcount"); hs3 = new HScrollbar(550, 320, 300, 10, 1*5+1, "Speed"); hs4 = new HScrollbar(550, 370, 300, 10, 1*5+1, "Brightness"); } boolean init = true; int frameNum = 0; void draw() { // Scrollbars hs1.update(); hs2.update(); hs3.update(); hs4.update(); // Buttons create.update(); drift.update(); // Set Config cfgDesiredSize = round(hs1.getValue()*470)+30; // Falls Pixelcount im Drift geändert wird -> Drift beenden! if ((drift.getClicked() == 0 || drift.getClicked() == 1) && (round(hs2.getValue()*28)+2 != cfgPixels)) { drift.setClicked(2); } cfgPixels = round(hs2.getValue()*28)+2; cfgSkipFrames = 20 - floor(hs3.getValue()*20); cfgProbability = 1 - hs4.getValue(); updateConfig(); if (frameNum >= cfgSkipFrames) { // Counter zurücksetzen frameNum = 0; // clear stage fill(67,75,72); noStroke(); rect(0,0,500,500); // Zeichnen switch(drift.getClicked()) { case 0: rowDrift(); break; case 1: diagonalDrift(); break; case 2: case -1: switch(create.getClicked()) { case 0: randomSquare(cfgProbability); break; case 1: systematicSquare(); break; } break; } } else { frameNum++; } if (counter > cfgSquareCount) { counter = 0; } // Mausklick zurücksetzen click = false; } /* ---------------- HELPER FUNCTIONS ---------------- */ // Mausklick abfangen void mouseReleased() { click = true; } // X/Y-Koordinate zu Pixel-ID ausgeben int[] getCoords(int id) { int x = id % cfgPixels; int y = floor(id / cfgPixels); int[] coords = new int[2]; coords[0] = x; coords[1] = y; return coords; } // ID des Pixel zu den gegebenen Koordinaten int getID(int x, int y) { return (y * cfgPixels) + x; } // Grundfläche schwarz einfärben void drawBlankSquare() { fill(0); rect(cfgPos,cfgPos,cfgSize,cfgSize); } void drawSquare() { for(int i=0;i0) { switchPixel(cnt/cfgPixelCount); println(cnt/cfgPixelCount); cnt-=cfgPixelCount; } */ if (counter < cfgPixelCount) { drawSquare(); drawPixel(counter,true); } else { drawSquare(); int id = counter%cfgPixelCount; switchPixel(id); if (id+1 < cfgPixelCount) { switchPixel(id+1); } } counter++; } /* Zufällig zeichnen */ void randomSquare(float p) { for (int id=0;id= p) { col = true; } drawPixel(id,col); } } /* Zeilendrift */ void rowDrift() { drawSquare(); boolean first = true; for(int i=0;i= pix.length) { n = 0; stopIteration = true; } if (pix[n] == true) { drawPixel(n,false); black = false; if (stopIteration) { i = n; } else { i = pix.length; } } } // } } } } /* Diagonaldrift */ void diagonalDrift() { drawSquare(); int i = 0; int cnt = 0; while (cnt < pix.length) { int nextid = getNextDiagonalID(i); int nextnextid = getNextDiagonalID(nextid); // 3 aufeinanderfolgende schwarze felder suchen if (pix[i] == false && pix[nextid] == false && pix[nextnextid] == false) { // umfärben in weiß drawPixel(i,true); // nächstes weißes feld suchen und schwarz färben boolean black = true; int n = i; while (black) { n = getNextDiagonalID(n); if (pix[n]) { drawPixel(n,false); black = false; i = n; } } } else { i = getNextDiagonalID(i); } cnt++; } } int getNextDiagonalID(int id) { if (id == pix.length - 1) { return 0; } int x, y, nextid; int[] coords = new int[2]; coords = getCoords(id); if (coords[1] == 0 || coords[0] == cfgPixels-1) { // Pixel am Rand x = min(max(coords[0] + coords[1] - cfgPixels + 2, 0),cfgPixels - 1); // x = floor(coords[0] / (cfgPixels - 1)) * (coords[1] + 1); y = min(coords[0] + 1, cfgPixels - 1); println(x + " from id " + id); nextid = getID(x,y); } else { // Pixel innerhalb des Quadrats nextid = id - cfgPixels + 1; } return nextid; } /* --------- INTERFACE --------- */ /* Schieberegler */ class HScrollbar { int swidth, sheight; // width and height of bar int xpos, ypos; // x and y position of bar float spos, newspos; // x position of slider int sposMin, sposMax; // max and min values of slider int loose; // how loose/heavy boolean over; // is the mouse over the slider? boolean locked; String label; HScrollbar (int xp, int yp, int sw, int sh, int l, String ilabel) { swidth = sw; sheight = sh; xpos = xp; ypos = (yp-sheight/2)+10; spos = xpos + swidth/2 - sheight/2; newspos = spos; sposMin = xpos; sposMax = xpos + swidth - sheight; loose = l; label = ilabel; } void update() { if(over()) { over = true; } else { over = false; } if(mousePressed && over) { locked = true; } if(!mousePressed) { locked = false; } if(locked) { newspos = constrain(mouseX-sheight/2, sposMin, sposMax); } if(abs(newspos - spos) > 1) { spos = spos + (newspos-spos)/loose; } this.draw(); } int constrain(int val, int minv, int maxv) { return min(max(val, minv), maxv); } boolean over() { if(mouseX > xpos && mouseX < xpos+swidth && mouseY > ypos && mouseY < ypos+sheight) { return true; } else { return false; } } void draw() { noStroke(); fill(colMid); text(label, xpos, ypos-10); fill(colDark); rect(xpos, ypos, swidth, sheight); if(over || locked) { fill(colLight); } else { fill(colMid); } rect(spos, ypos, sheight, sheight); } float getPos() { // convert spos to be values between // 0 and the total width of the scrollbar return spos - xpos; } float getValue() { return this.getPos() / (this.swidth - this.sheight); } } /* Zusammenhängende Buttons */ class ButtonGroup { GroupedButton[] buttons = new GroupedButton[9]; int buttoncount = 0; int pressed = -1; ButtonGroup() { // } void add(int ix, int iy, int iw, int ih, String ilabel) { int i = this.buttoncount; this.buttons[i] = new GroupedButton(ix, iy, iw, ih, ilabel, this); this.buttoncount++; } void clicked(GroupedButton btn) { for (int i=0;i -2 && btn < this.buttoncount) { this.buttons[btn].locked = true; this.clicked(buttons[btn]); } } } /* Einzelner Button, der einer Button-Gruppe angehört */ class GroupedButton { boolean over = false; boolean locked = false; int x; int y; int w; int h; String label; ButtonGroup group; GroupedButton(int ix, int iy, int iw, int ih, String ilabel, ButtonGroup igroup) { x = ix; y = iy; w = iw; h = ih; label = ilabel; group = igroup; } void update() { if(over() && click) { toggle(); click = false; } this.draw(); } void toggle() { if (locked) { locked = false; group.clicked(false); } else { locked = true; group.clicked(this); } } boolean over() { if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) { over = true; return true; } else { over = false; return false; } } boolean getStatus() { if (locked) { return true; } else { return false; } } void switchOff() { this.locked = false; this.draw(); } void draw() { // Draw Rect fill(colDark); stroke(colDark); if (this.over) { fill(colMid); stroke(colMid); } if (this.locked) { fill(colMid); stroke(colLight); } rect(x,y,w,h); // Draw Label fill(colMid); noStroke(); if (this.over) { fill(colLight); } if (this.locked) { fill(colLight); } text(label,x+10,y+18); } }