int currentDrawingWormIndex;
sm.spawnItf(width/2,height/2,0.001);
Worm currentW = (Worm) sm.worms.get(currentDrawingWormIndex);
currentW.spawnV(mouseX, mouseY);
currentDrawingWormIndex = sm.worms.size()-1;
Interference(float initX,float initY,float force){
loc = new PVector(initX,initY);
for (int i=0;i<worms.size();i++) {
Worm eachW = (Worm) worms.get(i);
for (int j=i+1;j<worms.size();j++) {
Worm repWorm = (Worm) worms.get(j);
eachW.repelWithOther(repWorm);
for (int k=0;k<itfs.size();k++) {
Interference eachItf = (Interference) itfs.get(k);
eachW.interferedBy(eachItf, eachItf.force);
for (int i=0;i<worms.size();i++) {
Worm eachW = (Worm) worms.get(i);
for (int i = worms.size()-1;i>=0;i--){
Worm eachW = (Worm) worms.get(i);
if(eachW.vertices.size()<=3){
Worm currentW = (Worm) worms.get(currentDrawingWormIndex);
int lastVertexIndex = currentW.vertices.size()-2;
if (lastVertexIndex>=0) {
Vertex currentVertex = (Vertex)currentW.vertices.get(lastVertexIndex+1);
Vertex lastVertex = (Vertex)currentW.vertices.get(lastVertexIndex);
PVector cSeg1v1 = new PVector(currentVertex.loc.x, currentVertex.loc.y);
PVector cSeg1v2 = new PVector(lastVertex.loc.x, lastVertex.loc.y);
for (int i=0;i<worms.size();i++) {
if (i==currentDrawingWormIndex) {
Worm eachW = (Worm) worms.get(i);
for (int j=1;j<eachW.vertices.size()-2;j++) {
Vertex v1 = (Vertex)eachW.vertices.get(j);
Vertex v2 = (Vertex)eachW.vertices.get(j+1);
PVector cSeg2v1 = new PVector(v1.loc.x, v1.loc.y);
PVector cSeg2v2 = new PVector(v2.loc.x, v2.loc.y);
if (intersect(cSeg1v1, cSeg1v2, cSeg2v1, cSeg2v2)) {
Worm cutOffSeg = new Worm();
for (int k=j;k<eachW.vertices.size();k++) {
Vertex ogV = (Vertex)eachW.vertices.get(k);
addedV = new Vertex((v1.loc.x+v2.loc.x)*0.5, (v1.loc.y+v2.loc.y)*0.5);
addedV = new Vertex(ogV.loc.x, ogV.loc.y);
cutOffSeg.vertices.add(addedV);
v1.loc.x = (v1.loc.x+v2.loc.x)*0.5;
v1.loc.y = (v1.loc.y+v2.loc.y)*0.5;
for (int k=eachW.vertices.size()-1;k>j+1;k--) {
eachW.vertices.remove(k);
boolean intersect(PVector s1v1, PVector s1v2, PVector s2v1, PVector s2v2) {
float denominator = (s1v1.x-s1v2.x)*(s2v1.y-s2v2.y)-(s1v1.y-s1v2.y)*(s2v1.x-s2v2.x);
float istX = ((s1v1.x*s1v2.y-s1v2.x*s1v1.y)*(s2v1.x-s2v2.x)-(s1v1.x-s1v2.x)*(s2v1.x*s2v2.y-s2v2.x*s2v1.y))/denominator;
float istY = ((s1v1.x*s1v2.y-s1v2.x*s1v1.y)*(s2v1.y-s2v2.y)-(s1v1.y-s1v2.y)*(s2v1.x*s2v2.y-s2v2.x*s2v1.y))/denominator;
if ( (istX-s1v1.x)*(istX-s1v2.x)<=0 && (istY-s1v1.y)*(istY-s1v2.y)<=0 && (istX-s2v1.x)*(istX-s2v2.x)<=0 && (istY-s2v1.y)*(istY-s2v2.y)<=0 ) {
Worm addedW = new Worm();
void spawnItf(float initX, float initY, float force) {
Interference addedItf = new Interference(initX, initY, force);
float thres,thresT,decay;
Vertex(float initX,float initY){
loc = new PVector(initX,initY);
thres = lerp(thres,thresT,0.1);
PVector dir = new PVector(0, 0);
dir = PVector.sub(loc, repV.loc);
repForce = 1/(dir.mag()+1);
void interferedBy(Interference itf, float force){
PVector dir = new PVector(0, 0);
dir = PVector.sub(itf.loc, loc);
dir.mult(distance*force);
void tensedBy(Vertex v1,Vertex v2, float force){
PVector mid = new PVector((v1.loc.x+v2.loc.x)*0.5,(v1.loc.y+v2.loc.y)*0.5);
PVector dir = new PVector(0, 0);
dir = PVector.sub(mid, loc);
dir.mult(distance*force);
ellipse(loc.x,loc.y,5,5);
vertices = new ArrayList();
for (int i=0;i<vertices.size();i++) {
Vertex eachV = (Vertex) vertices.get(i);
for (int i=1;i<vertices.size()-1;i++) {
Vertex eachV = (Vertex) vertices.get(i);
for (int j=i+1;j<vertices.size()-1;j++) {
Vertex repV = (Vertex) vertices.get(j);
void repelWithOther(Worm repWorm) {
for (int i=1;i<vertices.size()-1;i++) {
Vertex eachV = (Vertex) vertices.get(i);
for (int j=1;j<repWorm.vertices.size()-1;j++) {
Vertex repV = (Vertex) repWorm.vertices.get(j);
for (int i=1;i<vertices.size()-1;i++) {
Vertex eachV = (Vertex) vertices.get(i);
Vertex pV1 = (Vertex) vertices.get(i-1);
Vertex nV1 = (Vertex) vertices.get(i+1);
eachV.tensedBy(pV1,nV1,0.01);
if(i>1&&i<vertices.size()-2){
Vertex pV2 = (Vertex) vertices.get(i-2);
Vertex nV2 = (Vertex) vertices.get(i+2);
eachV.tensedBy(pV1,nV2,0.005);
eachV.tensedBy(pV2,nV1,0.005);
eachV.tensedBy(pV2,nV2,0.005);
void interferedBy(Interference itf, float force) {
for (int i=0;i<vertices.size();i++) {
Vertex eachV = (Vertex) vertices.get(i);
eachV.interferedBy(itf, force);
for (int i=1;i<vertices.size()-2;i++) {
Vertex control1 = (Vertex) vertices.get(i-1);
Vertex draw1 = (Vertex) vertices.get(i);
Vertex draw2 = (Vertex) vertices.get(i+1);
Vertex control2 = (Vertex) vertices.get(i+2);
curve(control1.loc.x, control1.loc.y, draw1.loc.x, draw1.loc.y, draw2.loc.x, draw2.loc.y, control2.loc.x, control2.loc.y);
void spawnV(float initX, float initY) {
if (vertices.size()==0) {
Vertex addedV = new Vertex(initX, initY);
Vertex lastV = (Vertex) vertices.get(vertices.size()-1);
if (dist(initX, initY, lastV.loc.x, lastV.loc.y)>=5) {
Vertex addedV = new Vertex(initX, initY);