// // Summary: // // Determines whether or not this <code>JRectangle</code> and the specified // <code>JRectangle</code> intersect. Two rectangles intersect if // their intersection is nonempty. // // @param r the specified <code>JRectangle</code> // @return <code>true</code> if the specified <code>JRectangle</code> // and this <code>JRectangle</code> intersect; // <code>false</code> otherwise. // public bool Intersects(JRectangle r) { int tw = this.Width; int th = this.Height; int rw = r.Width; int rh = r.Height; if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) { return(false); } int tx = this.X; int ty = this.Y; int rx = r.X; int ry = r.Y; rw += rx; rh += ry; tw += tx; th += ty; // overflow || intersect return((rw < rx || rw > tx) && (rh < ry || rh > ty) && (tw < tx || tw > rx) && (th < ty || th > ry)); }
// // Summary: // // Adds a <code>JRectangle</code> to this <code>JRectangle</code>. // The resulting <code>JRectangle</code> is the union of the two // rectangles. // <p> // If either {@code JRectangle} has any Size less than 0, the // result will have the Sizes of the other {@code JRectangle}. // If both {@code JRectangle}s have at least one Size less // than 0, the result will have at least one Size less than 0. // <p> // If either {@code JRectangle} has one or both Sizes equal // to 0, the result along those axes with 0 Sizes will be // equivalent to the results obtained by adding the corresponding // origin coordinate to the result JRectangle along that axis, // similar to the operation of the {@link #add(Point)} method, // but contribute no further Size beyond that. // <p> // If the resulting {@code JRectangle} would have a Size // too large to be expressed as an {@code int}, the result // will have a Size of {@code int.MaxValue} along // that Size. // @param r the specified <code>JRectangle</code> // public void Add(JRectangle r) { long tx2 = this.Width; long ty2 = this.Height; if ((tx2 | ty2) < 0) { SetBounds(r.X, r.Y, r.Width, r.Height); } long rx2 = r.Width; long ry2 = r.Height; if ((rx2 | ry2) < 0) { return; } int tx1 = this.X; int ty1 = this.Y; tx2 += tx1; ty2 += ty1; int rx1 = r.X; int ry1 = r.Y; rx2 += rx1; ry2 += ry1; if (tx1 > rx1) { tx1 = rx1; } if (ty1 > ry1) { ty1 = ry1; } if (tx2 < rx2) { tx2 = rx2; } if (ty2 < ry2) { ty2 = ry2; } tx2 -= tx1; ty2 -= ty1; // tx2,ty2 will never underflow since both original // rectangles were non-empty // they might overflow, though... if (tx2 > int.MaxValue) { tx2 = int.MaxValue; } if (ty2 > int.MaxValue) { ty2 = int.MaxValue; } SetBounds(tx1, ty1, (int)tx2, (int)ty2); }
// // Summary: // // Computes the intersection of this <code>JRectangle</code> with the // specified <code>JRectangle</code>. Returns a new <code>JRectangle</code> // that represents the intersection of the two rectangles. // If the two rectangles do not intersect, the result will be // an empty JRectangle. // // @param r the specified <code>JRectangle</code> // @return the largest <code>JRectangle</code> contained in both the // specified <code>JRectangle</code> and in // this <code>JRectangle</code>; or if the rectangles // do not intersect, an empty JRectangle. // public JRectangle Intersection(JRectangle r) { int tx1 = this.X; int ty1 = this.Y; int rx1 = r.X; int ry1 = r.Y; long tx2 = tx1; tx2 += this.Width; long ty2 = ty1; ty2 += this.Height; long rx2 = rx1; rx2 += r.Width; long ry2 = ry1; ry2 += r.Height; if (tx1 < rx1) { tx1 = rx1; } if (ty1 < ry1) { ty1 = ry1; } if (tx2 > rx2) { tx2 = rx2; } if (ty2 > ry2) { ty2 = ry2; } tx2 -= tx1; ty2 -= ty1; // tx2,ty2 will never overflow (they will never be // larger than the smallest of the two source w,h) // they might underflow, though... if (tx2 < int.MinValue) { tx2 = int.MinValue; } if (ty2 < int.MinValue) { ty2 = int.MinValue; } return(new JRectangle(tx1, ty1, (int)tx2, (int)ty2)); }
// // Summary: // // Computes the union of this <code>JRectangle</code> with the // specified <code>JRectangle</code>. Returns a new // <code>JRectangle</code> that // represents the union of the two rectangles. // <p> // If either {@code JRectangle} has any Size less than zero // the rules for <a href=#NonExistant>non-existent</a> rectangles // apply. // If only one has a Size less than zero, then the result // will be a copy of the other {@code JRectangle}. // If both have Size less than zero, then the result will // have at least one Size less than zero. // <p> // If the resulting {@code JRectangle} would have a Size // too large to be expressed as an {@code int}, the result // will have a Size of {@code int.MaxValue} along // that Size. // @param r the specified <code>JRectangle</code> // @return the smallest <code>JRectangle</code> containing both // the specified <code>JRectangle</code> and this // <code>JRectangle</code>. // public JRectangle Union(JRectangle r) { long tx2 = this.Width; long ty2 = this.Height; if ((tx2 | ty2) < 0) { // This JRectangle has negative Sizes... // If r has non-negative Sizes then it is the answer. // If r is non-existent (has a negative Size), then both // are non-existent and we can return any non-existent JRectangle // as an answer. Thus, returning r meets that criterion. // Either way, r is our answer. return(new JRectangle(r)); } long rx2 = r.Width; long ry2 = r.Height; if ((rx2 | ry2) < 0) { return(new JRectangle(this)); } int tx1 = this.X; int ty1 = this.Y; tx2 += tx1; ty2 += ty1; int rx1 = r.X; int ry1 = r.Y; rx2 += rx1; ry2 += ry1; if (tx1 > rx1) { tx1 = rx1; } if (ty1 > ry1) { ty1 = ry1; } if (tx2 < rx2) { tx2 = rx2; } if (ty2 < ry2) { ty2 = ry2; } tx2 -= tx1; ty2 -= ty1; // tx2,ty2 will never underflow since both original rectangles // were already proven to be non-empty // they might overflow, though... if (tx2 > int.MaxValue) { tx2 = int.MaxValue; } if (ty2 > int.MaxValue) { ty2 = int.MaxValue; } return(new JRectangle(tx1, ty1, (int)tx2, (int)ty2)); }
// // Summary: // // Constructs a new <code>JRectangle</code>, initialized to match // the values of the specified <code>JRectangle</code>. // @param r the <code>JRectangle</code> from which to copy initial values // to a newly constructed <code>JRectangle</code> // @since 1.1 // public JRectangle(JRectangle r) : this(r.X, r.Y, r.Width, r.Height) { }
// // Summary: // // Checks whether or not this <code>JRectangle</code> entirely contains // the specified <code>JRectangle</code>. // // @param r the specified <code>JRectangle</code> // @return <code>true</code> if the <code>JRectangle</code> // is contained entirely inside this <code>JRectangle</code>; // <code>false</code> otherwise // @since 1.2 // public bool Contains(JRectangle r) { return(Contains(r.X, r.Y, r.Width, r.Height)); }
// // Summary: // // Sets the bounding <code>JRectangle</code> of this <code>JRectangle</code> // to match the specified <code>JRectangle</code>. // <p> // This method is included for completeness, to parallel the // <code>setBounds</code> method of <code>Component</code>. // @param r the specified <code>JRectangle</code> // @see #getBounds // @see java.awt.Component#setBounds(java.awt.JRectangle) // @since 1.1 // public void SetBounds(JRectangle r) { SetBounds(r.X, r.Y, r.Width, r.Height); }