public void CalculateAgentsEnvironmentParameters() { for (int index = 0; index < Agents.Count; index++) { var agent = Agents[index]; var localAgent = agent; var agents = Agents.Except(new List <EnvironmentAgent> { localAgent }).ToList(); var nearAgents = agents.Where( x => x != localAgent && GetDistanceBetweenAgents(x, localAgent) < VisibleZone && (localAgent.Vector.X * (x.X - localAgent.X) + localAgent.Vector.Y * (x.Y - localAgent.Y)) >= 0); var nearFood = Foods.Where(x => GetDistanceBetweenAgents(x, localAgent) < VisibleZone && (localAgent.Vector.X * (x.X - localAgent.X) + localAgent.Vector.Y * (x.Y - localAgent.Y)) >= 0); if (nearAgents.Any()) { localAgent.AgentsNear = true; localAgent.DistanceToNearestAgent = agents.Min(x => GetDistanceBetweenAgents(x, localAgent)); localAgent.NearestAgent = agents.FirstOrDefault( y => GetDistanceBetweenAgents(y, localAgent) == localAgent.DistanceToNearestAgent); } else { localAgent.AgentsNear = false; localAgent.DistanceToNearestAgent = 0; localAgent.NearestAgent = new Agent(); } if (nearFood.Any()) { localAgent.FoodNear = true; localAgent.DistanceToNearestFood = Foods.Min(x => GetDistanceBetweenAgents(x, localAgent)); localAgent.NearestFood = Foods.FirstOrDefault( y => GetDistanceBetweenAgents(y, localAgent) == localAgent.DistanceToNearestFood); } else { localAgent.FoodNear = false; localAgent.DistanceToNearestFood = 0; localAgent.NearestFood = new Food(); } agent.DistanceToNearestVertical = Math.Min(Width - agent.X, agent.X); agent.DistanceToNearestHorizontal = Math.Min(Height - agent.Y, agent.Y); } }