つよくなりたいなあ

地に足をつけず世界を生きぬくブログです

Daily Coding: change color / collage

Generative Designの教本買いました。今日やったスケッチ。

マウスの位置で色が変わる

切り抜いた写真をランダムに配置してコラージュする。これは楽しい。画像は多いほどいいかも。

/*
 * 2018/05/28
 * collage by code
 */

PImage[] imgs;
String[] imageNames;
int imageCount;

CollageItem[] layer1, layer2, layer3;


void setup() {
  size(1024, 800);
  imageMode(CENTER);
  background(255);

  File dir = new File(sketchPath(""), "../sketch_collage_footage");

  if (dir.isDirectory()) {
    String[] contents = dir.list();
    imgs = new PImage[contents.length];
    imageNames = new String[contents.length];
    for (int i = 0; i < contents.length; i++) {

      if (contents[i].charAt(0) == '.') continue;
      else if (contents[i].toLowerCase().endsWith(".png")) {
        File childFile = new File(dir, contents[i]);
        imgs[imageCount] = loadImage(childFile.getPath());
        imageNames[imageCount] = childFile.getName();
        println(imageCount+" "+contents[i]+" "+childFile.getPath());
        imageCount++;
      }
    }
  }

  layer1 = generateCollageItems("layer1", 100, width/2, height/2, width, height, 0.1, 0.5, 0, 0);
  layer2 = generateCollageItems("layer2", 150, width/2, height/2, width, height, 0.1, 0.3, -PI/2, PI/2);
  layer3 = generateCollageItems("layer3", 110, width/2, height/2, width, height, 0.1, 0.85, 0, 0);

  drawCollageItems(layer1);
  drawCollageItems(layer2);
  drawCollageItems(layer3);
}

void draw() {
}

void keyPressed() {
  if (key == 's') {
    saveFrame("PCollage_####.jpg");
  }

  if (key == 'a') layer1 = generateCollageItems("layer1", (int)random(50, 200), width/2, height/2, width, height, 0.1, 0.5, 0, 0);
  if (key == 'b') layer2 = generateCollageItems("layer2", (int)random(25, 300), 200, height*0.75, width, 150, 0.1, random(0.3, 0.8), -PI/2, PI/2);
  if (key == 'c') layer3 = generateCollageItems("layer3", (int)random(50, 300), width/2, height*0.66, width, height*0.66, 0.1, random(0.4, 0.8), -0.05, 0.05);

  background(255);
  drawCollageItems(layer1);
  drawCollageItems(layer2);
  drawCollageItems(layer3);
}

// -----------------------------
class CollageItem {
  float x = 0, y = 0;
  float rotation = 0;
  float scaling = 1;
  int indexToImage = -1;
}

CollageItem[] generateCollageItems(String thePrefix, int theCount, float thePosX, float thePosY, float theRangeX, float theRangeY, float theScaleStart, float theScaleEnd, float therotationStart, float therotationEnd) {
  int[] indexes = new int[0];
  for (int i = 0; i < imageNames.length; i++) {
    if (imageNames[i] != null) {
      if (imageNames[i].startsWith(thePrefix)) {
        indexes = append(indexes, i);
      }
    }
  }

  CollageItem[] items = new CollageItem[theCount];
  for (int i = 0; i < items.length; i++) {
    items[i] = new CollageItem();
    items[i].indexToImage = indexes[i%indexes.length];
    items[i].x = thePosX + random(-theRangeX/2, theRangeX/2);
    items[i].y = thePosY + random(-theRangeY/2, theRangeY/2);
    items[i].scaling = random(theScaleStart, theScaleEnd);
    items[i].rotation = random(therotationStart, therotationEnd);
  }
  return items;
}

void drawCollageItems(CollageItem[] theItems) {
  for (int i = 0; i < theItems.length; i++) {
    pushMatrix();
    translate(theItems[i].x, theItems[i].y);
    rotate(theItems[i].rotation);
    scale(theItems[i].scaling);
    image(imgs[theItems[i].indexToImage], 0, 0);
    popMatrix();
  }
}

File dir = new File(sketchPath(""), "../sketch_collage_footage"); で別フォルダーにレイヤーごとの画像を入れています。

f:id:heyassy:20180528222332j:plain