private static void MoveK(int k, List <k> ks, ref bool haveChange) { bool[] kHaveChange = new bool[k]; for (int i = 0; i < ks.Count; i++) { twoDimensionPostion oldKPos = ks[i].pos; //cal avg pos of children int totalx = 0; int totaly = 0; for (int n = 0; n < ks[i].children.Count; n++) { totalx += ks[i].children[n].pos.x; totaly += ks[i].children[n].pos.y; } twoDimensionPostion newKPos = new twoDimensionPostion(ks[i].children.Count > 0?totalx / ks[i].children.Count:totalx, ks[i].children.Count > 0? totaly / ks[i].children.Count:totaly); ks[i].pos = newKPos; ks[i].children = new List <star>(); if (oldKPos.x != newKPos.x || oldKPos.y != newKPos.y) { kHaveChange[i] = true; } else { kHaveChange[i] = false; } } haveChange = false; for (int i = 0; i < kHaveChange.Length; i++) { if (kHaveChange[i]) { haveChange = true; } } }
private static double calDistance(twoDimensionPostion posA, twoDimensionPostion posB) { double a = posA.x - posB.x; double b = posA.y - posB.y; double c = Math.Sqrt(a * a + b * b); return(c); }
public static List <k> GenRandomK(int SideLength, int k) { List <k> ks = new List <k>(); for (int i = 0; i < k; i++) { int kx = rand.Next(SideLength); int ky = rand.Next(SideLength); twoDimensionPostion kp = new twoDimensionPostion(kx, ky); ks.Add(new k(kp)); } return(ks); }
public static List <star> generateStarGrid(int SideLength, int density) { if (density > (SideLength * SideLength)) { throw new Exception("density cannot more than size."); } List <star> stars = new List <star>(); while (stars.Count < density) { twoDimensionPostion newPos = new twoDimensionPostion(rand.Next(SideLength), rand.Next(SideLength)); star s = new star(newPos); stars.Add(s); } return(stars); }
public star(twoDimensionPostion pos) { this.pos = pos; }