Last active
August 29, 2015 14:28
-
-
Save naoyashiga/ff77cdd80cfbd19d537d to your computer and use it in GitHub Desktop.
Trigonometric function(三角関数) ~ Interactive Coding workshop at Tokyo ~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
ellipse(location.x,location.y,r,r); | |
} | |
void walk() { | |
//override in subclass | |
} | |
} | |
interface BaseParticleWithVectorInterface { | |
void walk(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Particle extends BaseParticleWithVector { | |
float angle; | |
float startAngle; | |
PVector center; | |
Particle() { | |
angle = 0; | |
//位相のズレ | |
startAngle = random(PI * 2); | |
//画面の中心 | |
center = new PVector(width / 2, height / 2); | |
//スカラー量の速度 | |
scalarVelocity = 1; | |
velocity.x = random(-scalarVelocity, scalarVelocity); | |
velocity.y = random(-scalarVelocity, scalarVelocity); | |
} | |
void render() { | |
fill(210,252,254,255); | |
noStroke(); | |
angle += PI / 180; | |
//パーティクルの半径を三角関数で操作 | |
r = 8 * abs((float)Math.sin((angle + startAngle) / 2)) + 1; | |
ellipse(location.x,location.y,r,r); | |
} | |
void walk() { | |
//速度を三角関数で操作 | |
//X方向、Y方向の速度を変えて、楕円を描く | |
velocity.x = 10 * (float)Math.cos(angle + startAngle) / 3; | |
velocity.y = 10 * (float)Math.sin(angle + startAngle) / 10; | |
//パーティクルから中心への方向ベクトル | |
PVector direction = PVector.sub(center,location); | |
//中心への方向ベクトルを速度に足す | |
location.x += velocity.x + direction.x / 100; | |
location.y += velocity.y + direction.y / 100; | |
//画面外に出た時の処理 | |
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//可変長配列 | |
ArrayList<Particle> particles; | |
void setup() { | |
//スクリーンの大きさはディスプレイに合わせる | |
size(displayWidth, displayHeight); | |
//パーティクルの数 | |
int particlesSize = 1000; | |
particles = new ArrayList<Particle>(); | |
for (int i = 0; i < particlesSize; i++) { | |
//位置はランダム | |
Particle u = new Particle(); | |
//配列に追加 | |
particles.add(u); | |
} | |
background(0); | |
} | |
void draw() { | |
fill(0); | |
rect(0, 0, width, height); | |
//ArrayListのfor文の省略記法 | |
for (Particle u : particles) { | |
u.walk(); | |
u.render(); | |
} | |
//フレームのキャプチャ保存 | |
// saveFrame("frames/######.tif"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ArrayListにおけるfor文の省略形
下記、2つは同じことをしています。
ArrayList \ Language (API) \ Processing 2+
https://processing.org/reference/ArrayList.html