public void CollisionWarning(IDictionary <string, Aircraft> aircrafts) { for (int i = 0; i < aircrafts.Count; i++) { for (int j = i + 1; j < aircrafts.Count; j++) { var ac1 = aircrafts.Values.ElementAt(i); var ac2 = aircrafts.Values.ElementAt(j); if (IsColliding(ac1, ac2)) { if (!_collidingAircrafts.Any(x => x.Item1.Tag == ac1.Tag && x.Item2.Tag == ac2.Tag)) { //This raises the collision event - but only the first time var tempTuple = new Tuple <Aircraft, Aircraft, SeparationEventArgs>(ac1, ac2, new SeparationEventArgs(ac1, ac2)); _collidingAircrafts.Add(tempTuple); SeparationEvent?.Invoke(this, tempTuple.Item3); } } else { if (_collidingAircrafts.Any(x => x.Item1.Tag == ac1.Tag && x.Item2.Tag == ac2.Tag)) { //This removes the collision event. var tempTuple = _collidingAircrafts.Find(x => x.Item1.Tag == ac1.Tag && x.Item2.Tag == ac2.Tag); SeparationAvoidedEvent?.Invoke(this, tempTuple.Item3); _collidingAircrafts.Remove(tempTuple); } } } } }
public void DetectCollision(List <Plane> planes) { //We don't want to deal with empty lists or less than 2 planes if (planes == null || planes.Count < 2) { return; } //Check if any new collisions have occured (The dreaded n^2 loop) for (int i = 0; i < planes.Count - 1; ++i) { for (int j = i + 1; j < planes.Count; ++j) { if (planes[j].Speed == 0) { break; //Break in case of new plane } int differenceInAltitude = planes[j].Altitude - planes[i].Altitude; if (differenceInAltitude < 300 && differenceInAltitude > -300) { //Planes are within 300 meters in altitude, otherwise just ignore double distance = CalculateDistance(planes[i], planes[j]); if (distance < 5000) { //Verify that collision is not already added to list if (Collisions.Contains(Tuple.Create(planes[i], planes[j]))) { break; } Collisions.Add(Tuple.Create(planes[i], planes[j])); SeparationEvent?.Invoke(this, new CollisionEventArgs(planes[i], planes[j])); } } } } }
public void MonitorSeparation(Dictionary <string, List <Track> > tracks) { for (int i = 0; i < tracks.Count; i++) { for (int j = i + 1; j < tracks.Count - i; j++) { if (tracks.ElementAt(i).Value.Count > 1 && tracks.ElementAt(j).Value.Count > 1 && tracks.ElementAt(i + j).Value.Count > 1) { var needSeparation = CheckSeparation(tracks.ElementAt(i).Value[1].Position.X, tracks.ElementAt(i).Value[1].Position.Y, tracks.ElementAt(i + j).Value[1].Position.X, tracks.ElementAt(i + j).Value[1].Position.Y, tracks.ElementAt(i).Value[1].Altitude, tracks.ElementAt(i + j).Value[1].Altitude); if (needSeparation) { SeparationEvent?.Invoke(this, new SeparationEventArgs() { Track1 = tracks.ElementAt(i).Value[1].Tag, TimeOfOccurence = tracks.ElementAt(i).Value[1].TimeStamp, Track2 = tracks.ElementAt(j).Value[1].Tag }); } } } } }
protected virtual void NewSeparationEvent(SeparationEvent e) { SeparationEvent?.Invoke(this, e); }