public Rectd Intersect(Recti rect) { double x1 = X + Width; double x2 = rect.X + rect.Width; double y1 = Y + Height; double y2 = rect.Y + rect.Height; double xL = Math.Max(X, rect.X); double xR = Math.Min(x1, x2); if (xR <= xL) { return(null); } else { double yB = Math.Min(y1, y2); double yT = Math.Max(Y, rect.Y); if (yB <= yT) { return(null); } else { return(new Rectd(xL, yT, xR - xL, yB - yT)); } } }
/// <summary> /// Returns the Rectangle that is the intersection of this rectangle with another. Returns null if there is no intersection /// </summary> /// <param name="rect"></param> /// <returns></returns> public Recti Intersect(Recti rect) { int x1 = X + Width; int x2 = rect.X + rect.Width; int y1 = Y + Height; int y2 = rect.Y + rect.Height; int xL = Math.Max(X, rect.X); int xR = Math.Min(x1, x2); if (xR <= xL) { return(null); } else { int yT = Math.Max(Y, rect.Y); int yB = Math.Min(y1, y2); if (yB <= yT) { return(null); } else { return(new Recti(xL, yT, xR - xL, yB - yT)); } } }