OSDN Git Service

ライブビューデータを拾うために検討中。その4。
[gokigen/FujiCam.git] / opencv41 / src / main / java / org / opencv / core / Rect.java
1 package org.opencv.core;
2
3 //javadoc:Rect_
4 public class Rect {
5
6     public int x, y, width, height;
7
8     public Rect(int x, int y, int width, int height) {
9         this.x = x;
10         this.y = y;
11         this.width = width;
12         this.height = height;
13     }
14
15     public Rect() {
16         this(0, 0, 0, 0);
17     }
18
19     public Rect(Point p1, Point p2) {
20         x = (int) (p1.x < p2.x ? p1.x : p2.x);
21         y = (int) (p1.y < p2.y ? p1.y : p2.y);
22         width = (int) (p1.x > p2.x ? p1.x : p2.x) - x;
23         height = (int) (p1.y > p2.y ? p1.y : p2.y) - y;
24     }
25
26     public Rect(Point p, Size s) {
27         this((int) p.x, (int) p.y, (int) s.width, (int) s.height);
28     }
29
30     public Rect(double[] vals) {
31         set(vals);
32     }
33
34     public void set(double[] vals) {
35         if (vals != null) {
36             x = vals.length > 0 ? (int) vals[0] : 0;
37             y = vals.length > 1 ? (int) vals[1] : 0;
38             width = vals.length > 2 ? (int) vals[2] : 0;
39             height = vals.length > 3 ? (int) vals[3] : 0;
40         } else {
41             x = 0;
42             y = 0;
43             width = 0;
44             height = 0;
45         }
46     }
47
48     public Rect clone() {
49         return new Rect(x, y, width, height);
50     }
51
52     public Point tl() {
53         return new Point(x, y);
54     }
55
56     public Point br() {
57         return new Point(x + width, y + height);
58     }
59
60     public Size size() {
61         return new Size(width, height);
62     }
63
64     public double area() {
65         return width * height;
66     }
67
68     public boolean empty() {
69         return width <= 0 || height <= 0;
70     }
71
72     public boolean contains(Point p) {
73         return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height;
74     }
75
76     @Override
77     public int hashCode() {
78         final int prime = 31;
79         int result = 1;
80         long temp;
81         temp = Double.doubleToLongBits(height);
82         result = prime * result + (int) (temp ^ (temp >>> 32));
83         temp = Double.doubleToLongBits(width);
84         result = prime * result + (int) (temp ^ (temp >>> 32));
85         temp = Double.doubleToLongBits(x);
86         result = prime * result + (int) (temp ^ (temp >>> 32));
87         temp = Double.doubleToLongBits(y);
88         result = prime * result + (int) (temp ^ (temp >>> 32));
89         return result;
90     }
91
92     @Override
93     public boolean equals(Object obj) {
94         if (this == obj) return true;
95         if (!(obj instanceof Rect)) return false;
96         Rect it = (Rect) obj;
97         return x == it.x && y == it.y && width == it.width && height == it.height;
98     }
99
100     @Override
101     public String toString() {
102         return "{" + x + ", " + y + ", " + width + "x" + height + "}";
103     }
104 }