Processingでサイン波
超基本(?)ですが、いまさらながらサイン波
そのままではおもしろくないので、X方向に明るさのことなる点を作成して
それをサイン波で動かす。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] col = new int[600]; //点の明るさ格納 | |
float[] y = new float[600]; //点の高さ格納 | |
void setup(){ | |
size(600,300); | |
frameRate(60); | |
for(int i=0; i<width; i++){ | |
col[i] = int(random(50,255)); | |
} | |
} | |
void draw(){ | |
background(0); | |
for(int i=0; i<width; i++){ //点を順番に描画X方向 | |
stroke(col[i]); | |
y[i] = sin(radians(i-frameCount))*height/3; | |
point(i,height/2+y[i]); | |
} | |
} | |
void keyPressed(){ | |
for(int i=0; i<width; i++){ | |
col[i] = int(random(50,255)); | |
} | |
} |
さらに、マウスの位置でインタラクティブに変動するように。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int[] col = new int[600]; //点の明るさ格納 | |
float[] y = new float[600]; //点の高さ格納 | |
int lTime = 0; //ローカルタイム | |
float waveCount = 1.0; //波の数をインタラクティブに変動 | |
float waveSpeed = 1.0; //波の速度をインタラクティブに変動 | |
void setup(){ | |
size(600,300); | |
frameRate(60); | |
for(int i=0; i<width; i++){ | |
col[i] = int(random(50,255)); | |
} | |
} | |
void draw(){ | |
lTime += waveSpeed; //ローカルタイムの更新 | |
waveCount = 1.0 + mouseX*5/width ; | |
waveSpeed = 1.0 + (height-mouseY)/10; | |
background(0); | |
for(int i=0; i<width; i++){ //点を順番に描画X方向 | |
stroke(col[i]); | |
y[i] = sin(radians(i-lTime)*waveCount)*height/3; | |
point(i,height/2+y[i]); | |
} | |
} | |
void keyPressed(){ | |
for(int i=0; i<width; i++){ | |
col[i] = int(random(50,255)); | |
} | |
} |
実行結果↓
ちなみに今回からGist使ってみました!
プログラムを載せるのに便利、色変わるのとか、行番号とか!
こちらのブログを参照させてもらいました。
正直、GitHubのアカウント作ってたけど全然活用してなかった。
でもこれからはBlog用にGist多用すると思われます。
便利なものがたくさんありますね。
2017.2.25
返信削除サンプル2の不具合修正して、Gist更新しました。
プログラムの更新も簡単、やっぱり便利です。