class Vec{
float x, y, z;
Vec(float X, float Y, float Z){
x = X;
y = Y;
z = Z;
}
void add (Vec v){
x = x + v.x;
y = y + v.y;
z = z + v.z;
}
void subtract(Vec v){
x = x - v.x;
y = y - v.y;
z = z - v.z;
}
void multiply (float m){
x = x*m;
y = y*m;
z = z*m;
}
float distance (Vec v){
float d = sqrt (pow(v.x-x, 2) + pow(v.y-y, 2) + pow(v.z-z, 2));
return d;
}
float magnitute (){
float d = sqrt (pow(x, 2) + pow(y,2 ) + pow(z, 2));
return d;
}
void set(Vec v)
{
x = v.x;
y = v.y;
z = v.z;
}
}
import processing.pdf.*;
Particle[] p = new Particle[40];
Particle[] attractor = new Particle [5];
Particle[] staticAttractor = new Particle[5];
void setup(){
size(800,800);
for(int i=0; i<p.length; i++){
p[i]= new Particle (random(height),random(width),5,5,0,.999,0,color(242,204,47),color(255,0,0));
}
for(int i=0; i< attractor.length; i++){
attractor[i]= new Particle (random(height),random(width),5,5,30,.999,0,color(245,245,245),color(255));
}
for (int i = 0; i < staticAttractor.length; i++)
{
float q = i;
staticAttractor[i] = new Particle(0.333*width,(q/staticAttractor.length)*height,1,1,0,.999,0,color(0),color(0));
}
}
void draw()
{
fill(0,15);
noStroke();
rect(0,0, width, height);
stroke(255);
frameRate(15);
for(int i=0; i<p.length; i++)
{
p[i].xp=100;
p[i].yp=100;
float distance = sqrt(pow(mouseX-p[i].pos.x, 2) + pow(mouseY - p[i].pos.y, 2));
if(distance < 100){
p[i].colorT+=.01;
}
else{
p[i].colorT-=.01;
}
p[i].newAttract(attractor);
p[i].findClosestAttractor(attractor);
p[i].checkColor();
p[i].drawParticle();
}
for (int i = 0; i < attractor.length; i++){
attractor[i].newAttract(staticAttractor);
attractor[i].findClosestAttractor(staticAttractor);
attractor[i].checkColor();
attractor[i].drawParticle();
}
for (int i = 0; i < staticAttractor.length; i++){
staticAttractor[i].drawParticle();
}
println(frameCount);
}
void mouseClicked()
{
setup();
}
class Particle
{
float w;
float h;
int polarity;
float attractionForce;
float accel = .5;
float r;
float xp, yp
float t=0;
float sinTranslatecount=0;
float curveTranslateCount=0;
float colorT=0;
float factor = 15.0;
color baseColor;
color lerpToColor;
float prevX, prevY;
Vec closestVec;
Vec pos = new Vec(0,0,0);
Vec v= new Vec(0,0,0);
Vec ppos;
Particle(float X, float Y, float W, float H, float R,float gravityWeight, int Polarity , color BaseColor, color LerpToColor)
{
pos.x=X;
pos.y=Y;
w=W;
h=H;
r=R;
attractionForce = gravityWeight;
polarity = Polarity;
baseColor = BaseColor;
lerpToColor = LerpToColor;
}
void newAttract(Particle[] a){
for(int i=0; i <a.length; i++){
float d =pos.distance(a[i].pos);
if (a[i].polarity == 0)
{
if(d < 90){
v.x -= accel * (a[i].pos.x - pos.x) / d;
v.y -= accel * (a[i].pos.y - pos.y) / d;
p[i].inflate(1.025);
}
else{
v.x += (accel * (a[i].pos.x - pos.x) / d)/a.length;
v.y += (accel * (a[i].pos.y - pos.y) / d)/a.length;
}
if (accel < .1) accel = .1;
if (accel > 2) accel = 2;
if (d<90) accel =.94;
else accel =1.001;
}
else{
if(d > 90){
v.x += accel * (a[i].pos.x - pos.x) / d;
v.y += accel * (a[i].pos.y - pos.y) / d;
p[i].inflate(.987);
}
else{
v.x -= (accel * (a[i].pos.x - pos.x) / d)/a.length;
v.y -= (accel * (a[i].pos.y - pos.y) / d)/a.length;
}
if (accel < .1) accel = .1;
if (accel > 2) accel = 2;
if (d<10) accel =.995;
else accel =1.001;
}
v.multiply(attractionForce);
}
}
void inflate(float rate)
{
w=w*rate;
h=h*rate;
if (w>50) w = 50;
if (h>50) h = 50;
}
void checkColor()
{
float shiftExtent = 100;
float closeDistance = pos.distance(closestVec);
if (closeDistance < shiftExtent)
{
colorT += .1;
if (colorT > 1) colorT = 1.0;
}
else
{
colorT -= .05;
if (colorT < 0) colorT = 0;
}
}
void drawParticle(){
float s= 60.0/((w+h)/2);
pos.add(v);
for (float t=0; t<=360; t=t+s)
{
float a = pos.x + w/2 * cos( radians(t))* cos( radians(r))-h/2*sin( radians(t))*sin(radians(r));
float b = pos.y + h/2 * sin( radians(t))* cos( radians(r))-h/2*cos( radians(t))*sin(radians(r));
stroke(lerpColor(baseColor,lerpToColor,colorT));
point(a,b);
}
}
void addLine()
{
checkColor();
prevX = pos.x;
prevY = pos.y;
pos.add(v);
stroke(lerpColor(baseColor,lerpToColor,colorT));
line(prevX,prevY,pos.x,pos.y);
}
void findClosestAttractor(Particle[] a)
{
closestVec = a[0].pos;
float d1 = a[0].pos.distance(a[0].pos);
for (int i = 1; i < a.length; i++)
{
float d2 = a[i].pos.distance(a[i].pos);
if (d1 > d2)
{
closestVec = a[i].pos;
d1 = d2;
}
}
}
}