Skip to content

Instantly share code, notes, and snippets.

@ohmygodwin
Last active December 26, 2015 04:19
Show Gist options
  • Save ohmygodwin/7092399 to your computer and use it in GitHub Desktop.
Save ohmygodwin/7092399 to your computer and use it in GitHub Desktop.
import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
VerletPhysics2D physics;
ArrayList clusters;
boolean showPhysics = true;
boolean showParticles = true;
void setup() {
size(662, 662);
smooth();
frameRate(20);
physics=new VerletPhysics2D();
physics.setWorldBounds(new Rect(10,10,width-20,height-20));
newGraph();
}
void newGraph() {
physics.clear();
clusters = new ArrayList();
for (int i = 0; i < 20; i++) {
Vec2D center = new Vec2D(width/2,height/2);
clusters.add(new Cluster((int) random(7, 15),random(50, 150),center));
}
for (int i = 0; i < clusters.size(); i++) {
for (int j = i+1; j < clusters.size(); j++) {
Cluster ci = (Cluster) clusters.get(i);
Cluster cj = (Cluster) clusters.get(j);
ci.connect(cj);
}
}
}
void draw() {
physics.update();
if (showParticles) {
for (int i = 0; i < clusters.size(); i++) {
Cluster c = (Cluster) clusters.get(i);
c.display();
}
}
if (showPhysics) {
for (int i = 0; i < clusters.size(); i++) {
Cluster ci = (Cluster) clusters.get(i);
ci.showConnections();
for (int j = i+1; j < clusters.size(); j++) {
Cluster cj = (Cluster) clusters.get(j);
ci.showConnections(cj);
}
}
}
}
void mousePressed() {
if (mousePressed) {
newGraph();
}
}
// The Nature of Code
// <http://www.shiffman.net/teaching/nature>
// Spring 2010
// Toxiclibs example: http://toxiclibs.org/
// Force directed graph
// Heavily based on: http://code.google.com/p/fidgen/
class Cluster {
ArrayList nodes;
float diameter;
Cluster(int n, float d, Vec2D center) {
nodes = new ArrayList();
diameter = d;
for (int i = 0; i < n; i++) {
nodes.add(new Node(center.add(Vec2D.randomVector())));
}
for (int i = 1; i < nodes.size(); i++) {
VerletParticle2D pi = (VerletParticle2D) nodes.get(i);
for (int j = 0; j < i; j++) {
VerletParticle2D pj = (VerletParticle2D) nodes.get(j);
physics.addSpring(new VerletSpring2D(pi,pj,diameter,0.01));
}
}
}
void display() {
for (int i = 0; i < nodes.size(); i++) {
Node n = (Node) nodes.get(i);
n.display();
}
}
void connect(Cluster other) {
ArrayList otherNodes = other.getNodes();
for (int i = 0; i < nodes.size(); i++) {
VerletParticle2D pi = (VerletParticle2D) nodes.get(i);
for (int j = 0; j < otherNodes.size(); j++) {
VerletParticle2D pj = (VerletParticle2D) otherNodes.get(j);
physics.addSpring(new VerletMinDistanceSpring2D(pi,pj,(diameter+other.diameter)*0.5,0.05));
}
}
}
void showConnections() {
stroke(0,150);
for (int i = 0; i < nodes.size(); i++) {
VerletParticle2D pi = (VerletParticle2D) nodes.get(i);
for (int j = i+1; j < nodes.size(); j++) {
VerletParticle2D pj = (VerletParticle2D) nodes.get(j);
stroke(255);
}
}
}
void showConnections(Cluster other) {
stroke(0,50);
ArrayList otherNodes = other.getNodes();
for (int i = 0; i < nodes.size(); i++) {
VerletParticle2D pi = (VerletParticle2D) nodes.get(i);
for (int j = 0; j < otherNodes.size(); j++) {
VerletParticle2D pj = (VerletParticle2D) otherNodes.get(j);
}
}
}
ArrayList getNodes() {
return nodes;
}
}
class Node extends VerletParticle2D {
Node(Vec2D pos) {
super(pos);
}
ArrayList<SquareSpin> squares;
void display() {
squares = new ArrayList<SquareSpin>();
squares.add(new SquareSpin(x, y));
for (int i = 0; i < squares.size(); i++) {
SquareSpin sq = squares.get(i);
sq.go();
sq.show();
}
}
ArrayList getSquares() {
return squares;
}
}
class Spin {
float speed = random(-0.05, 0.05);
float angle = 0;
float d;
float x, y;
Spin(float xpos, float ypos) {
x = xpos;
y = ypos;
}
void go() {
angle += speed;
}
void updateSize(float size) {
d = size;
}
}
class SquareSpin extends Spin {
SquareSpin(float x, float y) {
super(x, y);
}
void show() {
rotate(angle);
rectMode(CENTER);
noStroke();
fill(50, 50, 50, 2);
rect(x, y, 50, 50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment