public void AssertThat_GetCorners_Throws_WhenPresentedAnArrayWhichIsTooSmall() { var rect = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); Vector2[] arr = new Vector2[2]; rect.GetCorners(arr); }
public void AssertThat_Inflate_ByNegativeValue_DecreasesSize() { BoundingRectangle s = new BoundingRectangle(new Vector2(0), new Vector2(10)); Assert.AreEqual(new Vector2(3), s.Inflate(-6).Min); Assert.AreEqual(new Vector2(7), s.Inflate(-6).Max); }
public void AssertThat_Inflate_IncreasesSize() { BoundingRectangle s = new BoundingRectangle(new Vector2(0), new Vector2(10)); Assert.AreEqual(new Vector2(-5), s.Inflate(10).Min); Assert.AreEqual(new Vector2(15), s.Inflate(10).Max); }
public void AssertThat_Intersection_ReturnsNothing_WhenNotOverlappingWithGapBottomLeft() { var a = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); var b = new BoundingRectangle(new Vector2(-5, -5), new Vector2(-15, -15)); // ReSharper disable once PossibleInvalidOperationException Assert.IsFalse(a.Intersection(b).HasValue); }
public void AssertThat_Intersection_ReturnsIntersectingArea() { var a = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); var b = new BoundingRectangle(new Vector2(5, 5), new Vector2(15, 15)); // ReSharper disable once PossibleInvalidOperationException var m = a.Intersection(b).Value; Assert.AreEqual(new Vector2(5, 5), m.Min); Assert.AreEqual(new Vector2(10, 10), m.Max); }
public void AssertThat_GetCorners_GetsCorners() { var rect = new BoundingRectangle(new Vector2(0, 0), new Vector2(10, 10)); var corners = rect.GetCorners(); Assert.AreEqual(4, corners.Length); Assert.IsTrue(corners.Contains(new Vector2(0, 0))); Assert.IsTrue(corners.Contains(new Vector2(0, 10))); Assert.IsTrue(corners.Contains(new Vector2(10, 0))); Assert.IsTrue(corners.Contains(new Vector2(10, 10))); }
/// <summary> /// Creates the smallest BoundingRectangle that contains the two specified BoundingRectangle instances. /// </summary> /// <param name="original">One of the BoundingRectangles to contain.</param><param name="additional">One of the BoundingRectangles to contain.</param> public static BoundingRectangle CreateMerged(BoundingRectangle original, BoundingRectangle additional) { BoundingRectangle result; CreateMerged(ref original, ref additional, out result); return result; }
public IEnumerable<BoundingRectangle> Overlaps(BoundingRectangle query) { //todo: this could be optimised to some kind of interval tree to more efficiently find overlaps return _screenSpaceBounds.Where(bound => bound.Intersects(query)); }
public bool Intersects(BoundingRectangle box) { bool result; Intersects(ref box, out result); return result; }
/// <summary> /// Checks whether the current BoundingRectangle intersects another BoundingRectangle. /// </summary> /// <param name="box">The BoundingRectangle to check for intersection with.</param><param name="result">[OutAttribute] true if the BoundingRectangle instances intersect; false otherwise.</param> public void Intersects(ref BoundingRectangle box, out bool result) { result = box.Min.X < Max.X && box.Max.X > Min.X && box.Min.Y < Max.Y && box.Max.Y > Min.Y; }
/// <summary> /// Gets whether or not the provided <see cref="BoundingRectangle"/> lies within the bounds of this <see cref="BoundingRectangle"/>. /// </summary> /// <param name="value">The <see cref="BoundingRectangle"/> to check for inclusion in this <see cref="BoundingRectangle"/>.</param> /// <param name="result"><c>true</c> if the provided <see cref="BoundingRectangle"/>'s bounds lie entirely inside this <see cref="BoundingRectangle"/>; <c>false</c> otherwise. As an output parameter.</param> public void Contains(ref BoundingRectangle value, out bool result) { result = Vector2.Min(value.Min, Min) == Min && Vector2.Max(value.Max, Max) == Max; }
public BoundingRectangle? Intersection(ref BoundingRectangle box) { var min = Vector2.Max(Min, box.Min); var max = Vector2.Min(Max, box.Max); //Check if the min of the overlap is not the min, this means the overlap is inverted and not actually an overlap if (Vector2.Min(min, max) != min) return null; return new BoundingRectangle(min, max); }
/// <summary> /// Checks whether the current BoundingRectangle intersects another BoundingRectangle. /// </summary> /// <param name="box">The BoundingRectangle to check for intersection with.</param><param name="result">[OutAttribute] true if the BoundingRectangle instances intersect; false otherwise.</param> public void Intersects(ref BoundingRectangle box, out bool result) { result = box.Min.X <Max.X && box.Max.X> Min.X && box.Min.Y <Max.Y && box.Max.Y> Min.Y; }
/// <summary> /// Creates the smallest BoundingRectangle that contains the two specified BoundingRectangle instances. /// </summary> /// <param name="original">One of the BoundingRectangle instances to contain.</param><param name="additional">One of the BoundingRectangle instances to contain.</param><param name="result">[OutAttribute] The created BoundingRectangle.</param> public static void CreateMerged(ref BoundingRectangle original, ref BoundingRectangle additional, out BoundingRectangle result) { result = new BoundingRectangle( Vector2.Min(original.Min, additional.Min), Vector2.Max(original.Max, additional.Max) ); }
/// <summary> /// Expand bounding rectangle by distance / 2 at min and max (total of distance) /// </summary> /// <param name="rectangle">The rectangle to mutate</param> /// <param name="distance"></param> /// <returns></returns> public static void Inflate(ref BoundingRectangle rectangle, float distance) { var min = rectangle.Min - new Vector2(distance / 2); var max = rectangle.Max + new Vector2(distance / 2); if (distance < 0) { if (Vector2.Min(min, max) != min) throw new ArgumentOutOfRangeException("distance", "Distance specified to inflate rectangle is a negative value larger than the total diagonal size of the rectangle (doing this would invert the rectangle!"); } rectangle.Min = min; rectangle.Max = max; }
public bool Contains(BoundingRectangle value) { bool result; Contains(ref value, out result); return result; }
/// <summary> /// Creates the smallest BoundingRectangle that contains the two specified BoundingRectangle instances. /// </summary> /// <param name="original">One of the BoundingRectangle instances to contain.</param><param name="additional">One of the BoundingRectangle instances to contain.</param><param name="result">[OutAttribute] The created BoundingRectangle.</param> public static void CreateMerged(ref BoundingRectangle original, ref BoundingRectangle additional, out BoundingRectangle result) { result = new BoundingRectangle( Vector2.Min(original.Min, additional.Min), Vector2.Max(original.Max, additional.Max) ); }
/// <summary> /// Checks whether the current Ray intersects a BoundingRectangle. /// </summary> /// <param name="box">The BoundingRectangle to check for intersection with.</param><param name="result">[OutAttribute] Distance at which the ray intersects the BoundingBox or null if there is no intersection.</param> public void Intersects(ref BoundingRectangle box, out float? result) { const float EPSILON = 1e-6f; float? tMin = null, tMax = null; if (Math.Abs(Direction.X) < EPSILON) { if (Position.X < box.Min.X || Position.X > box.Max.X) { result = null; return; } } else { tMin = (box.Min.X - Position.X) / Direction.X; tMax = (box.Max.X - Position.X) / Direction.X; if (tMin > tMax) { var temp = tMin; tMin = tMax; tMax = temp; } } if (Math.Abs(Direction.Y) < EPSILON) { if (Position.Y < box.Min.Y || Position.Y > box.Max.Y) { result = null; return; } } else { var tMinY = (box.Min.Y - Position.Y) / Direction.Y; var tMaxY = (box.Max.Y - Position.Y) / Direction.Y; if (tMinY > tMaxY) { var temp = tMinY; tMinY = tMaxY; tMaxY = temp; } if ((tMin.HasValue && tMin > tMaxY) || (tMax.HasValue && tMinY > tMax)) { result = null; return; } if (!tMin.HasValue || tMinY > tMin) tMin = tMinY; if (!tMax.HasValue || tMaxY < tMax) tMax = tMaxY; } // having a positive tMin and a negative tMax means the ray is inside the box // we expect the intesection distance to be 0 in that case if ((tMin.HasValue && tMin < 0) && tMax > 0) { result = 0; return; } // a negative tMin means that the intersection point is behind the ray's origin // we discard these as not hitting the AABB if (tMin < 0) { result = null; return; } result = tMin; }
/// <summary> /// Checks whether the Ray intersects a specified BoundingRectangle. /// </summary> /// <param name="box">The BoundingRectangle to check for intersection with the Ray.</param> public float? Intersects(BoundingRectangle box) { float? result; Intersects(ref box, out result); return result; }
private static float EstimateError(Layer batch, IGeometry geometry, BoundingRectangle screenSpaceBounds) { float totalError = 0; foreach (var bound in batch.Overlaps(screenSpaceBounds)) { var overlap = bound.Intersection(screenSpaceBounds); if (!overlap.HasValue) continue; var size = overlap.Value.Max - overlap.Value.Min; totalError += size.X * size.Y; } return totalError; }
/// <summary> /// Checks whether the current BoundingRectangle intersects another BoundingRectangle. /// </summary> /// <param name="box">The BoundingBox to check for intersection with.</param> [Pure] public bool Intersects(BoundingRectangle box) { Intersects(ref box, out bool result); return(result); }
/// <summary> /// Calculate the intersection of this rectangle and another /// </summary> /// <param name="box"></param> /// <returns></returns> [Pure] public BoundingRectangle?Intersection(BoundingRectangle box) { return(Intersection(ref box)); }
/// <summary> /// Creates the smallest BoundingRectangle that contains the two specified BoundingRectangle instances. /// </summary> /// <param name="original">One of the BoundingRectangles to contain.</param><param name="additional">One of the BoundingRectangles to contain.</param> public static BoundingRectangle CreateMerged(BoundingRectangle original, BoundingRectangle additional) { CreateMerged(ref original, ref additional, out BoundingRectangle result); return(result); }
public void AssertThat_InflateSphere_ByNegativeValueLargerThanSize_Throws() { BoundingRectangle s = new BoundingRectangle(Vector2.Zero, new Vector2(10)); var result = s.Inflate(-50); }
public BoundingRectangle? Intersection(BoundingRectangle box) { return Intersection(ref box); }
/// <summary> /// Gets whether or not the provided <see cref="BoundingRectangle"/> lies within the bounds of this <see cref="BoundingRectangle"/>. /// </summary> /// <param name="value">The <see cref="BoundingRectangle"/> to check for inclusion in this <see cref="BoundingRectangle"/>.</param> /// <param name="result"><c>true</c> if the provided <see cref="BoundingRectangle"/>'s bounds lie entirely inside this <see cref="BoundingRectangle"/>; <c>false</c> otherwise. As an output parameter.</param> public void Contains(ref BoundingRectangle value, out bool result) { result = Vector2.Min(value.Min, Min) == Min && Vector2.Max(value.Max, Max) == Max; }
/// <summary> /// Gets whether or not the provided <see cref="BoundingRectangle"/> lies within the bounds of this <see cref="BoundingRectangle"/>. /// </summary> /// <param name="value">The <see cref="BoundingRectangle"/> to check for inclusion in this <see cref="BoundingRectangle"/>.</param> /// <returns><c>true</c> if the provided <see cref="BoundingRectangle"/>'s bounds lie entirely inside this <see cref="BoundingRectangle"/>; <c>false</c> otherwise.</returns> [Pure] public bool Contains(BoundingRectangle value) { Contains(ref value, out bool result); return(result); }
public bool Equals(BoundingRectangle other) { return Min.Equals(other.Min) && Max.Equals(other.Max); }
/// <summary> /// Determines whether two instances of BoundingBox are equal. /// </summary> /// <param name="other">The BoundingBox to compare with the current BoundingBox.</param> [Pure] public bool Equals(BoundingRectangle other) { return(Min.Equals(other.Min) && Max.Equals(other.Max)); }