예제 #1
0
        /// <summary>
        /// Cleans up a simple polygon by removing duplicate adjacent positions and making
        /// the first position not equal the last position
        /// </summary>
        public static IList <T> Cleanup <T>(IEnumerable <T> positions)
        {
            IList <T> positionsList = CollectionAlgorithms.EnumerableToList(positions);

            if (positionsList.Count < 3)
            {
                throw new ArgumentOutOfRangeException("positions", "At least three positions are required.");
            }

            List <T> cleanedPositions = new List <T>(positionsList.Count);

            for (int i0 = positionsList.Count - 1, i1 = 0; i1 < positionsList.Count; i0 = i1++)
            {
                T v0 = positionsList[i0];
                T v1 = positionsList[i1];

                if (!v0.Equals(v1))
                {
                    cleanedPositions.Add(v1);
                }
            }

            cleanedPositions.TrimExcess();
            return(cleanedPositions);
        }
예제 #2
0
        public void EnumerableToList2()
        {
            Dictionary <int, int> dictionary = new Dictionary <int, int>();

            dictionary.Add(0, 0);
            dictionary.Add(1, 1);

            IList <KeyValuePair <int, int> > returnedList = CollectionAlgorithms.EnumerableToList(dictionary);

            Assert.AreEqual(2, returnedList.Count);
            Assert.AreEqual(new KeyValuePair <int, int>(0, 0), returnedList[0]);
            Assert.AreEqual(new KeyValuePair <int, int>(1, 1), returnedList[1]);
        }
예제 #3
0
        public void EnumerableToList()
        {
            IList <int> list = new List <int>();

            list.Add(0);
            list.Add(1);

            IList <int> returnedList = CollectionAlgorithms.EnumerableToList(list);

            Assert.AreEqual(2, returnedList.Count);
            Assert.AreEqual(0, returnedList[0]);
            Assert.AreEqual(1, returnedList[1]);
        }
예제 #4
0
        public static double ComputeArea(IEnumerable <Vector2D> positions)
        {
            IList <Vector2D> positionsList = CollectionAlgorithms.EnumerableToList(positions);

            if (positionsList.Count < 3)
            {
                throw new ArgumentOutOfRangeException("positions", "At least three positions are required.");
            }

            double area = 0.0;

            //
            // Compute the area of the polygon.  The sign of the area determines the winding order.
            //
            for (int i0 = positionsList.Count - 1, i1 = 0; i1 < positionsList.Count; i0 = i1++)
            {
                Vector2D v0 = positionsList[i0];
                Vector2D v1 = positionsList[i1];

                area += (v0.X * v1.Y) - (v1.X * v0.Y);
            }

            return(area * 0.5);
        }
예제 #5
0
 public void EnumerableToListNull()
 {
     CollectionAlgorithms.EnumerableToList <int>(null);
 }