Skip to content

Instantly share code, notes, and snippets.

@matarillo
Created November 3, 2009 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matarillo/224641 to your computer and use it in GitHub Desktop.
Save matarillo/224641 to your computer and use it in GitHub Desktop.
public class Node
{
public Vector GetSpringForce(Node n)
{
// ばねの力は自然長からの変位に比例 (比例定数 -k, ばねの長さ l)
const double k = 0.1d;
const double l = 60.0d;
double dx = this.R.X - n.R.X;
double dy = this.R.Y - n.R.Y;
double d2 = dx * dx + dy * dy;
if (d2 < double.Epsilon)
{
// 距離0の時は例外として乱数で決定
Random rand = new Random();
return new Vector()
{
X = rand.NextDouble() - 0.5d,
Y = rand.NextDouble() - 0.5d
};
}
double d = Math.Sqrt(d2);
double cos = dx / d;
double sin = dy / d;
double dl = d - l;
return new Vector()
{
X = -k * dl * cos,
Y = -k * dl * sin
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment