public void maj(List <Nourriture> listn) { foreach (Nourriture n in listn) { if (target == null) { if (n.etat == true && n.avarie == false) { StrategieCourante = new Faim("Faim"); target = n; break; } else if (n.etat == false || n.avarie == true) { StrategieCourante = new Immobile("Immobile"); target = null; } } else { Nourriture nearest = closest(this, listn); if (nearest.etat == true && nearest.avarie == false) { StrategieCourante = new Faim("Faim"); target = nearest; break; } else if (nearest.etat == false || nearest.avarie == true) { StrategieCourante = new Immobile("Immobile"); target = null; } } } }
private static Nourriture closest(Pigeon p1, List <Nourriture> listn) { Nourriture closest = null; double minDist2 = double.MaxValue; foreach (Nourriture n in listn) { double dist2 = Distance2(p1, n); if (dist2 < minDist2) { minDist2 = dist2; closest = n; } } return(closest); }
private static double Distance2(Pigeon p1, Nourriture n2) { return(Pow2(n2.X - p1.X) + Pow2(n2.Y - p1.Y)); }