public class Boule {
float x;
float y;
float []X;
float []Y;
float startx;
float starty;
float vitx0;
float vity0;
float vitx;
float vity;
float angle;
float coeff;
int couleur;
int index;
Boule() {
couleur= color(random(255), random(255), random(255));
startx=random(400, 600);
starty=random(400, 600);
vitx0=4-random(4);
vity0=4-random( 4);
coeff=random(1, 15);
X=new float[0];
Y=new float[0];
x=startx;
y=starty;
vitx=vitx0;
vity=vity0;
}
}
import controlP5.*;
ControlP5 cp;
PGraphics feuille;
Boule []boulesArray;
int [][]XX;
int [][]YY;
int nbeBoules;
Button btnFond;
Textfield nbeCyclesInfo;
color fillCol;
color backColor=0;
PApplet app=this;
int count;
float minx;
float miny;
float maxx;
float maxy;
int nbeCycles;
PFont font;
public void setup() {
size(800, 800, P2D);
smooth();
cp=new ControlP5(this);
background(backColor);
font = loadFont("Arial-BoldMT-14.vlw");
textFont(font);
textAlign(CENTER);
frameRate(600);
btnFond=cp.addButton("Background", 10, 10, 10, 60, 20);
btnFond.setColorBackground(255-backColor);
if(backColor==0){
btnFond.setCaptionLabel(" White");
}else{
btnFond.setCaptionLabel(" Black");
}
btnFond.setColorCaptionLabel(color(backColor));
count=0;
nbeCycles=(int)random(300, 3000);
nbeCyclesInfo=cp.addTextfield(str(nbeCycles)+" cycles", 10, 50, 30, 20);
nbeCyclesInfo.setValue(str(count));
minx=600;
miny=600;
maxx=400;
maxy=400;
nbeBoules=(int) random(2, 6);
boulesArray= new Boule[nbeBoules];
XX=new int [nbeBoules][nbeCycles+1];
YY=new int [nbeBoules][nbeCycles+1];
for (int i=0;i<nbeBoules;i++) {
boulesArray[i]=new Boule();
}
fillCol=color(random(255), random(255), random(255));
fill(fillCol, 20);
noStroke();
pre();
count=0;
}
void draw() {
pushMatrix();
float ech=(float)width/max((maxy-miny), (maxx-minx));
scale(ech);
if (maxy-miny<maxx-minx) {
translate(-minx, -(maxy+miny+minx-maxx)/2);
}
else {
translate(-(maxx+minx+miny-maxy)/2, -miny);
}
count++;
if (count<nbeCycles) {
nbeCyclesInfo.setValue(str(count+1));
for (int i=0;i<nbeBoules;i++) {
// fill(boulesArray[i].couleur,30);
// noStroke();
// ellipse(XX[i][count], YY[i][count], 4, 4);
stroke(boulesArray[i].couleur, 45);
if (i<nbeBoules-1) {
//line(boulesArray[i].X[count], boulesArray[i].Y[count], boulesArray[i+1].X[count], boulesArray[i+1].Y[count]);
line(XX[i][count], YY[i][count], XX[i+1][count], YY[i+1][count]);
}
else {
//line(boulesArray[i].X[count], boulesArray[i].Y[count], boulesArray[0].X[count], boulesArray[0].Y[count]);
line(XX[i][count], YY[i][count], XX[0][count], YY[0][count]);
}
}
}
popMatrix();
if (count==nbeCycles+300) {
println(frameRate);
setup();
}
cp.draw();
}
void pre()
{
while (count<nbeCycles) {
count++;
for (int i=0;i<nbeBoules;i++) {
if (i<nbeBoules-1) {
boulesArray[i].angle= atan2(boulesArray[i].y-boulesArray[i+1].y, boulesArray[i].x-boulesArray[i+1].x)-atan2(boulesArray[i].vity, boulesArray[i].vitx);
}//
else {
boulesArray[i].angle=atan2(boulesArray[i].y-boulesArray[0].y, boulesArray[i].x-boulesArray[0].x)-atan2(boulesArray[i].vity, boulesArray[i].vitx);
}
if (abs(boulesArray[i].angle)>PI) {
boulesArray[i].angle=-boulesArray[i].angle;
}
boulesArray[i].angle=-boulesArray[i].angle/boulesArray[i].coeff;
float vitxinstant=boulesArray[i].vitx;
float vityinstant=boulesArray[i].vity;
boulesArray[i].vitx=vitxinstant*cos(boulesArray[i].angle)-vityinstant*sin(boulesArray[i].angle);//+((mouseX-boulesArray[i].x)/100);
boulesArray[i].vity=vityinstant*cos(boulesArray[i].angle)+vitxinstant*sin(boulesArray[i].angle);//+((mouseY-boulesArray[i].y)/100);
boulesArray[i].x+=boulesArray[i].vitx;
boulesArray[i].y+=boulesArray[i].vity;
maxx=max(maxx, boulesArray[i].x);
maxy=max(maxy, boulesArray[i].y);
minx=min(minx, boulesArray[i].x);
miny=min(miny, boulesArray[i].y);
boulesArray[i].X=append(boulesArray[i].X, boulesArray[i].x);
boulesArray[i].Y=append(boulesArray[i].Y, boulesArray[i].y);
XX[i][count]=floor(boulesArray[i].x);
YY[i][count]=floor(boulesArray[i].y);
}
}
}
void mousePressed() {
if (mouseButton==LEFT) {
setup();
}
}
void Background(int value) {
btnFond.setColorBackground(backColor);
backColor=abs(255-backColor);
btnFond.setColorCaptionLabel(color(backColor));
background(backColor);
if(backColor==0){
btnFond.setCaptionLabel(" White");
}else{
btnFond.setCaptionLabel(" Black");
}
}
A little bit different from boules01. This time, points are calculated first then the scene is scaled to fit the "paths".
Click the custom Button to change the background from black to white.
Click anywhere or wait to restart.