// Get ready to start.
        private void ResetBoundingRect(Vector2[] vertices)
        {
            _numPoints = vertices.Count();
            // Find the minimum and maximum points
            // in all four directions.
            double minx = vertices[0].X;
            double maxx = minx;
            double miny = vertices[0].Y;
            double maxy = miny;
            double minxi = 0;
            double maxxi = 0;
            double minyi = 0;
            double maxyi = 0;
            for (int i = 1; i < _numPoints; i++)
            {
                if (minx > vertices[i].X)
                {
                    minx = vertices[i].X;
                    minxi = i;
                }
                if (maxx < vertices[i].X)
                {
                    maxx = vertices[i].X;
                    maxxi = i;
                }
                if (miny > vertices[i].Y)
                {
                    miny = vertices[i].Y;
                    minyi = i;
                }
                if (maxy < vertices[i].Y)
                {
                    maxy = vertices[i].Y;
                    maxyi = i;
                }
            }
            _controlPoints[0] = (int)minxi;
            _controlPoints[1] = (int)maxyi;
            _controlPoints[2] = (int)maxxi;
            _controlPoints[3] = (int)minyi;
            _currentControlPoint = -1;
            // Reset the current and best bounding rectangle.
            _currentRectangle = new Vector2[]
            {
            new Vector2(minx, miny),
            new Vector2(maxx, miny),
            new Vector2(maxx, maxy),
            new Vector2(minx, maxy),
            };

            var currentArea = (maxx - minx) * (maxy - miny);
            _bestRectangle = _currentRectangle;
            _bestArea = currentArea;
            // So far we have not checked any edges.
            _edgeChecked = new bool[_numPoints];
            for (int i = 0; i < _numPoints; i++)
            {
                _edgeChecked[i] = false;
            }
        }
예제 #2
0
        public Rectangle2(Vector2[] vertices)
        {
            if (vertices == null) throw new ArgumentNullException("vertices");
            if (vertices.Count() != 4) throw new ArgumentException("You must submit 4 vertices!");

            if (!IsRectangle(vertices))
            {
                throw new ArgumentOutOfRangeException("vertices", "The given vertices must form a rectangle in 2D space!");
            }
            
            _rect = new Polygon2(vertices);
        }
예제 #3
0
        /// <summary>
        /// Checks if the given 4 vertices form a rectangle
        /// </summary>
        /// <param name="vertices"></param>
        /// <returns></returns>
        public static bool IsRectangle(Vector2[] vertices, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            if (vertices == null) throw new ArgumentNullException("vertices");
            if (vertices.Count() != 4) throw new ArgumentException("You must submit 4 vertices!");

            var topLine = new LineSegment2(vertices[0], vertices[1]).ToVector();
            var rightLine = new LineSegment2(vertices[1], vertices[2]).ToVector();
            var bottomLine = new LineSegment2(vertices[2], vertices[3]).ToVector();
            var leftLine = new LineSegment2(vertices[3], vertices[0]).ToVector();

            // Check that two lines have equal length

            if (Math.Abs(topLine.Length - bottomLine.Length) < tolerance
                && Math.Abs(rightLine.Length - leftLine.Length) < tolerance)
            {
                // Now ensure that the lines are orthogonal on each other
                bool isRect = topLine.IsPerpendicularTo(rightLine, tolerance);
                isRect &= rightLine.IsPerpendicularTo(bottomLine, tolerance);
                isRect &= bottomLine.IsPerpendicularTo(leftLine, tolerance);

                return isRect;
            }

            return false;
        }