/* Alignment
         * Align the heading of an agent based on those of its neighbors. The
         * force is calculated by generating an average heading vector. We then
         * subtract the agent's heading to get the steering force for the agent.
         *
         * We assume that neighbors represents all of the visible neighbors of the
         * provided agent.
         */
        private static Vector2 ComputeAlignment(Agent agent, List<Flockable> neighbors)
        {
            // A running sum of the given agent's neighbors headings.
            Vector2 headingSum = new Vector2();
            int divisor = 0;

            Vector2 weightedHeading;
            // For each of the given agents neighbors...
            foreach (Flockable otherAgent in neighbors)
            {
                // Ensure that our agent was not counted among its neighbors.
                if (agent.Equals(otherAgent) == false)
                {
                    // Add our neighbors heading to our running sum
                    weightedHeading = Vector2.Multiply(otherAgent.getHeading(), otherAgent.getFlockingWeight());
                    headingSum = Vector2.Add(headingSum, weightedHeading);

                    // Necessary as we allow our given boid to be included in the neighbors.
                    divisor += otherAgent.getFlockingWeight();
                }
            }

            // The average of the neighbor headings.
            // Zero vector in the event that provided agent has no neighbors.
            Vector2 averageHeading = new Vector2();

            if (divisor > 0) // Avoid divide by zero erros.
            {
                // Basic math.
                averageHeading = Vector2.Divide(headingSum, (float) divisor);

                // Subtract the provided agent's heading to produce the steering force.
                // If this is puzzling the most effective way to think about this is to draw a
                // picture of the current heading and the average heading.
                averageHeading = Vector2.Subtract(averageHeading, agent.getHeading());
            }

            // Return steering force necessary to achieve alignment with peers.
            return averageHeading;
        }