前の記事のパラメータを少し変えて、今度はひらがなバージョン。こうして見ると、ひらがなって味があるよね。
ArrayList letters;
PFont font;
int numLetters;
void setup(){
size(320, 460);
letters = new ArrayList();
numLetters = 50;
font = createFont("Helvetica", 18, true);
smooth();
noLoop();
}
void draw(){
if (letters.size() < numLetters){
letters.add(new Letter());
}
background(0, 40);
boolean allStop = true;
for (int i = 0; i<letters.size(); i++){
Letter l = (Letter) letters.get(i);
l.collide(letters);
l.move();
l.display();
if (allStop && mag(l.spx, l.spy) < 1) allStop = true;
else allStop = false;
}
if (allStop){
fill(204, 255, 0, 90);
textFont(font);
textSize(18);
text("Go!", width/2, height*2/3);
}
}
class Letter
{
char[] alphabet= {'あ','ぃ','い','ぅ','う','ぇ','え','ぉ','お','か','が','き','ぎ','く','ぐ','け','げ','こ','ご','さ','ざ','し','じ','す','ず','せ','ぜ','そ','ぞ','た','だ','ち','ぢ','っ','つ','づ','て','で','と','ど','な','に','ぬ','ね','の','は','ば','ぱ','ひ','び','ぴ','ふ','ぶ','ぷ','へ','べ','ぺ','ほ','ぼ','ぽ','ま','み','む','め','も','ゃ','や','ゅ','ゆ','ょ','よ','ら','り','る','れ','ろ','ゎ','わ','ゐ','ゑ','を','ん'};
char letter;
float x, y, spx, spy, r, tsize;
color c;
float FRICTION = -0.6;
float SPRING = 0.1;
float GRAVITY = 0.03;
boolean on_collition;
public Letter () {
letter = alphabet[int(random(alphabet.length))];
r = random(10, 26);
tsize = r*0.8;
x = width/2;
y = height/3;
spx = random(-0.1, 0.1);
spy = 0.2;
c = randomColor();
on_collition = false;
}
void move() {
spy -= GRAVITY;
x += spx;
y += spy;
if (x + r > width) {
x = width - r;
spx *= FRICTION;
}
else if (x - r < 0) {
x = r;
spx *= FRICTION;
}
if (y - r < 0) {
y = r;
spy *= FRICTION;
} else if (y + r > height*5) {
y = height*5 - r;
spy *= FRICTION;
}
if (mag(spx, spy) < 0.8 && on_collition){
spx = spy = 0;
}
on_collition = false;
}
void bounce() {
float minx = r;
float maxx = width-r;
float maxy = height -r;
if (x < minx || x > maxx){
spx = -spx * FRICTION;
if (x < minx) { x = 2*minx - x;}
else { x = 2*maxx -x; }
}
if (y > maxy){
spy = -spy * FRICTION;
y = 2*maxy - y;
}
}
void collide(ArrayList others) {
for (int i = 0; i<others.size(); i++){
Letter other = (Letter) others.get(i);
if (this == other) return;
float ox = other.x;
float oy = other.y;
float distance = dist(x, y, ox, oy);
float minDist = other.r + r;
if (distance <= minDist){
float angle = atan2(oy-y, ox-x);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - ox) * SPRING;
float ay = (targetY - oy) * SPRING;
spx -= ax;
spy -= ay;
other.spx += ax;
other.spy += ay;
on_collition = true;
}
}
}
void display() {
fill(c, 80);
noStroke();
ellipseMode(CENTER);
ellipse(x, y, r*2, r*2);
textAlign(CENTER);
textSize(tsize);
fill(c);
text(letter, x, y+r/3);
on_collition = false;
}
private color randomColor() {
color c = color(random(255), random(255), random(255));
return c;
}
}
void mousePressed() {
float d = dist(mouseX, mouseY, width/2, height*2/3);
if (d < 100) { setup(); loop(); };
for (int i = 0; i<letters.size(); i++){
Letter letter = (Letter) letters.get(i);
float dd = dist(mouseX, mouseY, letter.x, letter.y);
if (dd < letter.r){
letter.spx += random(-2, 2);
letter.spy += 5;
}
}
}