mirror of
https://github.com/processing/processing4.git
synced 2026-02-03 13:49:18 +01:00
46 lines
743 B
Plaintext
46 lines
743 B
Plaintext
// Code from Visualizing Data, First Edition, Copyright 2008 Ben Fry.
|
|
// Based on the GraphLayout example by Sun Microsystems.
|
|
|
|
|
|
class Edge {
|
|
Node from;
|
|
Node to;
|
|
float len;
|
|
int count;
|
|
|
|
|
|
Edge(Node from, Node to) {
|
|
this.from = from;
|
|
this.to = to;
|
|
this.len = 50;
|
|
}
|
|
|
|
|
|
void increment() {
|
|
count++;
|
|
}
|
|
|
|
|
|
void relax() {
|
|
float vx = to.x - from.x;
|
|
float vy = to.y - from.y;
|
|
float d = mag(vx, vy);
|
|
if (d > 0) {
|
|
float f = (len - d) / (d * 3);
|
|
float dx = f * vx;
|
|
float dy = f * vy;
|
|
to.dx += dx;
|
|
to.dy += dy;
|
|
from.dx -= dx;
|
|
from.dy -= dy;
|
|
}
|
|
}
|
|
|
|
|
|
void draw() {
|
|
stroke(edgeColor);
|
|
strokeWeight(0.35);
|
|
line(from.x, from.y, to.x, to.y);
|
|
}
|
|
}
|