- Show Code
- Show Sketch
// Editing The jostling circles to have differing radii
/** @peep sketch **/
/** @peep sketchcode **/
int NUM_CIRCLES = 100;
float MIN_RADIUS = 10;
float MAX_RADIUS = 50;
Circle[] circles;
void setup() {
size(300, 300);
frameRate(10);
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();
background(255);
for (int i = 0; i < circles.length; i++) {
circles[i].draw();
}
}
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 = 10;
}
void update() {
x += random(-1, 1);
y += random(-1, 1);
}
void draw() {
pushStyle();
noFill();
stroke(0);
strokeWeight(1);
ellipseMode(RADIUS);
pushMatrix();
translate(x, y);
ellipse(0, 0, radius, radius);
popMatrix();
popStyle();
}
}
- Show Code
- Show Sketch
//Utilizing Update to create a behaviour 1 function. Thus making circles move in an orientated line
/** @peep sketch **/
/** @peep sketchcode **/
int NUM_CIRCLES = 100;
float MIN_RADIUS = 10;
float MAX_RADIUS = 20;
Circle[] circles;
void setup() {
size(300, 300);
frameRate(10);
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();
background(255);
for (int i = 0; i < circles.length; i++) {
circles[i].draw();
}
}
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 = 10;
}
void draw() {
pushStyle();
noFill();
stroke(0);
strokeWeight(1);
ellipseMode(RADIUS);
pushMatrix();
translate(x, y);
rotate(heading);
ellipse(0, 0, radius, radius);
line(0, 0, radius, 0);
popMatrix();
popStyle();
}
void update() {
behaviour1();
}
void behaviour1() {
// Constant linear motion
float dx = speed * cos(heading);
float dy = speed * sin(heading);
x += dx;
y += dy;
}
}
- Show Code
- Show Sketch
//Editing the speed and max/min radius of process 4 to create different effects
/** @peep sketch **/
/** @peep sketchcode **/
int NUM_CIRCLES = 200;
float MIN_RADIUS = 1;
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(0.5);
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 = 5;
}
void draw() {
pushStyle();
noFill();
stroke(0);
strokeWeight(1);
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);
}
}
- Show Code
- Show Sketch
//Editing the code to be functional in PDE (changed the void return to boolean return and added .length in the for circle function
/** @peep sketch **/
/** @peep sketchcode **/
int NUM_CIRCLES = 200;
float MIN_RADIUS = 10;
float MAX_RADIUS = 20;
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(0.5);
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(1);
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.length; 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;
}
}
}
}
boolean 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.