EDU.gatech.cc.is.util
Class Vec2

java.lang.Object
  |
  +--EDU.gatech.cc.is.util.Vec2

public class Vec2
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

A class for manipulating 2d vectors. Both polar and cartesian components are always available. The fields x, y, t (theta) and r are directly available for reading, but you should never set them directly. Use setx, sety, sett and setr instead. This (non OO) approach was chosen deliberately for speed reasons; no whining.

+x is right, +y is up. t is in radians with t=0 in the +x direction and t = PI in the -r direction, increasing CCW.

NOTE: Vec2's can have direction without magnitude, i.e. theta has meaning even if x, y and r == 0.

Copyright (c)2000 Tucker Balch

See Also:
Serialized Form

Field Summary
static double PI
           
static double PI2
           
 double r
          The r component of the polar view of the vector; never set directly, use setr instead.
 double t
          The theta component of the polar view of the vector; never set directly, use sett instead.
 double x
          The x component of the cartesian view of the vector; never set directly, use setx instead.
 double y
          The y component of the cartesian view of the vector; never set directly, use sety instead.
 
Constructor Summary
Vec2()
          Create a new vector, (1,0).
Vec2(double x0, double y0)
          Create a new vector, (x0,y0).
Vec2(Vec2 v)
          Create a new vector by copying the input parameter.
 
Method Summary
 void add(Vec2 other)
          Add another vector to self, this = this + other
 java.lang.Object clone()
          Create a new vector by cloning.
static void main(java.lang.String[] args)
          A test routine.
 void normalize(double newr)
          Same as setr.
 int octant()
          Provides info on which octant (0-7) the vector lies in.
 int quadrant()
          Provides info on which quadrant (0-3) the vector lies in.
 void rotate(double rot)
          Rotate the vector.
 void setr(double newr)
          Set r.
 void sett(double newt)
          Set t.
 void setx(double newx)
          Set the x component.
 void sety(double newy)
          Set the y component.
 void sub(Vec2 other)
          Subtract other vector from self, this = this - other
 java.lang.String toString()
          Generate a string value for the vector.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

x

public double x
The x component of the cartesian view of the vector; never set directly, use setx instead.
See Also:
setx(double)

y

public double y
The y component of the cartesian view of the vector; never set directly, use sety instead.
See Also:
sety(double)

t

public double t
The theta component of the polar view of the vector; never set directly, use sett instead.
See Also:
sett(double)

r

public double r
The r component of the polar view of the vector; never set directly, use setr instead.
See Also:
setr(double)

PI2

public static final double PI2

PI

public static final double PI
Constructor Detail

Vec2

public Vec2()
Create a new vector, (1,0).

Vec2

public Vec2(double x0,
            double y0)
Create a new vector, (x0,y0).
Parameters:
x0 - the x component.
y0 - the y component.

Vec2

public Vec2(Vec2 v)
Create a new vector by copying the input parameter.
Parameters:
v - Vec2, the vector to copy.
Method Detail

clone

public java.lang.Object clone()
Create a new vector by cloning.
Overrides:
clone in class java.lang.Object
Parameters:
v - Vec2, the vector to copy.

setx

public void setx(double newx)
Set the x component. t and r are reset as well.
Parameters:
newx - the x component.

sety

public void sety(double newy)
Set the y component. t and r are reset as well.
Parameters:
newy - the x component.

sett

public void sett(double newt)
Set t. x and y are reset as well.
Parameters:
newt - the t component.

setr

public void setr(double newr)
Set r. x, y and t may be reset as well.
Parameters:
newr - the r component.

rotate

public void rotate(double rot)
Rotate the vector. x, y and t are reset.
Parameters:
rot - how many radians to rotate, CCW is positive.

sub

public void sub(Vec2 other)
Subtract other vector from self, this = this - other
Parameters:
other - the vector to subtract.

normalize

public void normalize(double newr)
Same as setr. Kept for comatibility.
Parameters:
newr - the r component.

add

public void add(Vec2 other)
Add another vector to self, this = this + other
Parameters:
other - the vector to add.

octant

public int octant()
Provides info on which octant (0-7) the vector lies in. 0 indicates 0 radians +- PI/8 1-7 continue CCW.
Returns:
0 - 7, depending on which direction the vector is pointing.

quadrant

public int quadrant()
Provides info on which quadrant (0-3) the vector lies in. 0 indicates 0 radians +- PI/4 1-3 continue CCW.
Returns:
0 - 3, depending on which direction the vector is pointing.

toString

public java.lang.String toString()
Generate a string value for the vector.
Overrides:
toString in class java.lang.Object
Returns:
"(x,y) (r,t)"

main

public static void main(java.lang.String[] args)
A test routine.