-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpringEdge.pde
More file actions
54 lines (43 loc) · 1.37 KB
/
SpringEdge.pde
File metadata and controls
54 lines (43 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Copyright 2005 Sean McCullough
//banksean at yahoo
public class SpringEdge extends Edge {
float k=0.1; //stiffness
float a=100; //natural length. ehmm uh, huh huh stiffness. natural length ;-)
//This edge sublcass apples a spring force between the two nodes it connects
//The spring force formula is F = k(currentLength-a)
//This equation is one-dimensional, and applies to the straight line
//between the two nodes.
public SpringEdge(Node a, Node b) {
super(a, b);
}
public void setNaturalLength(float l) {
a = l;
}
public float getNaturalLength() {
return a;
}
public Vector3D getForceTo() {
float dx = dX();
float dy = dY();
float l = sqrt(dx*dx + dy*dy);
float f = k*(l-a);
return new Vector3D(-f*dx/l, -f*dy/l, 0);
}
public Vector3D getForceFrom() {
float dx = dX();
float dy = dY();
float l = sqrt(dx*dx + dy*dy);
float f = k*(l-a);
return new Vector3D(f*dx/l, f*dy/l, 0);
}
public void draw(float xOffset,float yOffset,float zoom) {
float dx = dX();
float dy = dY();
Vector3D f = getForceFrom();
stroke(255,255,255,64);
strokeWeight(100/a);
line((from.getX()*zoom)+xOffset, (from.getY()*zoom)+yOffset, (to.getX()*zoom)+xOffset, (to.getY()*zoom)+yOffset);
//text(s, from.getX() + dx/2 - textWidth(s)/2, from.getY() + dy/2);
//smooth();
}
}