案例代码如下:
import processing.pdf.*;
import java.util.Calendar;
boolean savePDF = false;
int maxCount = 5000;
int currentCount = 1;
float\[\] x=new floatmaxCount;
float\[\] y=new floatmaxCount;
float\[\] r=new floatmaxCount;
void setup(){
size(600,600);
smooth();
frameRate(10);
x0 = width/2;
y0 = height/2;
r0 = 10;
}
void draw(){
if(savePDF)beginRecord(PDF,timestamp()+".pdf");
background(255);
strokeWeight(0.5);
float newR = random(1,7);
float newX = random(0+newR,width-newR);
float newY = random(0+newR,height-newR);
float closestDist = 100000000;
int closestIndex =0;
for(int i=0;i<currentCount;i++){
float newDist = dist(newX,newY,xi,yi);
if(newDist <closestDist){
closestDist =newDist;
closestIndex=i;
}
}
float angle =atan2(newY-yclosestIndex,newX-xclosestIndex);
xcurrentCount =xclosestIndex+cos(angle)*(rclosestIndex+newR);
ycurrentCount =yclosestIndex+sin(angle)*(rclosestIndex+newR);
rcurrentCount = newR;
currentCount++;
for(int i=0;i<currentCount;i++){
fill(50);
ellipse(xi,yi,ri*2,ri*2);
}
if(currentCount++>=maxCount) noLoop();
if(savePDF){
savePDF =false;
endShape();
}
}
void keyReleased(){
if(key=='s'||key=='S') saveFrame(timestamp()+"_##.png");
if(key=='p'||key=='P') savePDF=true;
}
String timestamp(){
Calendar now=Calendar.getInstance();
return String.format("%1ty%1tm%1td_%1tH%1tM%1tS",now);
}
保存并运行如图1

图1