|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmako5.math.R2v
public final class R2v
Represents a two-dimensional vector (R^2 vector).
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.
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.
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 requiredThis 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.
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 |
---|
public R2v(float x_, float y_)
x_
- the x-coordinatey_
- the y-coordinatepublic R2v(R2v v)
public R2v()
Method Detail |
---|
public Object clone()
clone
in class Object
public float getX()
public float getY()
public void set(R2v v)
public void set(float x, float y)
public void setX(float x)
public void setY(float y)
public void add(float x, float y)
public void add(R2v v)
public void sub(float x, float y)
public void sub(R2v v)
public void multi(float s)
public void div(float s)
public void normalize()
public float findDistanceTo(R2v v)
public float angleWith(R2v v)
Method: cos(theta) = this * v / (this.length() * v.length()); acos( [this*v] / [length()*v.length()] );
public float lengthSquared()
public float length()
public boolean hasHat()
public void setLength(float f)
public float dot(R2v v)
public Angle getDirection()
public Polar toPolar()
public String toString()
toString
in class Object
public void glTexCoord()
public void glTranslate()
public void glVertex()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |