Skip to content

Instantly share code, notes, and snippets.

@naoyashiga
Created February 23, 2016 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoyashiga/b237bb96354d982688e7 to your computer and use it in GitHub Desktop.
Save naoyashiga/b237bb96354d982688e7 to your computer and use it in GitHub Desktop.
Interactive coding勉強会 Easing
class BaseParticleWithVector implements BaseParticleWithVectorInterface {
PVector location;
PVector velocity;
float scalarVelocity;
float r;
BaseParticleWithVector() {
location = new PVector(random(width), random(height));
velocity = new PVector(0, 0);
scalarVelocity = 0;
r = 1;
}
void render() {
fill(255);
noStroke();
}
void walk() {
//override in subclass
}
}
interface BaseParticleWithVectorInterface {
void walk();
}
class Circle {
ArrayList<Particle> particles;
Circle(float circle_radius) {
//パーティクルの数
int particlesSize = 50;
particles = new ArrayList<Particle>();
for (int i = 0; i < particlesSize; i++) {
Particle u = new Particle(circle_radius);
//配列に追加
particles.add(u);
}
}
void render() {
for (Particle u : particles) {
u.walk();
u.render();
}
}
}
//可変長配列
ArrayList<Circle> circles;
void setup() {
//スクリーンの大きさはディスプレイに合わせる
size(displayWidth, displayHeight);
//円の数
int circlesSize = 50;
circles = new ArrayList<Circle>();
for (int i = 0; i < circlesSize; i++) {
Circle u = new Circle(10 * i + 1);
//配列に追加
circles.add(u);
}
background(0);
}
void draw() {
fill(0);
rect(0, 0, width, height);
//ArrayListのfor文の省略記法
for (Circle u : circles) {
u.render();
}
//フレームのキャプチャ保存
// saveFrame("frames/######.tif");
}
class Particle extends BaseParticleWithVector {
float angle;
float startAngle;
float dx = 0;
float R = 100;
PVector center;
// 変化の向
float flag = 1;
float speed = 0.01;
Particle(float circle_radius) {
angle = 0;
R = circle_radius;
//位相のズレ
startAngle = random(0, 2 * PI);
//画面の中心
center = new PVector(circle_radius * 2, height / 2);
}
void render() {
fill(255);
noStroke();
Easing easing = new EasingInQuint();
// 回転角度をEasing
angle = map(easing.get(dx), 0, 1, 0, 2 * PI);
// 円の半径をEasing
r = map(easing.get(dx), 0, 1, 0.5, 1);
ellipse(location.x,location.y,r,r);
if(dx > 1 || dx < 0) {
flag *= -1;
}
dx += speed * flag;
}
void walk() {
location.x = center.x + R * (float)Math.cos(angle + startAngle);
location.y = center.y + R * (float)Math.sin(angle + startAngle);
//画面外に出た時の処理
// if(location.x < 0) {
// location.x += width;
// } else if(location.x > width) {
// location.x -= width;
// }
// if(location.y < 0) {
// location.y += height;
// } else if(location.y > height) {
// location.y -= height;
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment