Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.geometry;
027
028/**
029 * A 2D rectangle used to describe the bounds of an object. It is defined by a
030 * location (minX, minY) and dimension (width x height).
031 */
032public class Rectangle2D {
033    /**
034     * An empty {@code Rectangle2D} instance (with all coordinates equal to zero).
035     */
036    public static final Rectangle2D EMPTY = new Rectangle2D(0, 0, 0, 0);
037
038    /**
039     * The x coordinate of the upper-left corner of this {@code Rectangle2D}.
040     *
041     * @defaultValue 0.0
042     */
043    public double getMinX() { return minX; }
044    private double minX;
045
046    /**
047     * The y coordinate of the upper-left corner of this {@code Rectangle2D}.
048     *
049     * @defaultValue 0.0
050     */
051    public double getMinY() { return minY; }
052    private double minY;
053
054    /**
055     * The width of this {@code Rectangle2D}.
056     *
057     * @defaultValue 0.0
058     */
059    public double getWidth() { return width; }
060    private double width;
061
062    /**
063     * The height of this {@code Rectangle2D}.
064     *
065     * @defaultValue 0.0
066     */
067    public double getHeight() { return height; }
068    private double height;
069
070    /**
071     * The x coordinate of the lower-right corner of this {@code Rectangle2D}.
072     *
073     * @defaultValue {@code minX + width}
074     */
075    public double getMaxX() { return maxX; }
076    private double maxX;
077
078    /**
079     * The y coordinate of the lower-right corner of this {@code Rectangle2D}.
080     *
081     * @defaultValue {@code minY + height}
082     */
083    public double getMaxY() { return maxY; }
084    private double maxY;
085
086    /**
087     * Cache the hash code to make computing hashes faster.
088     */
089    private int hash = 0;
090
091    /**
092     * Creates a new instance of {@code Rectangle2D}.
093     * @param minX The x coordinate of the upper-left corner of the {@code Rectangle2D}
094     * @param minY The y coordinate of the upper-left corner of the {@code Rectangle2D}
095     * @param width The width of the {@code Rectangle2D}
096     * @param height The height of the {@code Rectangle2D} 
097     */
098    public Rectangle2D(double minX, double minY, double width, double height) {
099        if (width < 0 || height < 0) {
100            throw new IllegalArgumentException("Both width and height must be >= 0");
101        }
102
103        this.minX = minX;
104        this.minY = minY;
105        this.width = width;
106        this.height = height;
107        this.maxX = minX + width;
108        this.maxY = minY + height;
109    }
110
111   /**
112    * Tests if the specified point is inside the boundary of {@code Rectangle2D}.
113    *
114    * @param p the specified point to be tested
115    * @return true if the specified point is inside the boundary of this
116    * {@code Rectangle2D}; false otherwise.
117    */
118    public boolean contains(Point2D p) {
119        if (p == null) return false;
120        return contains(p.getX(), p.getY());
121    }
122
123   /**
124    * Tests if the specified {@code (x, y)} coordinates are inside the boundary
125    * of {@code Rectangle2D}.
126    *
127    * @param x the specified x coordinate to be tested
128    * @param y the specified y coordinate to be tested
129    * @return true if the specified {@code (x, y)} coordinates are inside the
130    * boundary of this {@code Rectangle2D}; false otherwise.
131    */
132    public boolean contains(double x, double y) {
133        return x >= minX && x <= maxX && y >= minY && y <= maxY;
134    }
135
136   /**
137    * Tests if the interior of this {@code Rectangle2D} entirely contains the
138    * specified Rectangle2D, {@code r}.
139    *
140    * @param r The specified Rectangle2D
141    * @return true if the specified Rectangle2D, {@code r}, is inside the
142    * boundary of this {@code Rectangle2D}; false otherwise.
143    */
144    public boolean contains(Rectangle2D r) {
145        if (r == null) return false;
146        return r.minX >= minX && r.minY >= minY && r.maxX <= maxX && r.maxY <= maxY;
147    }
148
149   /**
150    * Tests if the interior of this {@code Rectangle2D} entirely contains the
151    * specified rectangular area.
152    *
153    * @param x the x coordinate of the upper-left corner of the specified
154    * rectangular area
155    * @param y the y coordinate of the upper-left corner of the specified
156    * rectangular area
157    * @param w the width of the specified rectangular area
158    * @param h the height of the specified rectangular area
159    * @return true if the interior of this {@code Rectangle2D} entirely contains
160    * the specified rectangular area; false otherwise.
161    */
162    public boolean contains(double x, double y, double w, double h) {
163        return x >= minX && y >= minY && w <= maxX - x && h <= maxY - y;
164    }
165
166   /**
167    * Tests if the interior of this {@code Rectangle2D} intersects the interior
168    * of a specified Rectangle2D, {@code r}.
169    *
170    * @param r The specified Rectangle2D
171    * @return true if the interior of this {@code Rectangle2D} and the interior
172    * of the specified Rectangle2D, {@code r}, intersect.
173    */
174    public boolean intersects(Rectangle2D r) {
175        if (r == null) return false;
176        return r.maxX > minX && r.maxY > minY && r.minX < maxX && r.minY < maxY;
177    }
178
179   /**
180    * Tests if the interior of this {@code Rectangle2D} intersects the interior
181    * of a specified rectangular area.
182    *
183    * @param x the x coordinate of the upper-left corner of the specified
184    * rectangular area
185    * @param y the y coordinate of the upper-left corner of the specified
186    * rectangular area
187    * @param w the width of the specified rectangular area
188    * @param h the height of the specified rectangular area
189    * @return true if the interior of this {@code Rectangle2D} and the interior
190    * of the rectangular area intersect.
191    */
192    public boolean intersects(double x, double y, double w, double h) {
193        return x < maxX && y < maxY && x + w > minX && y + h > minY;
194    }
195
196    /**
197     * Indicates whether some other object is "equal to" this one.
198     * @param obj the reference object with which to compare.
199     * @return {@code true} if this object is equal to the {@code obj} argument; {@code false} otherwise.
200     */
201    @Override public boolean equals(Object obj) {
202        if (obj == this) return true;
203        if (obj instanceof Rectangle2D) {
204            Rectangle2D other = (Rectangle2D) obj;
205            return minX == other.minX
206                && minY == other.minY
207                && width == other.width
208                && height == other.height;
209        } else return false;
210    }
211
212    /**
213     * Returns a hash code for this {@code Rectangle2D} object.
214     * @return a hash code for this {@code Rectangle2D} object.
215     */ 
216    @Override public int hashCode() {
217        if (hash == 0) {
218            long bits = 7L;
219            bits = 31L * bits + Double.doubleToLongBits(minX);
220            bits = 31L * bits + Double.doubleToLongBits(minY);
221            bits = 31L * bits + Double.doubleToLongBits(width);
222            bits = 31L * bits + Double.doubleToLongBits(height);
223            hash = (int) (bits ^ (bits >> 32));
224        }
225        return hash;
226    }
227
228    /**
229     * Returns a string representation of this {@code Rectangle2D}.
230     * This method is intended to be used only for informational purposes.
231     * The content and format of the returned string might vary between
232     * implementations.
233     * The returned string might be empty but cannot be {@code null}.
234     */
235    @Override public String toString() {
236        return "Rectangle2D [minX = " + minX
237                + ", minY=" + minY
238                + ", maxX=" + maxX
239                + ", maxY=" + maxY
240                + ", width=" + width
241                + ", height=" + height
242                + "]";
243    }
244}