/// <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> InterceptRect(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
 {
     var intersections = new List<Vector2>();
     var borderLines = LineSegment2.FromRectangle(rect); //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 InterceptRectWith(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
 {
     
     var borderLines = LineSegment2.FromRectangle(rect); //get 4 borderlines from rect
     if (borderLines != null) {
         return InterceptLinesWith(borderLines, tolerance);
     }
     return false;
 }
Example #3
0
        public void InterceptRectNo()
        {
            var line1 = LineSegment2.Parse("(10, 5000)(2000, 5000)");
            var rect  = new AARectangle(new Vector2(30, 30), new SizeD(50, 40));

            var actual = line1.RectIntersection(rect);

            Assert.AreEqual(false, line1.HasRectIntersection(rect));
            Assert.AreEqual(0, actual.Count);
        }
        /// <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 InterceptRectWith(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var borderLines = LineSegment2.FromRectangle(rect); //get 4 borderlines from rect

            if (borderLines != null)
            {
                return(InterceptLinesWith(borderLines, tolerance));
            }
            return(false);
        }
        /// <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> InterceptRect(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List <Vector2>();
            var borderLines   = LineSegment2.FromRectangle(rect); //get 4 borderlines from rect

            if (borderLines != null)
            {
                intersections.AddRange(InterceptLines(borderLines, tolerance));
            }
            return(intersections);
        }
        // Can be removed if Polygon2 supports Line intersections
        #region Line - AARectangle Intersection

        /// <summary>
        /// Returns true if there is at least one intersection point.
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public bool HasRectIntersection(AARectangle rect1, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            // is Start/EndPoint in the Rectangle?
            if (rect1.Contains(this.Start) || rect1.Contains(this.End)) // TODO handle collision
            {
                return(true);
            }
            // crosses the Line a Rectangle Border?
            var borderLines = FromRectangle(rect1); //get 4 borderlines from rect

            // check if any of the borderlines intercept with this line
            return(borderLines.Any(border => IntersectLine(border, tolerance).HasValue));
        }
        /// <summary>
        /// Explodes a Rectangle into the 4 border lines and returns an array of lines
        /// </summary>
        /// <param name="rect"></param>
        /// <returns>4 lines, one for each side of the rectangle</returns>
        public static LineSegment2[] FromRectangle(AARectangle rect)
        {
            var topLeft     = rect.Location;
            var topRight    = new Vector2(rect.X + rect.Width, rect.Y);
            var bottomRight = new Vector2(rect.X + rect.Width, rect.Y + rect.Height);
            var bottomLeft  = new Vector2(rect.X, rect.Y + rect.Height);

            return(new[]
            {
                new LineSegment2(topLeft, topRight),
                new LineSegment2(topRight, bottomRight),
                new LineSegment2(bottomRight, bottomLeft),
                new LineSegment2(bottomLeft, topLeft)
            });
        }
Example #8
0
        [TestCase("(10,10) (90,90)", "(30,30)", " (50,40)", "(30,30),(70,70)")]  // Diagonal Line with two intersections
        // [TestCase("(25, 567.521681),(355.95663, 567.521681)", "(167.97, 555.5)", "(45, 24)", "(11,11)")] // Case from real data

        public void InterceptRect(string line1Str, string rectLocationStr, string rectSizeStr, string expectedIntersections)
        {
            var line1 = LineSegment2.Parse(line1Str);
            var rect  = new AARectangle(Vector2.Parse(rectLocationStr), SizeD.Parse(rectSizeStr));

            var expectedInters = Vector2.ParseAll(expectedIntersections);

            var actual = line1.RectIntersection(rect);

            Assert.AreEqual(expectedInters.Length, actual.Count); // Expected number of intersections
            // Check each point
            if (expectedInters.Length == actual.Count)
            {
                foreach (var p in actual)
                {
                    Assert.True(expectedInters.Contains(p));
                }
            }
        }
        /// <summary>
        /// Returns every intersection Point of the Rect::Line if any. Max returned Points are 2.
        /// This Method actaully can't handle full contained Lines in the Rect - use the InterceptRectWith to determite if there is collision.
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="tolerance"></param>
        /// <returns>Returns count of Interception Points</returns>
        public List <Vector2> RectIntersection(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var   intersections = new List <Vector2>(2);
            short i             = 0;
            var   borderLines   = FromRectangle(rect); //get 4 borderlines from rect

            foreach (var border in borderLines)
            {
                var pnt = this.IntersectLine(border, tolerance);

                if (pnt.HasValue)
                {
                    // found intersection point
                    intersections.Add(pnt.Value);
                    i++;
                    if (i == 2)
                    {
                        break;
                    }
                }
            }
            return(intersections);
        }
        [TestCase("(10,10) (90,90)", "(30,30)", " (50,40)", "(30,30),(70,70)")] // Diagonal Line with two intersections
       // [TestCase("(25, 567.521681),(355.95663, 567.521681)", "(167.97, 555.5)", "(45, 24)", "(11,11)")] // Case from real data

        public void InterceptRect(string line1Str, string rectLocationStr, string rectSizeStr, string expectedIntersections)
        {
            var line1 = LineSegment2.Parse(line1Str);
            var rect = new AARectangle(Vector2.Parse(rectLocationStr), SizeD.Parse(rectSizeStr));

            var expectedInters = Vector2.ParseAll(expectedIntersections);

            var actual = line1.RectIntersection(rect);

            Assert.AreEqual(expectedInters.Length, actual.Count); // Expected number of intersections
            // Check each point
            if (expectedInters.Length == actual.Count)
            {
                foreach (var p in actual)
                {
                    Assert.True(expectedInters.Contains(p));
                }
            }
        }
        public void InterceptRectNo()
        {
            var line1 = LineSegment2.Parse("(10, 5000)(2000, 5000)");
            var rect = new AARectangle(new Vector2(30,30), new SizeD(50,40));

            var actual = line1.RectIntersection(rect);

            Assert.AreEqual(false, line1.HasRectIntersection(rect));
            Assert.AreEqual(0, actual.Count);
        }
        /// <summary>
        /// Returns every intersection Point of the Rect::Line if any. Max returned Points are 2.
        /// This Method actaully can't handle full contained Lines in the Rect - use the InterceptRectWith to determite if there is collision.
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="tolerance"></param>
        /// <returns>Returns count of Interception Points</returns>
        public List<Vector2> RectIntersection(AARectangle rect, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            var intersections = new List<Vector2>(2);
            short i = 0;
            var borderLines = FromRectangle(rect); //get 4 borderlines from rect

            foreach (var border in borderLines) {

                var pnt = this.IntersectLine(border, tolerance);

                if (pnt.HasValue)
                {
                    // found intersection point
                    intersections.Add(pnt.Value);
                    i++;
                    if (i == 2) break;  
                }
            }
            return intersections;
        }
 public Rectangle2(AARectangle rect, Angle? rotation = null)
     : this(rect.Location, rect.Size, rotation ?? Angle.Zero) { }
 public RectangleAA2(AARectangle prototype)
     : this(prototype.Location, prototype.Size)
 {
 }
        // Can be removed if Polygon2 supports Line intersections
        #region Line - AARectangle Intersection

        /// <summary>
        /// Returns true if there is at least one intersection point.
        /// </summary>
        /// <param name="rect1"></param>
        /// <param name="tolerance"></param>
        /// <returns></returns>
        public bool HasRectIntersection(AARectangle rect1, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            
            // is Start/EndPoint in the Rectangle?
            if (rect1.Contains(this.Start) || rect1.Contains(this.End)) // TODO handle collision
                return true; 
            // crosses the Line a Rectangle Border?
            var borderLines = FromRectangle(rect1); //get 4 borderlines from rect

            // check if any of the borderlines intercept with this line
            return borderLines.Any(border => IntersectLine(border, tolerance).HasValue);
        }
 public RectangleAA2(AARectangle prototype)
     : this(prototype.Location, prototype.Size)
 {
 }
 public Rectangle2(AARectangle rect, Angle?rotation = null)
     : this(rect.Location, rect.Size, rotation ?? Angle.Zero)
 {
 }
 public static RectangleF ToRectangleF(AARectangle aarect)
 {
     return new RectangleF((float)aarect.X, (float)aarect.Y, (float)aarect.Width, (float)aarect.Height);
 }
Example #19
0
 public static RectangleF ToRectangleF(AARectangle aarect)
 {
     return(new RectangleF((float)aarect.X, (float)aarect.Y, (float)aarect.Width, (float)aarect.Height));
 }