1 package org.opencv.core;
2 
3 //javadoc:RotatedRect_
4 public class RotatedRect {
5 
6     public Point center;
7     public Size size;
8     public double angle;
9 
RotatedRect()10     public RotatedRect() {
11         this.center = new Point();
12         this.size = new Size();
13         this.angle = 0;
14     }
15 
RotatedRect(Point c, Size s, double a)16     public RotatedRect(Point c, Size s, double a) {
17         this.center = c.clone();
18         this.size = s.clone();
19         this.angle = a;
20     }
21 
RotatedRect(double[] vals)22     public RotatedRect(double[] vals) {
23         this();
24         set(vals);
25     }
26 
set(double[] vals)27     public void set(double[] vals) {
28         if (vals != null) {
29             center.x = vals.length > 0 ? (double) vals[0] : 0;
30             center.y = vals.length > 1 ? (double) vals[1] : 0;
31             size.width = vals.length > 2 ? (double) vals[2] : 0;
32             size.height = vals.length > 3 ? (double) vals[3] : 0;
33             angle = vals.length > 4 ? (double) vals[4] : 0;
34         } else {
35             center.x = 0;
36             center.y = 0;
37             size.width = 0;
38             size.height = 0;
39             angle = 0;
40         }
41     }
42 
points(Point pt[])43     public void points(Point pt[])
44     {
45         double _angle = angle * Math.PI / 180.0;
46         double b = (double) Math.cos(_angle) * 0.5f;
47         double a = (double) Math.sin(_angle) * 0.5f;
48 
49         pt[0] = new Point(
50                 center.x - a * size.height - b * size.width,
51                 center.y + b * size.height - a * size.width);
52 
53         pt[1] = new Point(
54                 center.x + a * size.height - b * size.width,
55                 center.y - b * size.height - a * size.width);
56 
57         pt[2] = new Point(
58                 2 * center.x - pt[0].x,
59                 2 * center.y - pt[0].y);
60 
61         pt[3] = new Point(
62                 2 * center.x - pt[1].x,
63                 2 * center.y - pt[1].y);
64     }
65 
boundingRect()66     public Rect boundingRect()
67     {
68         Point pt[] = new Point[4];
69         points(pt);
70         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)),
71                 (int) Math.floor(Math.min(Math.min(Math.min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
72                 (int) Math.ceil(Math.max(Math.max(Math.max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
73                 (int) Math.ceil(Math.max(Math.max(Math.max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
74         r.width -= r.x - 1;
75         r.height -= r.y - 1;
76         return r;
77     }
78 
clone()79     public RotatedRect clone() {
80         return new RotatedRect(center, size, angle);
81     }
82 
83     @Override
hashCode()84     public int hashCode() {
85         final int prime = 31;
86         int result = 1;
87         long temp;
88         temp = Double.doubleToLongBits(center.x);
89         result = prime * result + (int) (temp ^ (temp >>> 32));
90         temp = Double.doubleToLongBits(center.y);
91         result = prime * result + (int) (temp ^ (temp >>> 32));
92         temp = Double.doubleToLongBits(size.width);
93         result = prime * result + (int) (temp ^ (temp >>> 32));
94         temp = Double.doubleToLongBits(size.height);
95         result = prime * result + (int) (temp ^ (temp >>> 32));
96         temp = Double.doubleToLongBits(angle);
97         result = prime * result + (int) (temp ^ (temp >>> 32));
98         return result;
99     }
100 
101     @Override
equals(Object obj)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
toString()110     public String toString() {
111         return "{ " + center + " " + size + " * " + angle + " }";
112     }
113 }
114