从openProcessing.org上面拷来的一个抽象平面图
代码:
/* Based on CREATIVE CODING course, MONASH UNIVERSITY
MOVE CIRCLES IN A STRIGHT LINE AND CONSTRAIN TO SURFACE.
IF TWO CIRCLES INTERSECT DRAW A LINE CONNECTING THEIR CENTERS,
CLOURING THE LINE BASED ON THE CIRCLE BEING ODD OR EVEN
ARROW UP = INCREASE RADIUS OF CIRCLE
ARROW DOWN = DECREASE RADIUS OF CIRCLE */
int n = 50; // NUMBER OF CIRCLES
float[] x = new float[n];
float[] y = new float[n];
float[] d = new float[n]; // RADIUS OF THE CIRCLES
float[] moveX = new float[n];
float[] moveY = new float[n];
void setup() {
size(800, 500);
background(255);
stroke(0, 10);
noFill();
smooth();
for (int i = 0; i<n; i++) { //ASSIGN VALUES TO VARIABLES
x[i]=random(width);
y[i]=random(height);
d[i]=random(20, 80);
// CALL FUNCTIONS
moveX[i]=random(-1, 1);
moveY[i]=random(-1, 1);
}
println(d); //SHOW RADIUS
}
void draw() {
for (int i = 0; i<n; i++) {
moveCircle(i);
connectCircle(i);
}
}
//####### FUNTIONS ###############
// MOVE CIRCLES IN A STRIGHT LINE AND CONSTRAIN TO SURFACE
void moveCircle(int i) {
x[i]=x[i]+moveX[i];
y[i]=y[i]+moveY[i];
// ellipse(x[i], y[i], d[i], d[i]);
if (x[i]>width || x[i]<0) {
moveX[i]= moveX[i]>0 ? -random(-1, 1) : random(-1, 1);
}
if (y[i]>height || y[i]<0) {
moveY[i] = moveY[i]>0 ? -random(-1, 1) : random(-1, 1);
}
}
// IF TWO CIRCLES INTERSECT THEN DRAW A LINE CONNECTING THEIR CENTERS,
//CLOURING THE LINE BASED ON THE CIRCLE BEING ODD OR EVEN
void connectCircle(int i) {
for (int j = 0; j<n; j++) {
if (j!=i) {
if (dist(x[i], y[i], x[j], y[j])<(d[i]+d[j])) {
if (i%2==0) {
stroke(255, 10);
} else {
stroke(0, 10);
}
line(x[i], y[i], x[j], y[j]);
}
}
}
}
// MODIFY RADIUS OF CIRCLES, SAVE AND REFRESH
void keyPressed() {
if (keyCode == UP) {
for (int i = 0; i <n; i++) {
d[i]++;
}
println(d);
}
if (keyCode == DOWN) {
for (int i = 0; i <n; i++) {
d[i]--;
}
println(d);
}
if (key == 'r') {
background(255);
}
if (key == 's') {
saveFrame("######.png");
}
}