/// <summary>
        /// Find a smallest bounding rectangle.
        /// </summary>
        /// <returns></returns>
        public Vector2[] FindBounds(Polygon2 polygon){

            if (polygon == null) throw new ArgumentNullException("polygon");

            var minified = new Polygon2(polygon.ToVertices().Distinct());
            minified.OrientCounterClockwise();
            var vertices = minified.ToVertices().ToArray();

            if (vertices.Count() == 0)
            {
                return new Vector2[0];
            }

            // This algorithm assumes the polygon is oriented counter-clockwise.

            // Get ready;
            ResetBoundingRect(vertices);

            // Check all possible bounding rectangles.
            for (int i = 0; i < vertices.Count(); i++)
            {
                CheckNextBoundingRectangle(vertices);
            }

            // Return the best result.
            return _bestRectangle;
        }
 /// <summary>
 /// Checks if a Rectangle collides with the Arc.
 /// </summary>
 /// <param name="rect">Rectangle to check</param>
 /// <returns>On collision, true is returned, false otherwise</returns>
 private bool InterceptWithPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
 {
     var borderLines = polygon.ToLines(); //get 4 borderlines from rect
     if (borderLines != null) {
         return InterceptLinesWith(borderLines, tolerance);
     }
     return false;
 }
 public static VisualPolygon Create(Polygon2 poly, Pen pen = null, Brush brush = null)
 {
     return new VisualPolygon(poly)
     {
         Pen = pen,
         FillBrush = brush
     };
 }
 /// <summary>Checks if a Rectangle collides with the Arc.
 /// 
 /// </summary>
 /// <param name="rect">Rectangle to check</param>
 /// <returns>On collision, a List of interception Points is returned</returns>
 private List<Vector2> InterceptPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
 {
     var intersections = new List<Vector2>();
     var borderLines = polygon.ToLines(); //get 4 borderlines from rect
     if (borderLines != null) {
         intersections.AddRange(InterceptLines(borderLines, tolerance));
     }
     return intersections;
 }
        /// <summary>
        /// Checks if a Rectangle collides with the Arc.
        /// </summary>
        /// <param name="rect">Rectangle to check</param>
        /// <returns>On collision, true is returned, false otherwise</returns>
        private bool InterceptWithPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var borderLines = polygon.ToLines(); //get 4 borderlines from rect

            if (borderLines != null)
            {
                return(InterceptLinesWith(borderLines, tolerance));
            }
            return(false);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Finds all intersection points with the given polygon
        /// </summary>
        /// <param name="polygon"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        private List <Vector2> InterceptPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List <Vector2>();

            foreach (var border in polygon.ToLines())
            {
                intersections.AddRange(this.InterceptLine(border, tolerance));
            }
            return(intersections);
        }
        /// <summary>Checks if a Rectangle collides with the Arc.
        ///
        /// </summary>
        /// <param name="rect">Rectangle to check</param>
        /// <returns>On collision, a List of interception Points is returned</returns>
        private List <Vector2> InterceptPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List <Vector2>();
            var borderLines   = polygon.ToLines(); //get 4 borderlines from rect

            if (borderLines != null)
            {
                intersections.AddRange(InterceptLines(borderLines, tolerance));
            }
            return(intersections);
        }
        /// <summary>
        /// Polygon - Polygon intersection
        /// </summary>
        /// <param name="other"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        private IEnumerable <Vector2> InterceptPolygon(Polygon2 otherPolygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List <Vector2>();
            var thisLines     = ToLines();
            var otherLines    = otherPolygon.ToLines();

            foreach (var line in thisLines)
            {
                foreach (var other in otherLines)
                {
                    intersections.AddRange(line.Intersect(other));
                }
            }
            return(intersections);
        }
        /// <summary>
        /// Is the given Polygon fully contained in this one?
        /// </summary>
        /// <param name="polygon"> </param>
        /// <param name="tolerance"> </param>
        /// <returns></returns>
        public bool Contains(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            if (polygon.ToVertices().All(x => Contains(x, tolerance)))
            {
                // When all vertices are contained in a convex polygon
                // we already know that the given vertices are inside
                // this polygon thus we are done
                if (IsConvex()) return true;

                // Otherwise, this is a concave polygon
                // we need to ensure, that the vertices do not intersect any line
                var otherLines = polygon.ToLines();
                return !HasIntersection(otherLines, tolerance);
            }

            return false;
        }
Ejemplo n.º 10
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);
        }
        /// <summary>
        /// Is the given Polygon fully contained in this one?
        /// </summary>
        /// <param name="polygon"> </param>
        /// <param name="tolerance"> </param>
        /// <returns></returns>
        public bool Contains(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            if (polygon.ToVertices().All(x => Contains(x, tolerance)))
            {
                // When all vertices are contained in a convex polygon
                // we already know that the given vertices are inside
                // this polygon thus we are done
                if (IsConvex())
                {
                    return(true);
                }

                // Otherwise, this is a concave polygon
                // we need to ensure, that the vertices do not intersect any line
                var otherLines = polygon.ToLines();
                return(!HasIntersection(otherLines, tolerance));
            }

            return(false);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 public Polygon2(Polygon2 prototype) {
     this.Prototype(prototype);
 }
Ejemplo n.º 14
0
 public Polygon2(Polygon2 prototype)
 {
     this.Prototype(prototype);
 }
        /// <summary>
        /// Polygon - Polygon intersection
        /// </summary>
        /// <param name="other"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        private IEnumerable<Vector2> InterceptPolygon(Polygon2 otherPolygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List<Vector2>();
            var thisLines = ToLines();
            var otherLines = otherPolygon.ToLines();

            foreach (var line in thisLines)
            {
                foreach (var other in otherLines)
                {
                    intersections.AddRange(line.Intersect(other));
                }
            }
            return intersections; 
        }
 /// <summary>
 /// Finds all intersection points with the given polygon
 /// </summary>
 /// <param name="polygon"></param>
 /// <param name="tolerance"></param>
 /// <returns></returns>
 private List<Vector2> InterceptPolygon(Polygon2 polygon, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
 {
     var intersections = new List<Vector2>();
     foreach (var border in polygon.ToLines())
     {
         intersections.AddRange(this.InterceptLine(border, tolerance));
     }
     return intersections;
 }