private static Charge[] startLocations(int count) { Charge[] charges = new Charge[count]; Random rnd = new Random(); charges[0] = new Charge(rnd); for (int i = 1; i < count; i++) { charges[i] = new Charge(rnd); int j = 0; for (; j < i; j++) if (Intersect(charges[i], charges[j])) break; if (j < i) i--; } return charges; }
private static double Distance(Charge ch1, Charge ch2) { return Math.Sqrt(Math.Pow(ch1.x - ch2.x, 2) + Math.Pow(ch1.y - ch2.y, 2)); }
private static bool Intersect(Charge ch1, Charge ch2) { return Math.Abs(ch1.x - ch2.x) <= radius && Math.Abs(ch1.y - ch2.y) <= radius; }
public static Charge[] MoveToCenterAndScale(Charge[] charges, int width, int height) { double minX = charges[0].x; double maxX = charges[0].x; double minY = charges[0].y; double maxY = charges[0].y; foreach (Charge ch in charges) { if (ch.x > maxX) maxX = ch.x; else if (ch.x < minX) minX = ch.x; if (ch.y > maxY) maxY = ch.y; else if (ch.y < minY) minY = ch.y; } double system_width = maxX - minX + radius * 3; double system_height = maxY - minY + radius * 3; double coeff = Math.Min(width / system_width, height / system_height); double mx = (maxX - minX) / 2 + minX; double my = (maxY - minY) / 2 + minY; Charge[] result = new Charge[charges.Length]; Parallel.For(0, result.Length, (i) => { result[i] = new Charge( (int)((charges[i].x - minX) * coeff + radius + 1), (int)((charges[i].y - minY) * coeff + radius + 1)); }); mx = (mx - minX) * coeff + radius + 1; my = (my - minY) * coeff + radius + 1; int dx = (int)(width / 2 - mx); int dy = (int)(height / 2 - my); Parallel.For(0, charges.Length, (i) => { result[i].x += dx; result[i].y += dy; }); return result; }