xxxxxxxxxx
class P{
float x;
float y;
float vx;
float vy;
float sz; // サイズ
float f; // 円の花形分割数
float d; // 回転角
float dv; // 回転速度
int t;
P(){
}
void init(){
t = random(200);
x = width / 2;
y = height / 2;
float d = random(360);
float s = random(10);
vx = cos(radians(d)) * s;
vy = sin(radians(d)) * s;
sz = random(50) + 10;
f = random(7) + 1;
dv = random(0.2) - 0.1;
}
void act(){
x += vx;
y += vy;
d += dv;
vx *= 0.98;
vy *= 0.98;
t--;
if(t < 0){
init();
}
}
}
ArrayList<P> lists = new ArrayList<P>();
float[] tp = new float[width * height]; // ピクセルカラー保持
void setup(){
size(500, 500);
colorMode(HSB); // (HSBの減算合成ちっくなところが今回のミソ)
for(int i = 0; i < width * height; i++){
tp[i] = 0;
}
for(int i = 0; i < 3; i++){
P p = new P();
lists.add(p);
}
}
void draw(){
// ピクセルカラーを減衰
for(int i = 0; i < width * height; i++){
tp[i] = tp[i] * 0.99;
}
for(P p : lists){
p.act();
// 各パーティクルからの距離に応じてピクセルに色を加算する。
for(int y = 0; y < height; y++){
if(abs(y - p.y) > p.sz){
continue; // 枝きり(縦もしくは横方向に、サイズ以上離れている場合は確実に計算が不要)
}
for(int x = 0; x < width; x++){
if(abs(x - p.x) > p.sz){
continue; // 枝きり
}
float tx = x - p.x;
float ty = y - p.y;
float d = p.sz - sqrt(tx * tx + ty * ty);
float a = atan2(ty ,tx);
d *= abs(cos(a * p.f + p.d));
tp[y * width + x] += d > 0 ? d : 0;
}
}
}
loadPixels();
for(int y = 0; y < height; y++){
for(int x = 0; x < width ; x++){
float t = tp[y * width + x];
pixels[y * width + x] = color(t % 360, t * 5, t * 5);
}
}
updatePixels();
}