|
|
Line 191: |
Line 191: |
| <br style="clear:both;"> | | <br style="clear:both;"> |
|
| |
|
| | | [http://www.uni-weimar.de/medien/wiki/index.php5?title=GMU:Processing_im_Park/Dirk_Wäsch/Code_Workshop_II Processing v3.0.1 Quellcode] |
| Processing v3.0.1 Quellcode:
| |
| <br>
| |
| <source lang="java">
| |
| PImage img;
| |
| int d = 50;
| |
| PImage[] tiles;
| |
| int rows, cols, n;
| |
|
| |
| void setup() {
| |
| size(1000, 500);
| |
| img = loadImage("https://c2.staticflickr.com/2/1449/24331989911_1044424084_k.jpg");
| |
| cols = (img.width/d);
| |
| rows = (img.height/d);
| |
| n = cols * rows;
| |
| tiles = new PImage[n];
| |
| for (int i = 0; i < n; i++) {
| |
| int x = d * (i % cols);
| |
| int y = d * (i / cols);
| |
| tiles[i] = img.get(x, y, d, d);
| |
| }
| |
| }
| |
|
| |
| | |
| void draw() {
| |
| sort(tiles);
| |
| // show tiles
| |
| for(int i = 0; i < n; i++) {
| |
|
| |
| PImage tile = tiles[i];
| |
|
| |
| int y = d * (i / cols);
| |
| int x = d * (i % cols);
| |
|
| |
| image(tile, x, y, d, d);
| |
|
| |
| }
| |
|
| |
| }
| |
|
| |
| void sort(PImage[] tiles) {
| |
|
| |
| for(int y = 0; y < rows; y++) {
| |
| for(int x = 0; x < cols-1; x++) {
| |
|
| |
| int pos1 = y * cols + x;
| |
| int pos2 = y * cols + (x+1);
| |
|
| |
| PImage tile1 = tiles[pos1];
| |
| PImage tile2 = tiles[pos2];
| |
|
| |
| if( hue(average(tile1)) - hue(average(tile2)) > mouseX ) {
| |
| PImage tmptile = tiles[pos1];
| |
| tiles[pos1] = tiles[pos2];
| |
| tiles[pos2] = tmptile;
| |
| }
| |
|
| |
| }
| |
| }
| |
| }
| |
| | |
| color average(PImage img) {
| |
| img.loadPixels();
| |
| color[] pxls = img.pixels;
| |
|
| |
| float r = 0, g = 0, b = 0;
| |
| int n = pxls.length;
| |
|
| |
| for(int i = 0; i < n; i++) {
| |
| color c = pxls[i];
| |
| r += red(c);
| |
| g += green(c);
| |
| b += blue(c);
| |
| }
| |
| color average = color(r/n, g/n, b/n);
| |
| return average;
| |
| }
| |
| </source><br>
| |
|
| |
|
| == Hausaufgabe VI == | | == Hausaufgabe VI == |