Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created February 5, 2012 07:27
Show Gist options
  • Save tatsuro-ueda/1743798 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/1743798 to your computer and use it in GitHub Desktop.
Processing Examples Basic Arrays
// 「coswav」という配列をつくりコサイン値を入れる
size(200, 200);
float[] coswave = new float[width];
for (int i = 0; i < width; i++) {
// 0~widthを0~πに置き換える
float amount = map(i, 0, width, 0, PI);
// 絶対値(1~0~1)を配列に入れる
coswave[i] = abs(cos(amount));
}
for (int i = 0; i < width; i++) {
stroke(coswave[i]*255);
line(i, 0, i, height/3);
}
for (int i = 0; i < width; i++) {
// 4分の1の階調。よく見るとわかる。
stroke(coswave[i]*255 / 4);
line(i, height/3, i, height/3*2);
}
for (int i = 0; i < width; i++) {
stroke(255 - coswave[i]*255);
// 1番目の反転
line(i, height/3*2, i, height);
}
// 各ドットは画像の中心からの距離で色付けされている
float[][] distances;
float maxDistance;
size(200, 200);
background(0);
maxDistance = dist(width/2, height/2, width, height);
distances = new float[width][height];
// 2次元配列に距離を入れる
for(int i=0; i<height; i++) {
for(int j=0; j<width; j++) {
float dist = dist(width/2, height/2, j, i);
distances[j][i] = dist/maxDistance * 255;
}
}
// 2ドット毎に処理をする(塗りつぶさない)
for(int i=0; i<height; i+=2) {
for(int j=0; j<width; j+=2) {
// 2次元配列の値を使って点を描く
stroke(distances[j][i]);
point(j, i);
}
}
// これは何をやっているんでしょう?誰か教えてください(涙
int unit = 40;
int count;
Module[] mods;
void setup() {
size(320, 240);
background(176);
noStroke();
int wideCount = width / unit;
int highCount = height / unit;
count = wideCount * highCount;
mods = new Module[count];
int index = 0;
for (int y = 0; y < highCount; y++) {
for (int x = 0; x < wideCount; x++) {
mods[index++] = new Module(x*unit, y*unit, unit/2, unit/2, random(0.05, 0.8));
}
}
}
void draw() {
for (int i = 0; i < count; i++) {
mods[i].update();
mods[i].draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment