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 contains(Point p) { 069 return x <= p.x && p.x < x + width && y <= p.y && p.y < y + height; 070 } 071 072 @Override 073 public int hashCode() { 074 final int prime = 31; 075 int result = 1; 076 long temp; 077 temp = Double.doubleToLongBits(height); 078 result = prime * result + (int) (temp ^ (temp >>> 32)); 079 temp = Double.doubleToLongBits(width); 080 result = prime * result + (int) (temp ^ (temp >>> 32)); 081 temp = Double.doubleToLongBits(x); 082 result = prime * result + (int) (temp ^ (temp >>> 32)); 083 temp = Double.doubleToLongBits(y); 084 result = prime * result + (int) (temp ^ (temp >>> 32)); 085 return result; 086 } 087 088 @Override 089 public boolean equals(Object obj) { 090 if (this == obj) return true; 091 if (!(obj instanceof Rect)) return false; 092 Rect it = (Rect) obj; 093 return x == it.x && y == it.y && width == it.width && height == it.height; 094 } 095 096 @Override 097 public String toString() { 098 return "{" + x + ", " + y + ", " + width + "x" + height + "}"; 099 } 100}