001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
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
044package org.jfree.data.xy;
045
046import java.io.Serializable;
047
048/**
049 * A vector.
050 *
051 * @since 1.0.6
052 */
053public 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    @Override
116    public boolean equals(Object obj) {
117        if (obj == this) {
118            return true;
119        }
120        if (!(obj instanceof Vector)) {
121            return false;
122        }
123        Vector that = (Vector) obj;
124        if (this.x != that.x) {
125            return false;
126        }
127        if (this.y != that.y) {
128            return false;
129        }
130        return true;
131    }
132
133    /**
134     * Returns a hash code for this instance.
135     *
136     * @return A hash code.
137     */
138    @Override
139    public int hashCode() {
140        int result = 193;
141        long temp = Double.doubleToLongBits(this.x);
142        result = 37 * result + (int) (temp ^ (temp >>> 32));
143        temp = Double.doubleToLongBits(this.y);
144        result = 37 * result + (int) (temp ^ (temp >>> 32));
145        return result;
146    }
147
148}