mako5.math
Class R2v

java.lang.Object
  extended by mako5.math.R2v
All Implemented Interfaces:
Cloneable

public final class R2v
extends Object
implements Cloneable

Represents a two-dimensional vector (R^2 vector).

Goals

The R2v class represents a vector in R^2 space. Applications include representing points, vectors, and texture coordinates. Float values store the coordinates providing floating point values while at a higher speed than doubles. Strict floating point is not used to increase float accuracy.

Risks and Payoffs

Using this class results in object overhead that must be deleted using the garbage collector. However, dot products and other vector calculations are already set and tested. Returning both x- and y- coordinates at the same time is also a plus.

Known problems

Using many of these as temporary objects will result in increased garbage collection. The quickest solution to this is to use either an array-based stack or queue to allow to O(1) insertion and removal of global scratch objects. The operation looks similar to this:

 //declared ahead of time
 Stack q = new ArrayStack(12);
 
 for (int i = 0; i < q.capacity(); q++)
        stack.push(new R2v());
 //...
 //in method
 R2v temp = stack.pop();
 temp.set(0, 0); //or other staring temp value
 //use temp
 stack.push(temp);
 temp = null; //not necessarily required
 
This keeps the number of objects constant and the temporary references used won't cause as severe a memory problem. The speed of the operations ensure that a signifant decrease in performance does not occur.

Version:
29-Sept-2005, 07-Apr 2006, 1-Jul 2006
Author:
Paul Jarrett

Constructor Summary
R2v()
          Creates a new R2v at (0.0f,0.0f).
R2v(float x_, float y_)
          Creates a new R2v.
R2v(R2v v)
          Creates a new R2v.
 
Method Summary
 void add(float x, float y)
          Adds x and y to this.
 void add(R2v v)
          Adds a vector to this.
 float angleWith(R2v v)
          Returns the angle of radians between this and another vector.
 Object clone()
          Clones this R2v.
 void div(float s)
          Divides this by a scalar.
 float dot(R2v v)
          Takes the dot product of this and another vector.
 float findDistanceTo(R2v v)
          Returns the distance to another point.
 Angle getDirection()
          Returns the angle that this makes with the positive x-axis.
 float getX()
          Returns the x coordinate.
 float getY()
          Returns the y-coordinate.
 void glTexCoord()
          Sets this as an OpenGL 2D texture coordinate.
 void glTranslate()
          Translates this in the x-y plane in openGL.
 void glVertex()
          Sets this as a 2D vertex.
 boolean hasHat()
          Returns true if this is a unit vector.
 float length()
          Returns the length of this vector.
 float lengthSquared()
          Returns the length squared of this vector.
 void multi(float s)
          Multiplies this by a scalar.
 void normalize()
          Normalizes this vector.
 void set(float x, float y)
          Sets this to the given x and y values.
 void set(R2v v)
          Sets this to another vector.
 void setLength(float f)
          Keeps the direction but sets a new length for this vector.
 void setX(float x)
          Sets the x value.
 void setY(float y)
          Sets the y-value.
 void sub(float x, float y)
          Subtracts from this.
 void sub(R2v v)
          Subtracts a vector from this.
 Polar toPolar()
          Converts this vector into a polar value.
 String toString()
          Converts this to a string
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

R2v

public R2v(float x_,
           float y_)
Creates a new R2v.

Parameters:
x_ - the x-coordinate
y_ - the y-coordinate

R2v

public R2v(R2v v)
Creates a new R2v.


R2v

public R2v()
Creates a new R2v at (0.0f,0.0f).

Method Detail

clone

public Object clone()
Clones this R2v.

Overrides:
clone in class Object

getX

public float getX()
Returns the x coordinate.


getY

public float getY()
Returns the y-coordinate.


set

public void set(R2v v)
Sets this to another vector.


set

public void set(float x,
                float y)
Sets this to the given x and y values.


setX

public void setX(float x)
Sets the x value.


setY

public void setY(float y)
Sets the y-value.


add

public void add(float x,
                float y)
Adds x and y to this.


add

public void add(R2v v)
Adds a vector to this.


sub

public void sub(float x,
                float y)
Subtracts from this.


sub

public void sub(R2v v)
Subtracts a vector from this.


multi

public void multi(float s)
Multiplies this by a scalar.


div

public void div(float s)
Divides this by a scalar.


normalize

public void normalize()
Normalizes this vector.


findDistanceTo

public float findDistanceTo(R2v v)
Returns the distance to another point.


angleWith

public float angleWith(R2v v)
Returns the angle of radians between this and another vector.
  Method:
  cos(theta) = this * v / (this.length() * v.length());
  acos( [this*v] / [length()*v.length()] );
 

Since:
29-Sept-2005

lengthSquared

public float lengthSquared()
Returns the length squared of this vector.


length

public float length()
Returns the length of this vector.


hasHat

public boolean hasHat()
Returns true if this is a unit vector.

Since:
29-Sept-2005

setLength

public void setLength(float f)
Keeps the direction but sets a new length for this vector.


dot

public float dot(R2v v)
Takes the dot product of this and another vector.


getDirection

public Angle getDirection()
Returns the angle that this makes with the positive x-axis.


toPolar

public Polar toPolar()
Converts this vector into a polar value.

Returns:
the polar equivalent of this vector
Since:
1-Jul 2006

toString

public String toString()
Converts this to a string

Overrides:
toString in class Object

glTexCoord

public void glTexCoord()
Sets this as an OpenGL 2D texture coordinate.


glTranslate

public void glTranslate()
Translates this in the x-y plane in openGL.


glVertex

public void glVertex()
Sets this as a 2D vertex.

Since:
05-Apr 2006