Last active
December 26, 2015 04:28
-
-
Save ohmygodwin/7093013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
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(5,12),random(30, 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() { | |
background(236, 232, 222); | |
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)); | |
} | |
} | |
} | |
// Draw all the internal connections | |
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(136, 166, 129); green | |
stroke(116, 167, 172); | |
line(pi.x,pi.y,pj.x,pj.y); | |
} | |
} | |
} | |
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); | |
} | |
void display() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment