001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006 *
007 * Project Info: http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022 * USA.
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * -----------
028 * Vector.java
029 * -----------
030 * (C) Copyright 2007, 2008, by Object Refinery Limited.
031 *
032 * Original Author: David Gilbert (for Object Refinery Limited);
033 * Contributor(s): -;
034 *
035 * Changes
036 * -------
037 * 30-Jan-2007 : Version 1 (DG);
038 * 24-May-2007 : Added getLength() and getAngle() methods, thanks to
039 * matinh (DG);
040 * 25-May-2007 : Moved from experimental to the main source tree (DG);
041 *
042 */
043
044 package org.jfree.data.xy;
045
046 import java.io.Serializable;
047
048 /**
049 * A vector.
050 *
051 * @since 1.0.6
052 */
053 public class Vector implements Serializable {
054
055 /** The vector x. */
056 private double x;
057
058 /** The vector y. */
059 private double y;
060
061 /**
062 * Creates a new instance of <code>Vector</code>.
063 *
064 * @param x the x-component.
065 * @param y the y-component.
066 */
067 public Vector(double x, double y) {
068 this.x = x;
069 this.y = y;
070 }
071
072 /**
073 * Returns the x-value.
074 *
075 * @return The x-value.
076 */
077 public double getX() {
078 return this.x;
079 }
080
081 /**
082 * Returns the y-value.
083 *
084 * @return The y-value.
085 */
086 public double getY() {
087 return this.y;
088 }
089
090 /**
091 * Returns the length of the vector.
092 *
093 * @return The vector length.
094 */
095 public double getLength() {
096 return Math.sqrt((this.x * this.x) + (this.y * this.y));
097 }
098
099 /**
100 * Returns the angle of the vector.
101 *
102 * @return The angle of the vector.
103 */
104 public double getAngle() {
105 return Math.atan2(this.y, this.x);
106 }
107
108 /**
109 * Tests this vector for equality with an arbitrary object.
110 *
111 * @param obj the object (<code>null</code> not permitted).
112 *
113 * @return A boolean.
114 */
115 public boolean equals(Object obj) {
116 if (obj == this) {
117 return true;
118 }
119 if (!(obj instanceof Vector)) {
120 return false;
121 }
122 Vector that = (Vector) obj;
123 if (this.x != that.x) {
124 return false;
125 }
126 if (this.y != that.y) {
127 return false;
128 }
129 return true;
130 }
131
132 /**
133 * Returns a hash code for this instance.
134 *
135 * @return A hash code.
136 */
137 public int hashCode() {
138 int result = 193;
139 long temp = Double.doubleToLongBits(this.x);
140 result = 37 * result + (int) (temp ^ (temp >>> 32));
141 temp = Double.doubleToLongBits(this.y);
142 result = 37 * result + (int) (temp ^ (temp >>> 32));
143 return result;
144 }
145
146 }