001package org.opencv.core;
002
003//javadoc:Rect_
004public class Rect {
005
006    public int x, y, width, height;
007
008    public Rect(int x, int y, int width, int height) {
009        this.x = x;
010        this.y = y;
011        this.width = width;
012        this.height = height;
013    }
014
015    public Rect() {
016        this(0, 0, 0, 0);
017    }
018
019    public Rect(Point p1, Point p2) {
020        x = (int) (p1.x < p2.x ? p1.x : p2.x);
021        y = (int) (p1.y < p2.y ? p1.y : p2.y);
022        width = (int) (p1.x > p2.x ? p1.x : p2.x) - x;
023        height = (int) (p1.y > p2.y ? p1.y : p2.y) - y;
024    }
025
026    public Rect(Point p, Size s) {
027        this((int) p.x, (int) p.y, (int) s.width, (int) s.height);
028    }
029
030    public Rect(double[] vals) {
031        set(vals);
032    }
033
034    public void set(double[] vals) {
035        if (vals != null) {
036            x = vals.length > 0 ? (int) vals[0] : 0;
037            y = vals.length > 1 ? (int) vals[1] : 0;
038            width = vals.length > 2 ? (int) vals[2] : 0;
039            height = vals.length > 3 ? (int) vals[3] : 0;
040        } else {
041            x = 0;
042            y = 0;
043            width = 0;
044            height = 0;
045        }
046    }
047
048    public Rect clone() {
049        return new Rect(x, y, width, height);
050    }
051
052    public Point tl() {
053        return new Point(x, y);
054    }
055
056    public Point br() {
057        return new Point(x + width, y + height);
058    }
059
060    public Size size() {
061        return new Size(width, height);
062    }
063
064    public double area() {
065        return width * height;
066    }
067
068    public boolean empty() {
069        return width <= 0 || height <= 0;
070    }
071
072    public boolean contains(Point p) {
073        return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
074    }
075
076    @Override
077    public int hashCode() {
078        final int prime = 31;
079        int result = 1;
080        long temp;
081        temp = Double.doubleToLongBits(height);
082        result = prime * result + (int) (temp ^ (temp >>> 32));
083        temp = Double.doubleToLongBits(width);
084        result = prime * result + (int) (temp ^ (temp >>> 32));
085        temp = Double.doubleToLongBits(x);
086        result = prime * result + (int) (temp ^ (temp >>> 32));
087        temp = Double.doubleToLongBits(y);
088        result = prime * result + (int) (temp ^ (temp >>> 32));
089        return result;
090    }
091
092    @Override
093    public boolean equals(Object obj) {
094        if (this == obj) return true;
095        if (!(obj instanceof Rect)) return false;
096        Rect it = (Rect) obj;
097        return x == it.x && y == it.y && width == it.width && height == it.height;
098    }
099
100    @Override
101    public String toString() {
102        return "{" + x + ", " + y + ", " + width + "x" + height + "}";
103    }
104}