com.partnersoft.geometry.xy
Class XyVector

java.lang.Object
  extended by com.partnersoft.geometry.xy.XyVector
All Implemented Interfaces:
Immutable

public class XyVector
extends java.lang.Object
implements Immutable

A mathematical vector in (x, y) space. Vectors represent displacements and can be used in lieu of slopes in parameterized line and curve drawing. Oh, and to do vector algebra.

Everyone loves vector algebra.

Copyright 2007 Partner Software, Inc.

Version:
$Id: XyVector.java 2328 2010-01-06 15:38:22Z paul $
Author:
Paul Reavis, Richard Fay, Rich Stepanski

Constructor Summary
XyVector(double deltaX, double deltaY)
          Creates a vector with the given deltaX and deltaY.
XyVector(double radius, XyDirection direction)
          Creates a vector equivalent for the given polar coordinates.
XyVector(XyPoint point)
          Creates a vector equivalent for the given point.
XyVector(XyPoint start, XyPoint end)
          Creates a vector difference between the two points.
 
Method Summary
 double dot(XyVector nother)
          Returns the dot-product of this vector with the one given.
 boolean equals(java.lang.Object nother)
           
 double getDeltaX()
          Offset on the X axis.
 double getDeltaY()
          Offset on the Y axis.
 XyDirection getDirection()
          Returns the direction this vector points.
 XyVector getLeftPerpendicular()
          Returns the vector with the same length that is perpendicular to this one and lies on the "left" side (visualized as if you are facing the direction this vector points).
 double getLength()
          Returns the length, or magnitude, of this vector.
 double getLengthSquared()
          Returns the square of the length, or magnitude, of this vector.
 XyVector getReversedVector()
          Returns the reversed, or opposite vector, to this one.
 XyVector getRightPerpendicular()
          Returns the vector with the same length that is perpendicular to this one and lies on the "right" side (visualized as if you are facing the direction this vector points).
 double getThetaRadians()
          Returns the direction of the vector in theta radians.
 int hashCode()
           
 XyVector minus(XyVector nother)
          Subtracts the given vector from this one and returns the resultant.
 XyVector plus(XyVector nother)
          Adds this vector to the given vector and returns the resultant vector.
 XyVector reversed()
          Returns a modified version of this vector with that goes in the opposite direction.
 XyVector times(double factor)
          Multiplies this vector by the given factor and returns the result.
 XyLine toLine()
          Returns the line containing this vector.
 XyLineSegment toLineSegment()
          Returns the line segment equivalent of this vector.
 XyPoint toPoint()
          Returns the (x, y) point that this is a positional vector for.
 XyPolar toPolar()
          Returns a polar coordinate representation of this vector.
 java.lang.String toString()
           
 XyVector toUnitVector()
          Returns an equivalent vector to this whose length is 1.0.
 XyVector withDeltaX(double newDeltaX)
          Returns a modified version of this vector with the given deltaX.
 XyVector withDeltaY(double newDeltaY)
          Returns a modified version of this vector with the given deltaY.
 XyVector withDirection(XyDirection newDirection)
          Returns a modified version of this vector with the given direction.
 XyVector withLength(double newLength)
          Returns a modified version of this vector with the given length.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XyVector

public XyVector(double deltaX,
                double deltaY)
Creates a vector with the given deltaX and deltaY.


XyVector

public XyVector(XyPoint point)
Creates a vector equivalent for the given point. The points x and y values are used as the deltaX and deltaY for the vector.

Parameters:
point - input point

XyVector

public XyVector(XyPoint start,
                XyPoint end)
Creates a vector difference between the two points.

Parameters:
start - starting or origin point
end - ending or destination point

XyVector

public XyVector(double radius,
                XyDirection direction)
Creates a vector equivalent for the given polar coordinates.

Method Detail

withDeltaX

public XyVector withDeltaX(double newDeltaX)
Returns a modified version of this vector with the given deltaX.


withDeltaY

public XyVector withDeltaY(double newDeltaY)
Returns a modified version of this vector with the given deltaY.


withLength

public XyVector withLength(double newLength)
Returns a modified version of this vector with the given length.

Parameters:
newLength -
Returns:

withDirection

public XyVector withDirection(XyDirection newDirection)
Returns a modified version of this vector with the given direction.


reversed

public XyVector reversed()
Returns a modified version of this vector with that goes in the opposite direction.


toPoint

public XyPoint toPoint()
Returns the (x, y) point that this is a positional vector for.

Returns:
point equivalent of this vector

toPolar

public XyPolar toPolar()
Returns a polar coordinate representation of this vector.

Returns:
polar equivalent to this vector

toLine

public XyLine toLine()
Returns the line containing this vector. This is as if the vector were a segment with starting point at (0, 0) and ending point at (deltaX, deltaY).

Returns:
line containing this vector

toLineSegment

public XyLineSegment toLineSegment()
Returns the line segment equivalent of this vector. This is as if the vector were a segment with starting point at (0, 0) and ending point at (deltaX, deltaY).

Returns:
line segment equivalent

toUnitVector

public XyVector toUnitVector()
Returns an equivalent vector to this whose length is 1.0.

Returns:
unit vector for this vector

plus

public XyVector plus(XyVector nother)
Adds this vector to the given vector and returns the resultant vector.

Parameters:
nother - vector to add
Returns:
resultant vector

minus

public XyVector minus(XyVector nother)
Subtracts the given vector from this one and returns the resultant.

Parameters:
nother - vector to subtract
Returns:
resultant vector

times

public XyVector times(double factor)
Multiplies this vector by the given factor and returns the result.

Parameters:
factor - multiplication factor
Returns:
resultant vector

dot

public double dot(XyVector nother)
Returns the dot-product of this vector with the one given.

Returns:
dot product of this and nother vector

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object nother)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

getDeltaX

public double getDeltaX()
Offset on the X axis. If you visualize the vector as a line segment with one end at the origin (0, 0), this is the X coordinate of the other end.


getDeltaY

public double getDeltaY()
Offset on the Y axis. If you visualize the vector as a line segment with one end at the origin (0, 0), this is the Y coordinate of the other end.


getDirection

public XyDirection getDirection()
Returns the direction this vector points.

Returns:
direction of this vector

getLength

public double getLength()
Returns the length, or magnitude, of this vector.

Returns:
vector length

getLengthSquared

public double getLengthSquared()
Returns the square of the length, or magnitude, of this vector. You can use this when doing a lot of comparisons to avoid the square-root required by the regular length function.

Returns:
square of vector length

getReversedVector

public XyVector getReversedVector()
Returns the reversed, or opposite vector, to this one. Visualized with the start of the vector at the origin, this would be the vector at 180 degrees rotation, heading in exactly the opposite direction such that both vectors lie on the same line.


getLeftPerpendicular

public XyVector getLeftPerpendicular()
Returns the vector with the same length that is perpendicular to this one and lies on the "left" side (visualized as if you are facing the direction this vector points).


getRightPerpendicular

public XyVector getRightPerpendicular()
Returns the vector with the same length that is perpendicular to this one and lies on the "right" side (visualized as if you are facing the direction this vector points).


getThetaRadians

public double getThetaRadians()
Returns the direction of the vector in theta radians. Equivalent to getDirection().getThetaRadians(), but faster.