-
-
Save vanderlin/5549166 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
| 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