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