イメージからアプレットを起動
Lines01を元に、線を伸びていくように描画
FlashのコードではMovieClip._xscale (_yscale)を使ったプロパティの変更だけでコードの書き替えは完了であったが、ProcessingではMovieClipオブジェクトから存在しないので、Lines05などで行ったLineのクラス作成とインスタンス化、さらにそれらに対して、scale()を使ってサイズの変更と、スケール変更に伴う座標位置の調整をpushMatrix()、popMatrix()とtranslate()で行った。
僕自身が座標の扱いに慣れていないため、書いてみれば簡素なコードに気づくまで時間がかかってしまった。
今回のポイントは、Line.pde内に集約されているといっても過言でないと思う。
p42_Lines11.pde
[java]
int leftBounds = 20;
int topBounds = 20;
float x = leftBounds;
float y = topBounds;
int rightBounds = 380;
int bottomBounds = 380;
int baseMax = 20;
int randomMax = 10;
int maxLines = 1000;
int counter = 0;
//int alpha = 0;
Line[] Lines;
//Ellipse[] Ellipses;
void setup(){
size(400, 400);
frameRate(10);
colorMode(HSB, 360, 100, 100);
background(0, 0, 100);
Lines = new Line[maxLines];
// Ellipses = new Ellipse[maxLines];
strokeWeight(1);
smooth();
}
void draw(){
background(0,0,100);
if(counter < maxLines){
float maxDistance = baseMax * random(0, 1) * randomMax;
float minDistance = 2;
int direction = random(0, 1) < 0.5 ? -1 : 1;
float xEnd = x + (direction * (minDistance + ( random(0, 1) * maxDistance)) );
direction = random(0, 1) < 0.5 ? -1 : 1;
float yEnd = y + (direction * (minDistance + ( random(0, 1) * maxDistance)) );
if(xEnd < leftBounds) xEnd = leftBounds;
if(xEnd > rightBounds) xEnd = rightBounds;
if(yEnd < topBounds) yEnd = topBounds;
if(yEnd > bottomBounds) yEnd = bottomBounds;
Lines[counter] = new Line(x, y, xEnd, yEnd);
// Lines[counter].setAlpha(alpha);
// Ellipses[counter] = new Ellipse(x, y, maxDistance * .2, maxDistance * .2);
x = xEnd;
y = yEnd;
}
counter++;
// alpha++;
// if(alpha > 255) alpha = 0;
// if(counter >= 10 && counter
// Lines[counter-10].disappear();
// }
// else if(counter > maxLines+10) noLoop();
if(counter < maxLines){
for(int i=0; i
// Ellipses[i].bdraw();
}
}
else{
for(int i=0; i< maxLines; i++){
Lines[i].bdraw();
// Ellipses[i].bdraw();
}
}
}
void keyPressed(){
if(key == 'r'){
background(255);
counter = 0;
Lines = new Line[maxLines];
// Ellipses = new Ellipse[maxLines];
}
if(key =='q'){
saveFrame("p42_Lines11.jpg");
exit();
}
}
[/java]
Lines.pde
[java]
public class Line{
//member
float x, y, xEnd, yEnd;
int alpha;
int steps;
float[] scl;
//constructor
Line(float x, float y, float xEnd, float yEnd){
this.x = x;
this.y = y;
this.xEnd = xEnd;
this.yEnd = yEnd;
alpha = 255;
steps = 20;
scl = new float[steps];
scl[0] = 0;
for(int i=0; i < steps; i++){
float pre = 100000/(i+1);
scl[i] = pre/100000;
// println(i+" = "+scl[i]);
}
}
//method
void setAlpha(int alpha){
this.alpha = alpha;
bdraw();
}
void bdraw(){
stroke(0, alpha);
if(steps > 0){
pushMatrix();
translate(x, y);
scale(scl[steps-1]);
line(0, 0, xEnd – x, yEnd – y);
popMatrix();
steps–;
}
else
line(x, y, xEnd, yEnd);
}
void disappear(){
x = y = xEnd = yEnd = 1000;
bdraw();
}
}
[/java]
タグ: processing
