Source for org.jfree.data.xy.Vector

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -----------
  28:  * Vector.java
  29:  * -----------
  30:  * (C) Copyright 2007, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 30-Jan-2007 : Version 1 (DG);
  38:  * 24-May-2007 : Added getLength() and getAngle() methods, thanks to 
  39:  *               matinh (DG);
  40:  * 25-May-2007 : Moved from experimental to the main source tree (DG);
  41:  *
  42:  */
  43: 
  44: package org.jfree.data.xy;
  45: 
  46: import java.io.Serializable;
  47: 
  48: /**
  49:  * A vector.
  50:  * 
  51:  * @since 1.0.6
  52:  */
  53: public class Vector implements Serializable {
  54:     
  55:     /** The vector x. */
  56:     private double x;
  57:     
  58:     /** The vector y. */
  59:     private double y;
  60:     
  61:     /** 
  62:      * Creates a new instance of <code>Vector</code>.
  63:      *
  64:      * @param x  the x-component.
  65:      * @param y  the y-component.  
  66:      */
  67:     public Vector(double x, double y) {
  68:         this.x = x;
  69:         this.y = y;
  70:     }
  71:     
  72:     /**
  73:      * Returns the x-value.
  74:      * 
  75:      * @return The x-value.
  76:      */
  77:     public double getX() {
  78:         return this.x;
  79:     }
  80:     
  81:     /**
  82:      * Returns the y-value.
  83:      * 
  84:      * @return The y-value.
  85:      */
  86:     public double getY() {
  87:         return this.y;
  88:     }
  89:     
  90:     /**
  91:      * Returns the length of the vector.
  92:      * 
  93:      * @return The vector length.
  94:      */
  95:     public double getLength() {
  96:         return Math.sqrt((this.x * this.x) + (this.y * this.y));
  97:     }
  98:  
  99:     /**
 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: }