IKChain[] ch;
boolean white = false;
float thet = 0;
int chainCount = 400;
int startRad = -5;
float spd = 10;
int len = 35;
int cnt = 40;
void setup()
{
size(700,700);
background(100,0,0);
fill(0);
ch = new IKChain[1000];
//smooth();
stroke(0,5);
noFill();
}
void draw()
{
// background(0);
pushMatrix();
translate(width/2,height/2);
rotate(thet);
translate(-width/2,-height/2);
for(int i = 1; i < ch.length; i++)
{
if(ch[i] != null)
{
ch[i].advance();
ch[i].drawMe();
}
}
//thet+= PI/1500;
popMatrix();
}
void mousePressed()
{
if(mouseButton == RIGHT)
{
noLoop();
// save((int)random(1000)+".tiff");
}
ch = new IKChain[chainCount];
white = !white;
if(white)
stroke(255,25);
else
stroke(0,5);
for(int i = 1; i < ch.length; i++)
{
float theta = TWO_PI/(ch.length+0.0) * i;
float xP = mouseX + cos(theta)*startRad;
float yP = mouseY + sin(theta)*startRad;
ch[i] = new IKChain(xP,yP,theta, spd, len, cnt);
}
}
class IKPoint
{
float x, y;
float d;
ArrayList neighbours;
IKPoint(float xP, float yP,float dst)
{
d = dst;
neighbours = new ArrayList();
this.x = xP;
this.y = yP;
}
void addNeighbour(IKPoint pt)
{
this.neighbours.add(pt);
}
void move(IKPoint parent)
{
makeMove(this, parent);
for(int i = 0; i<neighbours.size();i++)
{
IKPoint pt = (IKPoint)neighbours.get(i);
if(parent!=pt)
pt.move(this);
}
}
void move(float xP, float yP)
{
this.x += xP;
this.y += yP;
for(int i = 0; i<neighbours.size();i++)
{
IKPoint pt = (IKPoint)neighbours.get(i);
pt.move(this);
}
}
void makeMove(IKPoint child, IKPoint parent)
{
float dx = child.x-parent.x;
float dy = child.y-parent.y;
float theta = atan2(dy,dx);
child.x = parent.x+cos(theta)*d;
child.y = parent.y+sin(theta)*d;
}
}
class IKChain
{
IKPoint[] pts;
float vx, vy, theta, speed;
float life = 0;
int maxRan = 180;
IKChain(float xP, float yP, float thet, float spd, float unit, int unitCount)
{
theta = thet;
speed = spd;
vx = cos(theta)*speed;
vy = sin(theta)*speed;
pts = new IKPoint[unitCount];
for(int i = 0; i < pts.length; i++)
{
pts[i] = new IKPoint(xP,yP,unit);
if(i>0)
{
pts[i].addNeighbour(pts[i-1]);
pts[i-1].addNeighbour(pts[i]);
}
}
}
void drawMe()
{
beginShape();
for(int i = 0; i < pts.length; i+=2)
{
if(white)
stroke(255, 10);
else
stroke(0, 10);
// stroke(255);
curveVertex(pts[i].x, pts[i].y);
}
endShape();
}
void advance()
{
life+= .5;
float chaos = PI/random(5,maxRan);
chaos = Math.floor(random(2)) == 0 ? chaos : - chaos;
theta += chaos;
vx = cos(theta)*speed;
vy = sin(theta)*speed;
pts[pts.length-1].move(vx,vy);
}
}