2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ProcessingAdvent Calendar 2018

Day 22

Processingでシンメトリー

Posted at

#はじめに
Processing Advent Calendar 2018 の記事です。

#実行結果
test.gif
         ↓
test2.gif

#コード

PImage img;
void setup() {
  size(400, 400);
}

void draw() {

  stroke(random(115), random(115), random(115), 40);
  fill(random(155)+100, random(155)+100, random(155)+100, 40);
  rect( random(0, width), random(0, width), random(0, width), 
    random(0, width) );
  symmetry();
}

void symmetry() {

  if (mousePressed) {
    save("test.gif");// 現在のスケッチを保存 
    int j = 0;       // 現在の列
    int w = width/2; // 真ん中  
    int n = w + 1  ; // 次の段へ
    int d = 1;       // シンメトリーにする場所を計算するのに使う

    background(255);
    img = loadImage("test.gif");
    image(img, 0, 0);
    strokeWeight(1.2); //1.2より値が小さいと、透ける
    loadPixels();

    for (int i=0; i<width*height; i++) { 
      if (i<w+(width*j)) {
        color cp = img.pixels[i];
        stroke(cp);
        point(width-d, j);
        d++;
      } else {
        i+=w;
        d=1;
        j++;
      }
    }    
    noLoop();
    save("test2.gif");
  }
}

#解説

pixelsのリファレンス

画像は↓テーブルのような順番で、pixels[]の配列にcolorが格納される。
シンメトリーにしたいので。

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

    ↓

1 2 2 1
5 6 6 5
9 10 10 9
13 14 14 13
こんな風にpoint()で、色を上書きする。

#おわりに
良いお年をお迎えくださいませ。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?