add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / shape / Rectangle.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.spatial.geometry.shape;
19
20
21 /**
22  * Rectangle shape.  
23  *
24  * <p><font color="red"><b>NOTE:</b> This API is still in
25  * flux and might change in incompatible ways in the next
26  * release.</font>
27  */
28 public class Rectangle implements Geometry2D {
29   private Point2D ptMin, ptMax;
30   
31   public Rectangle() {
32     ptMin=new Point2D(-1, 1);
33     ptMax=new Point2D(1, 1);
34   }
35   
36   public Rectangle(Point2D ptMin, Point2D ptMax) {
37     this.ptMin=new Point2D(ptMin);
38     this.ptMax=new Point2D(ptMax);
39   }
40   
41   public Rectangle(double x1, double y1, double x2, double y2) {
42     set(x1, y1, x2, y2);
43   }
44
45   @Override
46   public String toString() {
47     return "[" + ptMin + "," + ptMax + "]";
48   }
49   
50   private void set(double x1, double y1, double x2, double y2) {
51     this.ptMin=new Point2D(Math.min(x1, x2), Math.min(y1, y2));
52     this.ptMax=new Point2D(Math.max(x1, x2), Math.max(y1, y2));
53   }
54   
55   public double area() {
56     return (ptMax.getX() - ptMin.getX()) * (ptMax.getY() - ptMin.getY());
57   }
58
59   public Point2D centroid() {
60     return new Point2D( (ptMin.getX() + ptMax.getX()) / 2,
61                   (ptMin.getY() + ptMax.getY()) / 2);
62   }
63
64   public boolean contains(Point2D p) {
65     return p.getX() >= ptMin.getX() && 
66       p.getX() <= ptMax.getX() &&
67       p.getY() >= ptMin.getY() &&
68       p.getY() <= ptMax.getY();
69   }
70
71   public void translate(Vector2D v) {
72     ptMin.add(v);
73     ptMax.add(v);
74   }
75
76   Point2D MinPt() {
77     return ptMin;
78   }
79
80   Point2D MaxPt() {
81     return ptMax;
82   }
83
84   public IntersectCase intersect(Rectangle r) {
85     throw new UnsupportedOperationException();
86     // TODO
87   }
88
89   public Point2D getMaxPoint() {
90     return ptMax;
91   }
92
93   public Point2D getMinPoint() {
94     return ptMin;
95   }
96
97   @Override
98   public int hashCode() {
99     final int prime = 31;
100     int result = 1;
101     result = prime * result + ((ptMax == null) ? 0 : ptMax.hashCode());
102     result = prime * result + ((ptMin == null) ? 0 : ptMin.hashCode());
103     return result;
104   }
105
106   @Override
107   public boolean equals(Object obj) {
108     if (this == obj)
109       return true;
110     if (obj == null)
111       return false;
112     if (getClass() != obj.getClass())
113       return false;
114     Rectangle other = (Rectangle) obj;
115     if (ptMax == null) {
116       if (other.ptMax != null)
117         return false;
118     } else if (!ptMax.equals(other.ptMax))
119       return false;
120     if (ptMin == null) {
121       if (other.ptMin != null)
122         return false;
123     } else if (!ptMin.equals(other.ptMin))
124       return false;
125     return true;
126   }
127
128 }