天天看點

Rect

/*Rect*/

public final classRect implements Parcelable{

    public int left;

    public int top;

    public int right;

    public int bottom;

    private static final Pattern FLATTENED_PATTERN = Pattern.compile(

           "(-?\\d+) (-?\\d+) (-?\\d+)(-?\\d+)");

    public Rect() {}

    public Rect(int left, int top, int right, int bottom) {

       this.left = left;

       this.top = top;

       this.right = right;

       this.bottom = bottom;

    }

    public Rect(Rect r) {

       if(r == null){

           left= top = right = bottom= 0;

       } else{

           left= r.left;

           top= r.top;

           right= r.right;

           bottom= r.bottom;

       }

    }

    @Override

    public booleanequals(Object o) {

       if(this == o) return true;

       if(o == null|| getClass() != o.getClass()) return false;

       Rect r = (Rect) o;

       return left == r.left && top == r.top && right == r.right && bottom == r.bottom;

    }

    @Override

    public inthashCode() {

       intresult = left;

       result = 31 * result + top;

       result = 31 * result + right;

       result = 31 * result + bottom;

       returnresult;

    }

    @Override

    public String toString() {

       StringBuilder sb = newStringBuilder(32);

       sb.append("Rect(");sb.append(left);sb.append(", ");

       sb.append(top);sb.append(" - ");sb.append(right);

       sb.append(", ");sb.append(bottom);sb.append(")");

       returnsb.toString();

    }

    public String toShortString() {

       returntoShortString(newStringBuilder(32));

    }

    public String toShortString(StringBuilder sb) {

       sb.setLength(0);

       sb.append('[');sb.append(left);sb.append(',');

       sb.append(top);sb.append("][");sb.append(right);

       sb.append(',');sb.append(bottom);sb.append(']');

       returnsb.toString();

    }

    public String flattenToString() {

       StringBuilder sb = newStringBuilder(32);

       // WARNING: Do not change the format ofthis string, it must be

       // preserved because Rects are savedin this flattened format.

       sb.append(left);

       sb.append(' ');

       sb.append(top);

       sb.append(' ');

       sb.append(right);

       sb.append(' ');

       sb.append(bottom);

       returnsb.toString();

    }

    public staticRect unflattenFromString(String str) {

       Matcher matcher = FLATTENED_PATTERN.matcher(str);

       if(!matcher.matches()) {

           return null;

       }

       return new Rect(Integer.parseInt(matcher.group(1)),

                Integer.parseInt(matcher.group(2)),

                Integer.parseInt(matcher.group(3)),

                Integer.parseInt(matcher.group(4)));

    }

    public voidprintShortString(PrintWriter pw) {

       pw.print('[');pw.print(left);pw.print(',');

       pw.print(top);pw.print("][");pw.print(right);

       pw.print(',');pw.print(bottom);pw.print(']');

    }

    public final boolean isEmpty() {

       return left >= right || top>= bottom;

    }

    public final int width() {

       return right - left;

    }

    public final int height() {

       return bottom - top;

    }

    public final int centerX() {

       return(left + right) >> 1;

    }

    public final int centerY() {

       return(top + bottom) >> 1;

    }

    public final float exactCenterX() {

       return(left + right) * 0.5f;

    }

    public final float exactCenterY() {

       return(top + bottom) * 0.5f;

    }

    public voidsetEmpty() {

       left= right = top = bottom= 0;

    }

    public voidset(intleft, inttop, intright, intbottom) {

       this.left = left;

       this.top = top;

       this.right = right;

       this.bottom = bottom;

    }

    public voidset(Rect src) {

       this.left = src.left;

       this.top = src.top;

       this.right = src.right;

       this.bottom = src.bottom;

    }

    public voidoffset(intdx, intdy) {

       left+= dx;

       top+= dy;

       right+= dx;

       bottom+= dy;

    }

    public voidoffsetTo(intnewLeft, intnewTop) {

       right+= newLeft - left;

       bottom+= newTop - top;

       left= newLeft;

       top= newTop;

    }

    public voidinset(intdx, intdy) {

       left+= dx;

       top+= dy;

       right-= dx;

       bottom-= dy;

    }

    public booleancontains(intx, inty) {

       return left < right && top < bottom  // check for empty first

              && x >= left&& x < right&& y >= top&& y < bottom;

    }

    public booleancontains(intleft, inttop, intright, intbottom) {

              // check for empty first

       return this.left< this.right && this.top< this.bottom

              // now check for containment

                && this.left<= left && this.top <= top

                && this.right>= right && this.bottom >= bottom;

    }

    public booleancontains(Rect r) {

              // check for empty first

       return this.left< this.right && this.top< this.bottom

              // now check for containment

              && left<= r.left&& top<= r.top&& right>= r.right&& bottom>= r.bottom;

    }

    public booleanintersect(intleft, inttop, intright, intbottom) {

       if(this.left < right && left < this.right&& this.top < bottom && top < this.bottom){

           if(this.left < left) this.left= left;

           if(this.top < top) this.top= top;

           if(this.right > right) this.right= right;

           if(this.bottom > bottom) this.bottom= bottom;

           return true;

       }

       return false;

    }

    public booleanintersect(Rect r) {

       returnintersect(r.left,r.top, r.right, r.bottom);

    }

    public booleansetIntersect(Rect a, Rect b) {

       if(a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) {

           left= Math.max(a.left,b.left);

           top= Math.max(a.top,b.top);

           right= Math.min(a.right,b.right);

           bottom= Math.min(a.bottom,b.bottom);

           return true;

       }

       return false;

    }

    public booleanintersects(intleft, inttop, intright, intbottom) {

       return this.left< right && left < this.right && this.top< bottom && top < this.bottom;

    }

    public static boolean intersects(Rect a, Rect b) {

       returna.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom;

    }

    public voidunion(intleft, inttop, intright, intbottom) {

       if((left < right) && (top < bottom)) {

           if((this.left < this.right)&& (this.top < this.bottom)){

                if (this.left> left) this.left = left;

                if (this.top> top) this.top = top;

                if (this.right< right) this.right = right;

                if (this.bottom< bottom) this.bottom = bottom;

           } else{

                this.left= left;

                this.top= top;

                this.right= right;

                this.bottom= bottom;

           }

       }

    }

    public voidunion(Rect r) {

       union(r.left,r.top, r.right, r.bottom);

    }

    public voidunion(intx, inty) {

       if(x < left){

           left= x;

       } else if (x > right) {

           right= x;

       }

       if(y < top){

           top= y;

       } else if (y > bottom) {

           bottom= y;

       }

    }

    public voidsort() {

       if(left > right) {

           inttemp = left;

           left= right;

           right= temp;

       }

       if(top > bottom) {

           inttemp = top;

           top= bottom;

           bottom= temp;

       }

    }

    public intdescribeContents() {

       return0;

    }

    public voidwriteToParcel(Parcel out, intflags) {

        out.writeInt(left);

       out.writeInt(top);

       out.writeInt(right);

       out.writeInt(bottom);

    }

    public static final Parcelable.Creator<Rect>CREATOR = new Parcelable.Creator<Rect>(){

       publicRect createFromParcel(Parcel in) {

           Rect r = newRect();

           r.readFromParcel(in);

           returnr;

       }

       publicRect[] newArray(intsize) {

           return new Rect[size];

       }

    };

    public voidreadFromParcel(Parcel in) {

       left= in.readInt();

       top= in.readInt();

       right= in.readInt();

       bottom= in.readInt();

    }

    public voidscale(floatscale) {

       if(scale != 1.0f) {

           left= (int)(left * scale +0.5f);

           top= (int)(top * scale +0.5f);

           right= (int)(right * scale +0.5f);

           bottom= (int)(bottom * scale +0.5f);

       }

    }

}