pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / spatial / src / java / org / apache / lucene / spatial / geometry / shape / Vector2D.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  * 2D vector
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 Vector2D {
29   private double x;
30   private double y;
31
32   /**
33    * Create a vector from the origin of the coordinate system to the given
34    * point
35    * 
36    * @param x
37    * @param y
38    */
39   public Vector2D(double x, double y) {
40     this.x = x;
41     this.y = y;
42   }
43
44   /**
45    * Create a vector from the origin of the coordinate system to the given
46    * point
47    */
48   public Vector2D(Point2D p) {
49     this(p.getX(), p.getY());
50   }
51
52   /**
53    * Create a vector from one point to another
54    * 
55    * @param from
56    * @param to
57    */
58   public Vector2D(Point2D from, Point2D to) {
59     this(to.getX() - from.getX(), to.getY() - from.getY());
60   }
61
62   public Vector2D() {
63     this.x = 0;
64     this.y = 0;
65   }
66
67   public Vector2D(Vector2D other) {
68     this.x = other.x;
69     this.y = other.y;
70   }
71
72   public double getX() {
73     return x;
74   }
75
76   public double getY() {
77     return y;
78   }
79
80   public void setX(double x) {
81     this.x = x;
82   }
83
84   public void setY(double y) {
85     this.y = y;
86   }
87
88   public void set(double x, double y) {
89     this.x = x;
90     this.y = y;
91   }
92
93   public boolean equals(Vector2D other) {
94     return other != null && x == other.x && y == other.y;
95   }
96
97   public double dot(Vector2D in) {
98     return ((x) * in.x) + (y * in.y);
99   }
100
101   /**
102    * Vector length (magnitude) squared
103    */
104   public double normSqr() {
105     // Cast to F to prevent overflows
106     return (x * x) + (y * y);
107   }
108
109   public double norm() {
110     return Math.sqrt(normSqr());
111   }
112
113   public Vector2D mult(double d) {
114     return new Vector2D(x*d, y*d);
115   }
116
117   @Override
118   public int hashCode() {
119     final int prime = 31;
120     int result = 1;
121     long temp;
122     temp = Double.doubleToLongBits(x);
123     result = prime * result + (int) (temp ^ (temp >>> 32));
124     temp = Double.doubleToLongBits(y);
125     result = prime * result + (int) (temp ^ (temp >>> 32));
126     return result;
127   }
128
129   @Override
130   public boolean equals(Object obj) {
131     if (this == obj)
132       return true;
133     if (obj == null)
134       return false;
135     if (getClass() != obj.getClass())
136       return false;
137     Vector2D other = (Vector2D) obj;
138     if (Double.doubleToLongBits(x) != Double.doubleToLongBits(other.x))
139       return false;
140     if (Double.doubleToLongBits(y) != Double.doubleToLongBits(other.y))
141       return false;
142     return true;
143   }
144   
145 }