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.*;
//PGraphics jeos;
Particle[] p = new Particle[40]; //particle array
Particle[] attractor = new Particle [5];
Particle[] staticAttractor = new Particle[5];
void setup(){
size(800,800); //canvas size
/////////////////////////////////////////////////////////////////////////////////////////////////
for(int i=0; i<p.length; i++){ // amount of particles
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++){
//int q = i%3;
//if (q == 0) q =1;
//else q = 0;
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() //continuously executes the lines of code contained inside its block until the program is stopped
{
//background(0); //backgroud colour
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;
//p[i].checkColor(color(255,0,0), color(255));
//p[i].curveTranslate(p[i].pos.x, p[i].pos.y, random(.2, 2), 1.5, 3, 3, .4);
}
else{
p[i].colorT-=.01;
//p[i].curveTranslate(p[i].pos.x, p[i].pos.y, .2, 1.5, 3, 3, .1);
}
/*for(int j=0; j<i%5; j++){
if (i !=j){
float pDist = p[i].pos.distance(p[j].pos);
if (pDist <100){
line (p[i].pos.x, p[i].pos.y, p[j].pos.x, p[j].pos.y);
//line.c1=color(#48D0FF);
}
}
} */
p[i].newAttract(attractor);
p[i].findClosestAttractor(attractor);
p[i].checkColor();
//p[i].addLine();
p[i].drawParticle();
}
//loops through every attractor and draws it to screen
for (int i = 0; i < attractor.length; i++){
attractor[i].newAttract(staticAttractor);
attractor[i].findClosestAttractor(staticAttractor);
//attractor[i].addLine();
attractor[i].checkColor();
attractor[i].drawParticle();
}
for (int i = 0; i < staticAttractor.length; i++){
staticAttractor[i].drawParticle();
}
println(frameCount);
}
/*
void mousePressed(){
save("test5.tif");
}
*/
/*void keyPressed()
{
if (key=='b' || key == 'B')
{
beginRecord(PDF,"JeosAA.pdf");
}
else if (key=='e' || key == 'E')
{
endRecord();
exit();
}
}*/
void mouseClicked() //do the following when mouse clicked
{
setup(); //refresh when mouse is clicked
}
class Particle //class = composite of data; Particle = name of class
{
float w; //radius1
float h; //radius2
int polarity;
float attractionForce;
float accel = .5; //particle acceleratioin
float r; //rotation
float xp, yp;// to help determine the ellipse last location
float t=0; //start
float sinTranslatecount=0;
float curveTranslateCount=0; // global called in order to start the count...
float colorT=0;
float factor = 15.0;
color baseColor;
color lerpToColor;
float prevX, prevY;
Vec closestVec;
Vec pos = new Vec(0,0,0); //center
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; //definition
pos.y=Y; //definition
w=W; //definition
h=H; //definition
r=R; //definition
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(mousePressed == true && shOn== false) shOn = true;//accel*=.95; look here.............
//if( mousePressed == true && shOn == true ) shOn = false;
//if (shOn == true)accel*=.95;
//if (shOn == false)accel*=1.01;
if (d<10) accel =.995;
else accel =1.001;
//else accel*=1.01;
}
v.multiply(attractionForce);
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
void inflate(float rate) // first function
{
w=w*rate;
h=h*rate;
if (w>50) w = 50;
if (h>50) h = 50;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
void checkColor()
{
//float shiftExtent = ((w+h)/2)*factor;
float shiftExtent = 100;
float closeDistance = pos.distance(closestVec);
if (closeDistance < shiftExtent)
{
//colorT = 1 - (closeDistance/shiftExtent);
colorT += .1;
if (colorT > 1) colorT = 1.0;
}
else
{
colorT -= .05;
if (colorT < 0) colorT = 0;
}
}
void drawParticle(){ // rotate function
//checkColor(color(255,0,0), color(255));
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;
}
}
}
}
OpenProcessing is an online community platform devoted to sharing and discussing Processing sketches in a collaborative, open-source environment.
Download Processing
Terms of Service
To contact, send an email to:

See the feedback forum and vote!
Follow OpenProcessing on Twitter.
All sketches are licensed under Creative Commons Attribution-Share Alike 3.0.
Syntax highlighting and Processing brush under LGPL 3.
All the source code is licensed under Creative Commons GNU GPL.
Comments engine by Scriptsmill Comments Script.


