Beispiel #1
0
        /// <summary>
        /// When given an asteroid, calculate the number of asteroids visible (ie not occluded) from this object.
        /// Then update the "AsteroidsVisible" property on the input object with that count.
        /// </summary>
        /// <param name="targetAsteroid"></param>
        internal void UpdateAsteroidsInFOV(Asteroid targetAsteroid)
        {
            HashSet <double> angles = new HashSet <double>();

            foreach (var asteroid in Asteroids)
            {
                if (asteroid == targetAsteroid)
                {
                    continue;
                }

                double slope = targetAsteroid.GetAngle(asteroid);
                angles.Add(slope);
            }

            targetAsteroid.AsteroidsVisible = angles.Count;
        }
Beispiel #2
0
        /// <summary>
        /// A helper function that generates a SortedDictionary containing all valid angles between the source object
        /// and any neighbour Asteroids. Each angle in the SortedDictionary contains a SortedList sorted on the distance
        /// between the source and neighrbour object.
        /// Distance is actualy the square of the distance to avoid the expensive sqrt operation.
        /// </summary>
        /// <param name="sourceAsteroid"></param>
        private SortedDictionary <double, SortedList <double, Asteroid> > GetDictionaryOfNeighbours(Asteroid sourceAsteroid)
        {
            SortedDictionary <double, SortedList <double, Asteroid> > anglesWithNeighbors = new SortedDictionary <double, SortedList <double, Asteroid> >();

            foreach (Asteroid asteroid in this.Asteroids)
            {
                if (asteroid == sourceAsteroid)
                {
                    continue;
                }
                double angle = sourceAsteroid.GetAngle(asteroid);

                if (anglesWithNeighbors.ContainsKey(angle) == false)
                {
                    anglesWithNeighbors.Add(angle, new SortedList <double, Asteroid>());
                }
                double distance = Math.Pow(sourceAsteroid.X - asteroid.X, 2) + Math.Pow(sourceAsteroid.Y - asteroid.Y, 2);
                anglesWithNeighbors[angle].Add(distance, asteroid);
            }

            return(anglesWithNeighbors);
        }