3
3

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 2012-12-26

四角形をrect()で描画して、(角を中心にして)くるくると回転させるProcessingブログラムです。

void setup() {    
    size(400, 400);    
    frameRate(60);
}

void draw() {    
    background(255);    
    translate(width/2, height/2);    
    rotate(radians(frameCount));    
    rect(0, 0, 60, 60);
}

疑問: 四角形が 1 回転するまでにかかる時間は?

frameCountが360になったときに、丁度360度回転(1回転)となるのだから、draw()が360回呼び出されるのに必要な時間を求めればいいね。いま、frameRate(60) としているから、1秒間に60回draw()が呼ばれることになる。ということは、360(=60*6)回draw()が呼ばれるまでには6秒かかるということなるね。

プログラムを書いて確認してみよう。

int start;

void setup() {    
    size(400, 400);    
    frameRate(30);    
    start = 0;
}

void draw() {    
    background(255);    
    translate(width/2, height/2);    
    rotate(radians(frameCount));    
    rect(0, 0, 60, 60);    
    if (frameCount % 360 == 0) {        
        println(millis() - start);        
        start = millis();    
    }
}

実行結果です。

6552
6002
6000
6002
6000
6001
6002

1 回転に 6000 ミリ秒かかっている。よし、思った通りだ!

じゃあ、今度は frameRate(30) に変えてみよう。そうすると、今度は何秒かかる?

フレームレートを 60 から 30 に変更したのだから、1 回転するまでの時間は、倍の 12 秒かかるはず。

void setup() {    
    size(400, 400);    
    frameRate(30); // フレームレートを30に変更    
    start = 0;
}

実行結果です。

12851
12000
12001
12002
12002
12000
12002

考えたとおりの結果となったね。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?