ProcessingでGenerative art #44
継承とポリモーフィズムの練習。
Code
ParticleSystem ps;
void setup() {
size(1000, 700);
pixelDensity(2);
smooth();
colorMode(HSB, 360, 100, 100, 100);
background(360);
ps = new ParticleSystem();
}
void draw() {
if (mousePressed) {
ps.addParticle(new PVector(mouseX,mouseY));
}
ps.run();
}
void keyPressed() {
if (key == 's')saveFrame("####.png");
if (key == ' ')background(360);
}
//------------------------------------------------------------------
class Ex_1 extends Particle {
Ex_1(PVector l) {
super(l);
}
void display() {
noFill();
strokeWeight(0.5);
stroke(214, s, b -50, 100-lifespan);
pushMatrix();
translate(pos.x, pos.y);
rotate(angle *0.5);
ellipse(0, r*2, map(lifespan, 100, 0, 0, d), map(lifespan, 100, 0, 0, d));
popMatrix();
}
}
//------------------------------------------------------------------
class Ex_2 extends Particle {
Ex_2(PVector l) {
super(l);
}
void display() {
strokeWeight(0.5);
fill(40, s, 90, 100-lifespan);
stroke(0, 100-lifespan);
pushMatrix();
translate(pos.x, pos.y);
rotate(angle);
rect(0, r*2, d/4, d/2);
popMatrix();
}
}
//------------------------------------------------------------------
class Particle {
PVector pos;
PVector vel;
PVector acc;
float lifespan;
float angle, angleStep;
float d, r;
float s, b;
Particle(PVector l) {
acc = new PVector(random(-0.05, 0.05), random(-0.05, 0.05));
vel = new PVector(0, 0);
pos= l.get();
lifespan = 100.0;
angle = random(PI*2);
r = random(80);
d = random(5, 30);
s = random(100);
b = random(50, 100);
angleStep = random(-0.1, 0.1);
}
void run() {
update();
display();
}
void update() {
vel.add(acc);
pos.add(vel);
lifespan -= 1.0;
angle += angleStep;
}
void display() {
noStroke();
fill(340, s, b);
pushMatrix();
translate(pos.x, pos.y);
rotate(angle);
ellipse(0, r, map(lifespan, 100, 0, 0, d), map(lifespan, 100, 0, 0, d));
popMatrix();
}
boolean isDead() {
if (lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
//------------------------------------------------------------------
import java.util.*;
class ParticleSystem{
ArrayList<Particle> particles;
PVector origin;
ParticleSystem(){
//origin = pos.get();
particles = new ArrayList<Particle>();
}
void addParticle(PVector pos){
origin = pos.get();
float r = random(1);
if (r < 0.33) {
particles.add(new Particle(origin));
}
else if(r < 0.66){
particles.add(new Ex_2(origin));
}
else {
particles.add(new Ex_1(origin));
}
}
void run(){
Iterator<Particle>it = particles.iterator();
while(it.hasNext()){
Particle p = it.next();
p.run();
if(p.isDead()){
it.remove();
}
}
}
}
Happy Coding!!
応援してくださる方!いつでもサポート受け付けてます!