/// <summary> /// Internal helper function for computing intersections. /// </summary> private static bool IntersectVertical(ref RCIntRectangle lRect, ref RCIntRectangle rRect, out int intersectTop, out int intersectBottom) { if (lRect.Top <= rRect.Top && rRect.Top < lRect.Bottom && lRect.Bottom <= rRect.Bottom) { intersectTop = rRect.Top; intersectBottom = lRect.Bottom; return(true); } else if (rRect.Top <= lRect.Top && lRect.Top < lRect.Bottom && lRect.Bottom <= rRect.Bottom) { intersectTop = lRect.Top; intersectBottom = lRect.Bottom; return(true); } else if (rRect.Top <= lRect.Top && lRect.Top < rRect.Bottom && rRect.Bottom <= lRect.Bottom) { intersectTop = lRect.Top; intersectBottom = rRect.Bottom; return(true); } else { intersectTop = 0; intersectBottom = 0; return(false); } }
/// <summary> /// Returns a third RCIntRectangle that represents the intersection of two other RCRectangles. /// If there is no intersection, RCIntRectangle.Undefined is returned. /// </summary> /// <param name="lRect">An RCIntRectangle to intersect.</param> /// <param name="rRect">An RCIntRectangle to intersect.</param> /// <returns> /// An RCIntRectangle that is the intersection of lRect and rRect or RCIntRectangle.Undefined if there is no intersection. /// </returns> public static RCIntRectangle Intersect(RCIntRectangle lRect, RCIntRectangle rRect) { if (!lRect.isDefined) { throw new ArgumentNullException("lRect"); } if (!rRect.isDefined) { throw new ArgumentNullException("rRect"); } int intersectLeft, intersectRight, intersectTop, intersectBottom; /// Compute the horizontal coordinates of the intersection if (!IntersectHorizontal(ref lRect, ref rRect, out intersectLeft, out intersectRight) && !IntersectHorizontal(ref rRect, ref lRect, out intersectLeft, out intersectRight)) { return(RCIntRectangle.Undefined); } /// Compute the vertical coordinates of the intersection if (!IntersectVertical(ref lRect, ref rRect, out intersectTop, out intersectBottom) && !IntersectVertical(ref rRect, ref lRect, out intersectTop, out intersectBottom)) { return(RCIntRectangle.Undefined); } return(new RCIntRectangle(intersectLeft, intersectTop, intersectRight - intersectLeft, intersectBottom - intersectTop)); }
/// <summary> /// Internal helper function for computing intersections. /// </summary> private static bool IntersectHorizontal(ref RCIntRectangle lRect, ref RCIntRectangle rRect, out int intersectLeft, out int intersectRight) { if ((lRect.Left <= rRect.Left && rRect.Left < lRect.Right && lRect.Right <= rRect.Right)) { intersectLeft = rRect.Left; intersectRight = lRect.Right; return(true); } else if (rRect.Left <= lRect.Left && lRect.Left < lRect.Right && lRect.Right <= rRect.Right) { intersectLeft = lRect.Left; intersectRight = lRect.Right; return(true); } else if (rRect.Left <= lRect.Left && lRect.Left < rRect.Right && rRect.Right <= lRect.Right) { intersectLeft = lRect.Left; intersectRight = rRect.Right; return(true); } else { intersectLeft = 0; intersectRight = 0; return(false); } }
/// <summary> /// Replaces this RCIntRectangle with the intersection of itself and the specified Rectangle. /// </summary> /// <param name="other">The other RCIntRectangle to intersect with.</param> /// <remarks> /// If this and other RCRectangles don't intersect each other, then this RCIntRectangle becomes RCIntRectangle.Undefined. /// </remarks> public void Intersect(RCIntRectangle other) { if (!this.isDefined) { throw new InvalidOperationException("Illegal use of undefined RCIntRectangle!"); } if (!other.isDefined) { throw new ArgumentNullException("other"); } this = RCIntRectangle.Intersect(this, other); }
/// <summary> /// Determines if this RCIntRectangle intersects with rect. /// </summary> /// <param name="rect">The RCIntRectangle to test.</param> /// <returns>True if there is any intersection, false otherwise.</returns> public bool IntersectsWith(RCIntRectangle rect) { if (!this.isDefined) { throw new InvalidOperationException("Illegal use of undefined RCIntRectangle!"); } if (!rect.isDefined) { throw new ArgumentNullException("rect"); } return((this.Left < rect.Right) && (this.Right > rect.Left) && (this.Top < rect.Bottom) && (this.Bottom > rect.Top)); }
/// <summary> /// Initializes a new RCIntRectangle with the specified RCIntRectangle. /// </summary> /// <param name="other">The RCIntRectangle to initialize with.</param> public RCIntRectangle(RCIntRectangle other) { if (!other.isDefined) { throw new ArgumentNullException("other"); } this.isDefined = true; this.x = other.x; this.y = other.y; this.width = other.width; this.height = other.height; }
/// <summary> /// Determines if the rectangular region represented by rect is entirely contained within this RCIntRectangle. /// </summary> /// <param name="rect">The RCIntRectangle to test.</param> /// <returns>True if the rectangular region represented by rect is entirely contained within this RCIntRectangle, false otherwise.</returns> public bool Contains(RCIntRectangle rect) { if (!this.isDefined) { throw new InvalidOperationException("Illegal use of undefined RCIntRectangle!"); } if (!rect.isDefined) { throw new ArgumentNullException("rect"); } return(this.Contains(rect.x, rect.y) && this.Contains(rect.x + rect.width - 1, rect.y + rect.height - 1)); }
/// <summary> /// Checks whether this RCIntRectangle has the same location and size as the specified RCIntRectangle. /// </summary> /// <param name="other">The RCIntRectangle to test.</param> /// <returns>True if other RCIntRectangle has same location and size as this RCIntRectangle.</returns> public bool Equals(RCIntRectangle other) { return((!this.isDefined && !other.isDefined) || (this.isDefined && other.isDefined && this.x == other.x && this.y == other.y && this.width == other.width && this.height == other.height)); }