org.hermit.geometry
Class Vector

java.lang.Object
  extended by org.hermit.geometry.Vector
All Implemented Interfaces:
java.lang.Comparable<Vector>

public class Vector
extends java.lang.Object
implements java.lang.Comparable<Vector>

An immutable vector in the plane. This immutable class represents a vector as an X and Y offset.


Constructor Summary
Vector(double x, double y)
          Create a Vector from individual offsets.
 
Method Summary
static Vector add(Vector a, Vector b)
          Calculate the vector sum of two vectors.
 int compareTo(Vector o)
          Compares this object with the specified object for order.
 boolean equals(java.lang.Object obj)
          Indicates whether some other object is "equal to" this one.
 double getX()
          Get the X component of this vector.
 double getY()
          Get the Y component of this vector.
 int hashCode()
          Returns a hash code value for the object.
 double length()
          Get the length of this vector.
static Vector scale(Vector vec, double scale)
          Scale a vector by a given value.
static Vector sub(Vector a, Vector b)
          Calculate the vector difference of two vectors.
 java.lang.String toString()
          Convert this instance to a String suitable for display.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Vector

public Vector(double x,
              double y)
Create a Vector from individual offsets.

Parameters:
x - The X offset.
y - The Y offset.
Method Detail

getX

public double getX()
Get the X component of this vector.

Returns:
The X component of this vector.

getY

public double getY()
Get the Y component of this vector.

Returns:
The Y component of this vector.

length

public double length()
Get the length of this vector.

Returns:
The length of this vector.

add

public static Vector add(Vector a,
                         Vector b)
Calculate the vector sum of two vectors.

Parameters:
a - The first vector to add.
b - The second vector.
Returns:
The sum.

sub

public static Vector sub(Vector a,
                         Vector b)
Calculate the vector difference of two vectors.

Parameters:
a - The first vector to add.
b - The second vector.
Returns:
The difference, a - b.

scale

public static Vector scale(Vector vec,
                           double scale)
Scale a vector by a given value.

Parameters:
vec - The vector to scale.
scale - The value to scale by.
Returns:
The product of vec and scale.

equals

public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one. This method implements an equivalence relation on non-null object references.

This method simply compares the components of the two vectors, with a limited precision.

Note that the precision of the test is limited by the precision set in MathTools.setPrecision(double). That is, only as many fractional digits are compared as configured there; hence, two very close vectors will be considered equal.

Overrides:
equals in class java.lang.Object
Parameters:
obj - The reference object with which to compare.
Returns:
true if this object is the same as the obj argument, to within the given precision; false otherwise.

compareTo

public int compareTo(Vector o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

This method compares the components of the two vectors, with a limited precision. The Y component is given precedence; that is, the smaller Y will be considered less than the larger Y. If the Y values are equal to within the configured precision, then the X values are compared.

Note that the precision of the test is limited by the precision set in MathTools.setPrecision(double). That is, only as many fractional digits are compared as configured there; hence, two very close vectors will be considered equal.

Specified by:
compareTo in interface java.lang.Comparable<Vector>
Parameters:
o - The object to be compared to this one.
Returns:
A negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
java.lang.ClassCastException - The specified object's type prevents it from being compared to this object.

hashCode

public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hashtables.

The hash code returned here is based on the components of this Vector, and is designed to be different for different vectors. The least significant bits of the components are not compared, in line with the precision set in MathTools.setPrecision(double). Hence, this method should be consistent with equals() and compareTo().

Overrides:
hashCode in class java.lang.Object
Returns:
A hash code value for this object.

toString

public java.lang.String toString()
Convert this instance to a String suitable for display.

Overrides:
toString in class java.lang.Object
Returns:
String representation of this instance.