class Vertex { String label; Particle p; // for the motion of this vertex int r=10; // radius boolean isLocked=false; // not moving boolean showLabel=false, showLabelOnce=false; Vertex(String label) { this.label = label; } void draw() { if (isLocked) fill(196,0,0); else fill(0); ellipse(p.position().x(), p.position().y(), 2*r,2*r); } void label() { if (isLocked || showLabel || showLabelOnce) { if (isLocked) fill(196,0,0); else fill(0,196,0); text(label, p.position().x(), p.position().y()); showLabelOnce = false; } } void update() { // Bounce off the wall (and lose a little velocity) // From an example at the traer.physics site if (p.position().x() < 0 || p.position().x() > width) p.velocity().set(-0.9*p.velocity().x(), p.velocity().y(), 0); if (p.position().y() < 0 || p.position().y() > height) p.velocity().set(p.velocity().x(), -0.9*p.velocity().y(), 0); p.position().set(constrain(p.position().x(), 0, width), constrain(p.position().y(), 0, height), 0); } } class Edge { Vertex a, b; Edge(Vertex a, Vertex b) { this.a = a; this.b = b; } void draw() { if (a.isLocked || b.isLocked) stroke(196,0,0); else stroke(128); line(a.p.position().x(), a.p.position().y(), b.p.position().x(), b.p.position().y()); } } class Graph { Vertex[] vertices; Edge[] edges; int locked = -1; // which vertex is being dragged (-1 if none) void model(ParticleSystem physics, float G, float k, float damp, float rest) { // Create the particles for (int i=0; i= 0 && mouseButton == LEFT) { // unlock existing lock vertices[locked].isLocked = false; vertices[locked].p.makeFree(); vertices[locked].p.velocity().set(0,0,0); locked = -1; } if (grabbed >= 0) { if (mouseButton == LEFT) { // new lock locked = grabbed; vertices[locked].isLocked = true; vertices[locked].p.makeFixed(); } else if (mouseButton == RIGHT) { // toggle label vertices[grabbed].showLabel = !vertices[grabbed].showLabel; } } } void dragged() { if (locked >= 0) vertices[locked].p.position().set(mouseX,mouseY,0); } void mouseOver() { int over = overVertex(); if (over >= 0) vertices[over].showLabelOnce = true; } // Returns index of vertex which the mouse is over, or -1 if none int overVertex() { int over=-1; for (int i=0; i