【Processing】copy()を使ってみる

 

copy()を使ってこんなの作れるかなーと思って書いてみたらできた。

copy(コピー起点x,コピー起点y,起点からの範囲x,起点からの範囲y,

   貼り付けx,貼り付けy,貼り付け範囲x,貼り付け範囲y);

範囲指定はrect()と同じ。

 

PImage img; //画像のオブジェクトを宣言
int x = width/2; //ペースト位置x
int y = height/2; //ペースト位置y
int sx = 0; //コピー起点x
int sy = 0; //コピー起点y
int ex = 0; //コピー終点x
int ey = 0; //コピー終点y
      
void setup(){
  img = loadImage("snj.jpg"); //画像をロードする
  size(559,800);   //画面サイズ
  image(img,0,0); //画像を表示
}

void draw(){
}

void mousePressed(){
sx = mouseX; //起点を設定
sy = mouseY; //起点を設定
}


void mouseReleased(){
ex = mouseX - sx; //終点を設定
ey = mouseY - sy; //終点を設定

//ランダムで10箇所にコピー!!
for(int i = 0 ; i < 10 ; i++ ){
x = int(random(width));
y = int(random(height));
copy(sx,sy,ex,ey,x,y,ex,ey);
}
}