Skip to content

Instantly share code, notes, and snippets.

@matarillo
Created November 3, 2009 00:57
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/224672 to your computer and use it in GitHub Desktop.
Save matarillo/224672 to your computer and use it in GitHub Desktop.
public class Node
{
public Vector GetReplusiveForce(Node n)
{
// 反発は距離の2乗に反比例 (比例定数 g)
const double g = 500.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;
return new Vector()
{
X = g / d2 * cos,
Y = g / d2 * sin
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment