public void OnNewWatcher(Obserwator watcher) { if (friends.Contains(watcher)) { return; } if (friends.Count == 0) { friends.Add(watcher); } else if (friends.Count == 1) { double distance = Distance(watcher); if (distance < Distance(friends[0])) { friends.Add(watcher); friends[1] = friends[0]; friends[0] = watcher; } else { friends.Add(watcher); } } else { double distance1 = Distance(friends[0]); double distance2 = Distance(friends[1]); double distance = Distance(watcher); if (distance > distance2) { return; } if (distance < distance2 && distance > distance1) { friends[1] = watcher; } else { friends[1] = friends[0]; friends[0] = watcher; } } }
public void Create() { Obserwator obs = new Obserwator(count++); watchers.Add(obs); if (watcherDelegate != null) { foreach (WatcherDelegate del in watcherDelegate.GetInvocationList()) { del(obs); } } obs.Subscribe(this); }
private double Distance(Obserwator watcher) { return(Math.Round(Math.Sqrt(Math.Pow((x - watcher.x), 2) + Math.Pow((y - watcher.y), 2)), 7)); }