/// <summary> /// Give the nearest object. /// </summary> /// <param name="objectList"></param> /// <returns></returns> public ReactableObject nearestObject(List <ReactableObject> objectList) { ReactableObject nearestObject = null; double distance = 0; foreach (ReactableObject reactableObject in objectList) { // The first object in the radius of the current object is the nearest. if (nearestObject == null) { nearestObject = reactableObject; distance = distanceCalculation(reactableObject); } // Check if the others object in the radius is the nearest. if (distanceCalculation(reactableObject) < distance) { nearestObject = reactableObject; distance = distanceCalculation(reactableObject); } } return(nearestObject); }
public double distanceCalculation(ReactableObject a) { return(Math.Sqrt((a.x - x) * (a.x - x) + (a.y - y) * (a.y - y))); }