Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Created May 9, 2013 17:45
Show Gist options
  • Save vanderlin/5549166 to your computer and use it in GitHub Desktop.
Save vanderlin/5549166 to your computer and use it in GitHub Desktop.
int n = 10;
float xpos[] = new float[n];
float ypos[] = new float[n];
// --------------------------------------------------------
void setup() {
size(500, 500);
for (int i=0; i<n; i++) {
xpos[i] = i * 10;
ypos[i] = i * 10;
}
}
// --------------------------------------------------------
void draw() {
background(255);
xpos[0] = mouseX;
ypos[0] = mouseY;
float restLen = 20.0;
for (int i=1; i<n; i++) {
float dx = xpos[i] - xpos[i-1];
float dy = ypos[i] - ypos[i-1];
float d = sqrt(dx*dx + dy*dy);
if (d>0) {
xpos[i] = xpos[i-1] + (dx * restLen) / d;
ypos[i] = ypos[i-1] + (dy * restLen) / d;
}
}
for (int i=1; i<n; i++) {
noStroke();
stroke(0);
line(xpos[i], ypos[i], xpos[i-1], ypos[i-1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment