- Show Sketch
/** @peep sketchcode */
int NUM_CIRCLES = 100;
float MIN_RADIUS = 20;
float MAX_RADIUS = 50;
float DELTA_ANGLE = TWO_PI/36;
Circle[] circles;
void setup() {
size(300, 300);
frameRate(10);
background(255);
smooth();
circles = new Circle[NUM_CIRCLES];
for (int i = 0; i < circles.length; i++) {
circles[i] = new Circle(random(width), random(height), random(MIN_RADIUS, MAX_RADIUS));
}
}
void update() {
for (int i = 0; i < circles.length; i++) {
circles[i].update();
}
}
void draw() {
update(); // Update all of the circles
strokeWeight(1);
for (int i = 0; i < circles.length; i++) {
// Get a first circle
Circle circle1 = circles[i];
for (int j = i+1; j < circles.length; j++) {
// Get a second circle
Circle circle2 = circles[j];
// If the circles are touching
if (circle1.touching(circle2)) {
// Calculate the grey value using the map function based on the distance between the circles
stroke(map(circle1.distance(circle2), 0, circle1.radius + circle2.radius, 0, 255));
// Draw a line between the centres of the circles
line(circle1.x, circle1.y, circle2.x, circle2.y);
}
}
}
}
class Circle {
float x;
float y;
float radius;
float heading;
float speed;
Circle(float _x, float _y, float _radius) {
x = _x;
y = _y;
radius = _radius;
heading = random(TWO_PI);
speed = 1;
}
void draw() {
pushStyle();
noFill();
stroke(0);
strokeWeight(3);
ellipseMode(RADIUS);
pushMatrix();
translate(x, y);
rotate(heading);
ellipse(0, 0, radius, radius);
line(0, 0, radius, 0);
popMatrix();
stroke(192, 0, 0, 64);
for (int i = 0; i < circles.length; i++) {
Circle other = circles[i];
if (touching(other)) {
line(x, y, other.x, other.y);
}
}
popStyle();
}
void update() {
behaviour1();
behaviour2();
behaviour3();
behaviour4();
}
void behaviour1() {
// Constant linear motion
float dx = speed * cos(heading);
float dy = speed * sin(heading);
x += dx;
y += dy;
}
void behaviour2() {
// Constrain to surface
if (x < radius) x = radius;
if (y < radius) y = radius;
if (x > width - radius) x = width - radius;
if (y > height - radius) y = height - radius;
}
void behaviour3() {
// While touching another, change direction
for (int i = 0; i < circles.length; i++) {
if (circles[i] != this) {
if (touching(circles[i])) {
heading += random(-DELTA_ANGLE, DELTA_ANGLE);
}
}
}
}
void behaviour4() {
// While touching another, move away from its centre
for (int i = 0; i < circles; i++) {
if (circles[i] != this) {
if (touching(circles[i])) {
Circle other = circles[i];
float d = distance(other);
float dx = (other.x - x)/d;
float dy = (other.y - y)/d;
x -= speed * dx;
y -= speed * dy;
}
}
}
}
void touching(Circle other) {
return (distance(other) < radius + other.radius);
}
float distance(Circle other) {
return dist(x, y, other.x, other.y);
}
}
Nothing in the gallery yet!
Comments
Nobody has said anything yet.