001package org.opencv.core;
002
003//javadoc:Rect2d_
004public class Rect2d {
005
006    public double x, y, width, height;
007
008    public Rect2d(double x, double y, double width, double height) {
009        this.x = x;
010        this.y = y;
011        this.width = width;
012        this.height = height;
013    }
014
015    public Rect2d() {
016        this(0, 0, 0, 0);
017    }
018
019    public Rect2d(Point p1, Point p2) {
020        x = (double) (p1.x < p2.x ? p1.x : p2.x);
021        y = (double) (p1.y < p2.y ? p1.y : p2.y);
022        width = (double) (p1.x > p2.x ? p1.x : p2.x) - x;
023        height = (double) (p1.y > p2.y ? p1.y : p2.y) - y;
024    }
025
026    public Rect2d(Point p, Size s) {
027        this((double) p.x, (double) p.y, (double) s.width, (double) s.height);
028    }
029
030    public Rect2d(double[] vals) {
031        set(vals);
032    }
033
034    public void set(double[] vals) {
035        if (vals != null) {
036            x = vals.length > 0 ? (double) vals[0] : 0;
037            y = vals.length > 1 ? (double) vals[1] : 0;
038            width = vals.length > 2 ? (double) vals[2] : 0;
039            height = vals.length > 3 ? (double) vals[3] : 0;
040        } else {
041            x = 0;
042            y = 0;
043            width = 0;
044            height = 0;
045        }
046    }
047
048    public Rect2d clone() {
049        return new Rect2d(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 Rect2d)) return false;
096        Rect2d it = (Rect2d) 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}