1
2

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.

Processing > はじめの一歩 > ランダムな線の描画

Last updated at Posted at 2016-07-12
動作確認
Windows 7 pro

参考 http://ap.kakoku.net/

openprocessingというブラウザ動作の環境でProcessingを試した。

int counter;
 
void setup() {  //setup function called initially, only once
  size(250, 250);
  background(255);  //set background white
  colorMode(HSB);   //set colors to Hue, Saturation, Brightness mode
  counter = 0;
}
 
void draw() {  //draw function loops
  counter++;
//  noStroke();
  
  stx = random(50);
  sty = random(50);
  wid = random(100);
  hei = random(100);
  line( stx, sty, stx+wid, sty+hei);
  
  if(mousePressed == true) { //add some interaction
      noStroke();
  }
}

マウスクリックした時点で線の追加をやめる実装。
最初から動かしなおす場合は、ブラウザのリロードを使う。

qiita.png

C++ Builder, Visual Studio, Unity以外の描画ツールとして使えるだろうか。。。

v0.2 > クリック時にクリア

参考 https://forum.processing.org/one/topic/how-to-clear-the-screen-after-drawing-something.html

just call background(yourBgColor);

background()で描画したものを消すらしい。

void setup() {  //setup function called initially, only once
  size(250, 250);
  background(255);  //set background white
  colorMode(HSB);   //set colors to Hue, Saturation, Brightness mode
  counter = 0;
}
  
void draw() {  //draw function loops
  counter++;
//  noStroke();
   
  stx = random(50);
  sty = random(50);
  wid = random(100);
  hei = random(100);
  line( stx, sty, stx+wid, sty+hei);
   
  if(mousePressed == true) { //add some interaction
      background(255);
  }
}

(追記 2016/07/13)

Windows用Processing3で実行する場合は上記コードではエラーが出て、以下のように変更が必要だった。
変数の型宣言を明示しないといけないとのこと。
また、random()の戻り値がfloatのためエラーが出た。(int)で対応した。

int counter;

void setup() {  //setup function called initially, only once
  size(250, 250);
  background(255);  //set background white
  colorMode(HSB);   //set colors to Hue, Saturation, Brightness mode
  counter = 0;
}

void draw() {  //draw function loops
  counter++;
//  noStroke();
  int stx, sty, wid, hei;

  stx = (int)random(50);
  sty = (int)random(50);
  wid = (int)random(100);
  hei = (int)random(100);
  line( stx, sty, stx+wid, sty+hei);

  if(mousePressed == true) { //add some interaction
      background(255);
  }
}
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?