19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19d040edbae968d826aa2c82d382345811a45c646bDianne Hackbornimport java.io.PrintWriter;
20d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcel;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Parcelable;
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.FloatMath;
249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport com.android.internal.util.FastMath;
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * RectF holds four float coordinates for a rectangle. The rectangle is
289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * represented by the coordinates of its 4 edges (left, top, right bottom).
299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * These fields can be accessed directly. Use width() and height() to retrieve
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the rectangle's width and height. Note: most methods do not check to see that
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * the coordinates are sorted correctly (i.e. left <= right and top <= bottom).
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class RectF implements Parcelable {
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float left;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float top;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float right;
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public float bottom;
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a new empty RectF. All coordinates are initialized to 0.
419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public RectF() {}
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a new rectangle with the specified coordinates. Note: no range
469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * checking is performed, so the caller must ensure that left <= right and
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * top <= bottom.
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
49852be16790d099f95fb2c4fe7b316455662ee97fPin Ting     * @param left   The X coordinate of the left side of the rectangle
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top    The Y coordinate of the top of the rectangle
51852be16790d099f95fb2c4fe7b316455662ee97fPin Ting     * @param right  The X coordinate of the right side of the rectangle
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The Y coordinate of the bottom of the rectangle
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public RectF(float left, float top, float right, float bottom) {
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.left = left;
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.top = top;
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.right = right;
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.bottom = bottom;
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Create a new rectangle, initialized with the values in the specified
639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rectangle (which is left unmodified).
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param r The rectangle whose coordinates are copied into the new
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *          rectangle.
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public RectF(RectF r) {
69dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        if (r == null) {
70dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            left = top = right = bottom = 0.0f;
71dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        } else {
72dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            left = r.left;
73dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            top = r.top;
74dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            right = r.right;
75dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            bottom = r.bottom;
76dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        }
779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public RectF(Rect r) {
80dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        if (r == null) {
81dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            left = top = right = bottom = 0.0f;
82dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        } else {
83dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            left = r.left;
84dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            top = r.top;
85dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            right = r.right;
86dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy            bottom = r.bottom;
87dd4b1fec4c93c7417314febecebfd91e916f5ff6Romain Guy        }
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9074d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    @Override
9174d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    public boolean equals(Object o) {
9274d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        if (this == o) return true;
9374d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        if (o == null || getClass() != o.getClass()) return false;
9474d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy
953b577ddb1bca8b1c1682951fea69de24502bcf5dAmith Yamasani        RectF r = (RectF) o;
9674d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        return left == r.left && top == r.top && right == r.right && bottom == r.bottom;
9774d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    }
9874d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy
9974d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    @Override
10074d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    public int hashCode() {
10174d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        int result = (left != +0.0f ? Float.floatToIntBits(left) : 0);
10274d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        result = 31 * result + (top != +0.0f ? Float.floatToIntBits(top) : 0);
10374d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        result = 31 * result + (right != +0.0f ? Float.floatToIntBits(right) : 0);
10474d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        result = 31 * result + (bottom != +0.0f ? Float.floatToIntBits(bottom) : 0);
10574d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy        return result;
10674d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy    }
10774d7ca133a6db82abba5c0abe443fb001928e72cRomain Guy
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public String toString() {
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return "RectF(" + left + ", " + top + ", "
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                      + right + ", " + bottom + ")";
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
112d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn
113d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    /**
114d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     * Return a string representation of the rectangle in a compact form.
115d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     */
116d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    public String toShortString() {
117d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        return toShortString(new StringBuilder(32));
118d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    }
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
121d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     * Return a string representation of the rectangle in a compact form.
122d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     * @hide
123d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     */
124d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    public String toShortString(StringBuilder sb) {
125d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        sb.setLength(0);
126d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        sb.append('['); sb.append(left); sb.append(',');
127d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        sb.append(top); sb.append("]["); sb.append(right);
128d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        sb.append(','); sb.append(bottom); sb.append(']');
129d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        return sb.toString();
130d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    }
131d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn
132d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    /**
133d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     * Print short representation to given writer.
134d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     * @hide
135d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn     */
136d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    public void printShortString(PrintWriter pw) {
137d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        pw.print('['); pw.print(left); pw.print(',');
138d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        pw.print(top); pw.print("]["); pw.print(right);
139d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn        pw.print(','); pw.print(bottom); pw.print(']');
140d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    }
141d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn
142d040edbae968d826aa2c82d382345811a45c646bDianne Hackborn    /**
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if the rectangle is empty (left >= right or top >= bottom)
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final boolean isEmpty() {
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return left >= right || top >= bottom;
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the rectangle's width. This does not check for a valid rectangle
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * (i.e. left <= right) so the result may be negative.
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final float width() {
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return right - left;
1559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the rectangle's height. This does not check for a valid rectangle
1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * (i.e. top <= bottom) so the result may be negative.
1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final float height() {
1629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return bottom - top;
1639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the horizontal center of the rectangle. This does not check for
1679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a valid rectangle (i.e. left <= right)
1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final float centerX() {
1709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (left + right) * 0.5f;
1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return the vertical center of the rectangle. This does not check for
1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a valid rectangle (i.e. top <= bottom)
1769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public final float centerY() {
1789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return (top + bottom) * 0.5f;
1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the rectangle to (0,0,0,0)
1839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setEmpty() {
1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        left = right = top = bottom = 0;
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the rectangle's coordinates to the specified values. Note: no range
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * checking is performed, so it is up to the caller to ensure that
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * left <= right and top <= bottom.
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
193852be16790d099f95fb2c4fe7b316455662ee97fPin Ting     * @param left   The X coordinate of the left side of the rectangle
1949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top    The Y coordinate of the top of the rectangle
195852be16790d099f95fb2c4fe7b316455662ee97fPin Ting     * @param right  The X coordinate of the right side of the rectangle
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The Y coordinate of the bottom of the rectangle
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void set(float left, float top, float right, float bottom) {
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.left   = left;
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.top    = top;
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.right  = right;
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.bottom = bottom;
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Copy the coordinates from src into this rectangle.
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param src The rectangle whose coordinates are copied into this
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           rectangle.
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void set(RectF src) {
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.left   = src.left;
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.top    = src.top;
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.right  = src.right;
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.bottom = src.bottom;
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Copy the coordinates from src into this rectangle.
2209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param src The rectangle whose coordinates are copied into this
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           rectangle.
2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void set(Rect src) {
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.left   = src.left;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.top    = src.top;
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.right  = src.right;
2289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        this.bottom = src.bottom;
2299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Offset the rectangle by adding dx to its left and right coordinates, and
2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * adding dy to its top and bottom coordinates.
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param dx The amount to add to the rectangle's left and right coordinates
2369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param dy The amount to add to the rectangle's top and bottom coordinates
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void offset(float dx, float dy) {
2399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        left    += dx;
2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        top     += dy;
2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        right   += dx;
2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bottom  += dy;
2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Offset the rectangle to a specific (left, top) position,
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * keeping its width and height the same.
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param newLeft   The new "left" coordinate for the rectangle
2509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param newTop    The new "top" coordinate for the rectangle
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void offsetTo(float newLeft, float newTop) {
2539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        right += newLeft - left;
2549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bottom += newTop - top;
2559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        left = newLeft;
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        top = newTop;
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Inset the rectangle by (dx,dy). If dx is positive, then the sides are
2619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * moved inwards, making the rectangle narrower. If dx is negative, then the
2629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * sides are moved outwards, making the rectangle wider. The same holds true
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * for dy and the top and bottom.
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param dx The amount to add(subtract) from the rectangle's left(right)
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param dy The amount to add(subtract) from the rectangle's top(bottom)
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void inset(float dx, float dy) {
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        left    += dx;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        top     += dy;
2719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        right   -= dx;
2729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bottom  -= dy;
2739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if (x,y) is inside the rectangle. The left and top are
2779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * considered to be inside, while the right and bottom are not. This means
2789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that for a x,y to be contained: left <= x < right and top <= y < bottom.
2799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * An empty rectangle never contains any point.
2809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x The X coordinate of the point being tested for containment
2829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y The Y coordinate of the point being tested for containment
2839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff (x,y) are contained by the rectangle, where containment
2849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              means left <= x < right and top <= y < bottom
2859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean contains(float x, float y) {
2879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return left < right && top < bottom  // check for empty first
2889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && x >= left && x < right && y >= top && y < bottom;
2899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
2909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true iff the 4 specified sides of a rectangle are inside or equal
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to this rectangle. i.e. is this rectangle a superset of the specified
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rectangle. An empty rectangle never contains another rectangle.
2959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param left The left side of the rectangle being tested for containment
2979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top The top of the rectangle being tested for containment
2989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param right The right side of the rectangle being tested for containment
2999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The bottom of the rectangle being tested for containment
3009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff the the 4 specified sides of a rectangle are inside or
3019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              equal to this rectangle
3029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean contains(float left, float top, float right, float bottom) {
3049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // check for empty first
3059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return this.left < this.right && this.top < this.bottom
3069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // now check for containment
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && this.left <= left && this.top <= top
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && this.right >= right && this.bottom >= bottom;
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true iff the specified rectangle r is inside or equal to this
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rectangle. An empty rectangle never contains another rectangle.
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param r The rectangle being tested for containment.
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff the specified rectangle r is inside or equal to this
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              rectangle
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean contains(RectF r) {
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // check for empty first
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return this.left < this.right && this.top < this.bottom
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                // now check for containment
3239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && left <= r.left && top <= r.top
3249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && right >= r.right && bottom >= r.bottom;
3259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the rectangle specified by left,top,right,bottom intersects this
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rectangle, return true and set this rectangle to that intersection,
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * otherwise return false and do not change this rectangle. No check is
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * performed to see if either rectangle is empty. Note: To just test for
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * intersection, use intersects()
3339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param left The left side of the rectangle being intersected with this
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             rectangle
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top The top of the rectangle being intersected with this rectangle
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param right The right side of the rectangle being intersected with this
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              rectangle.
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The bottom of the rectangle being intersected with this
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *             rectangle.
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the specified rectangle and this rectangle intersect
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              (and this rectangle is then set to that intersection) else
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              return false and do not change this rectangle.
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean intersect(float left, float top, float right, float bottom) {
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (this.left < right && left < this.right
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && this.top < bottom && top < this.bottom) {
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (this.left < left) {
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.left = left;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (this.top < top) {
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.top = top;
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (this.right > right) {
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.right = right;
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if (this.bottom > bottom) {
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.bottom = bottom;
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return true;
3619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
3629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
3639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the specified rectangle intersects this rectangle, return true and set
3679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this rectangle to that intersection, otherwise return false and do not
3689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * change this rectangle. No check is performed to see if either rectangle
3699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * is empty. To just test for intersection, use intersects()
3709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param r The rectangle being intersected with this rectangle.
3729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if the specified rectangle and this rectangle intersect
3739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              (and this rectangle is then set to that intersection) else
3749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              return false and do not change this rectangle.
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean intersect(RectF r) {
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return intersect(r.left, r.top, r.right, r.bottom);
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If rectangles a and b intersect, return true and set this rectangle to
3829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * that intersection, otherwise return false and do not change this
3839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * rectangle. No check is performed to see if either rectangle is empty.
3849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * To just test for intersection, use intersects()
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param a The first rectangle being intersected with
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param b The second rectangle being intersected with
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff the two specified rectangles intersect. If they do, set
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              this rectangle to that intersection. If they do not, return
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              false and do not change this rectangle.
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean setIntersect(RectF a, RectF b) {
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (a.left < b.right && b.left < a.right
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && a.top < b.bottom && b.top < a.bottom) {
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            left = Math.max(a.left, b.left);
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            top = Math.max(a.top, b.top);
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            right = Math.min(a.right, b.right);
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bottom = Math.min(a.bottom, b.bottom);
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return true;
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return false;
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true if this rectangle intersects the specified rectangle.
4069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * In no event is this rectangle modified. No check is performed to see
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if either rectangle is empty. To record the intersection, use intersect()
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * or setIntersect().
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param left The left side of the rectangle being tested for intersection
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top The top of the rectangle being tested for intersection
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param right The right side of the rectangle being tested for
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              intersection
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The bottom of the rectangle being tested for intersection
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff the specified rectangle intersects this rectangle. In
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              no event is this rectangle modified.
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean intersects(float left, float top, float right,
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                              float bottom) {
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return this.left < right && left < this.right
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && this.top < bottom && top < this.bottom;
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns true iff the two specified rectangles intersect. In no event are
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * either of the rectangles modified. To record the intersection,
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * use intersect() or setIntersect().
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param a The first rectangle being tested for intersection
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param b The second rectangle being tested for intersection
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true iff the two specified rectangles intersect. In no event are
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              either of the rectangles modified.
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static boolean intersects(RectF a, RectF b) {
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return a.left < b.right && b.left < a.right
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                && a.top < b.bottom && b.top < a.bottom;
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the dst integer Rect by rounding this rectangle's coordinates
4419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * to their nearest integer values.
4429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void round(Rect dst) {
4449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst.set(FastMath.round(left), FastMath.round(top),
4459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                FastMath.round(right), FastMath.round(bottom));
4469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the dst integer Rect by rounding "out" this rectangle, choosing the
4509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * floor of top and left, and the ceiling of right and bottom.
4519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void roundOut(Rect dst) {
4539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        dst.set((int) FloatMath.floor(left), (int) FloatMath.floor(top),
4549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                (int) FloatMath.ceil(right), (int) FloatMath.ceil(bottom));
4559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Update this Rect to enclose itself and the specified rectangle. If the
4599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * specified rectangle is empty, nothing is done. If this rectangle is empty
4609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * it is set to the specified rectangle.
4619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param left The left edge being unioned with this rectangle
4639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param top The top edge being unioned with this rectangle
4649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param right The right edge being unioned with this rectangle
4659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param bottom The bottom edge being unioned with this rectangle
4669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void union(float left, float top, float right, float bottom) {
4689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if ((left < right) && (top < bottom)) {
4699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            if ((this.left < this.right) && (this.top < this.bottom)) {
4709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (this.left > left)
4719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    this.left = left;
4729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (this.top > top)
4739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    this.top = top;
4749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (this.right < right)
4759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    this.right = right;
4769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                if (this.bottom < bottom)
4779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    this.bottom = bottom;
4789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            } else {
4799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.left = left;
4809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.top = top;
4819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.right = right;
4829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                this.bottom = bottom;
4839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            }
4849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
4859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Update this Rect to enclose itself and the specified rectangle. If the
4899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * specified rectangle is empty, nothing is done. If this rectangle is empty
4909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * it is set to the specified rectangle.
4919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param r The rectangle being unioned with this rectangle
4939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void union(RectF r) {
4959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        union(r.left, r.top, r.right, r.bottom);
4969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
4979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Update this Rect to enclose itself and the [x,y] coordinate. There is no
5009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * check to see that this rectangle is non-empty.
5019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param x The x coordinate of the point to add to the rectangle
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param y The y coordinate of the point to add to the rectangle
5049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void union(float x, float y) {
5069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (x < left) {
5079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            left = x;
5089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (x > right) {
5099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            right = x;
5109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (y < top) {
5129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            top = y;
5139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } else if (y > bottom) {
5149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bottom = y;
5159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Swap top/bottom or left/right if there are flipped (i.e. left > right
5209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and/or top > bottom). This can be called if
5219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the edges are computed separately, and may have crossed over each other.
5229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If the edges are already correct (i.e. left <= right and top <= bottom)
5239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * then nothing is done.
5249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void sort() {
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (left > right) {
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            float temp = left;
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            left = right;
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            right = temp;
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (top > bottom) {
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            float temp = top;
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            top = bottom;
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            bottom = temp;
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Parcelable interface methods
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int describeContents() {
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return 0;
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Write this rectangle to the specified parcel. To restore a rectangle from
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * a parcel, use readFromParcel()
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param out The parcel to write the rectangle's coordinates into
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void writeToParcel(Parcel out, int flags) {
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        out.writeFloat(left);
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        out.writeFloat(top);
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        out.writeFloat(right);
5549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        out.writeFloat(bottom);
5559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final Parcelable.Creator<RectF> CREATOR = new Parcelable.Creator<RectF>() {
5589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
5599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Return a new rectangle from the data in the specified parcel.
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public RectF createFromParcel(Parcel in) {
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            RectF r = new RectF();
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            r.readFromParcel(in);
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return r;
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        /**
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         * Return an array of rectangles of the specified size.
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project         */
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        public RectF[] newArray(int size) {
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return new RectF[size];
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    };
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Set the rectangle's coordinates from the data stored in the specified
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * parcel. To write a rectangle to a parcel, call writeToParcel().
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param in The parcel to read the rectangle's coordinates from
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void readFromParcel(Parcel in) {
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        left = in.readFloat();
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        top = in.readFloat();
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        right = in.readFloat();
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        bottom = in.readFloat();
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
5879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
588