GMU:Processing im Park/Justine Barthel: Difference between revisions

From Medien Wiki
No edit summary
No edit summary
Line 334: Line 334:


Link zu den Bildern:[https://www.flickr.com/photos/138192882@N07/albums/72157663707849795]
Link zu den Bildern:[https://www.flickr.com/photos/138192882@N07/albums/72157663707849795]
* Tile Sorting
[[File:Tilesorting_03.PNG| 300px]]
*Quellcode:
<source lang="java">
PImage img;
int d = 10;
PImage[] tiles;
int rows, cols, n;
void setup() {
  size(500, 500);
  img = loadImage("Sun_01.jpg");
  image (img, 0, 0, 500, 500);
  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);
  for(int i = 0; i < n; i++) {
    PImage tile = tiles[i];
    int y = 50 * (i / cols);
    int x = 50 * (i % cols);
    int d = 5 * (i / cols);
    int e = 50 * (i % cols);
    image(tile, x, y, 50, 50);
    image(tile, d, e);
  }
}
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( saturation(average(tile2)) - saturation(average(tile1)) > mouseX ) {
        PImage tmptile = tiles[pos1];
        tiles[pos1] = tiles[pos2];   
        tiles[pos2] = tmptile;
      }
    }
  }
}
color average(PImage img) {
  img.loadPixels();
  color[] pxls = img.pixels;
  float r = 50, g = 50, b = 50;
  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>
* Weitere Bilder (allerdings mit einem anderen Quellcode [https://www.uni-weimar.de/medien/wiki/GMU:Processing_im_Park/Part3])
[[File:Tilesorting_01.PNG| 300px]]
[[File:Tilesorting_01.PNG| 300px]]