V
vtcompsci
/**
* Vector2d.java
* Represents some operations on 2-D vectors for geometry and basic
* physics computations.
*
*/
class Vector2d {
private float x = 0, y = 0;
/**
* Default Constructor.
*
*/
public Vector2d() { x = 0; y = 0; }
/**
* Parameterized Constructor. Sets the member variables to
* the values passed in.
* @param xv The desired value of x
* @param yv The desired value of y
*/
public Vector2d(float xv, float yv) { x = xv; y = yv; }
public Vector2d(double xv, double yv) { x = (float)xv; y = (float)yv;
}
// Accessor/Settor functions.
public void X(float xv) { x = xv; }
public void Y(float yv) { y = yv; }
public float X() { return x; }
public float Y() { return x; }
/**
* Add two vectors
* @param rhs the right hand side vector to be added
* @return a new vector, the sum of this + rhs.
*/
public Vector2d add(Vector2d rhs) {
return new Vector2d(x + rhs.x, y + rhs.y);
}
/**
* Subtract two vectors.
* @param rhs the right hand side vector
* @return a new vector, this - rhs
*/
public Vector2d sub(Vector2d rhs) {
return new Vector2d(x - rhs.x, y - rhs.y);
}
/**
* Negate a vector
* @return A new vector equal to -this
*/
public Vector2d neg() {
return new Vector2d(-x, -y);
}
/**
* Multiply a vector by a constant.
* @param d the constant by which the vector will be scaled.
* @return a new vector equal to d*this
*/
public Vector2d scale(float d) {
return new Vector2d(x * d, y * d);
}
/**
* Determine the magnitude of a vector.
* @return Return the magnitude of this
*/
public float mag() {
return (float) Math.sqrt(x*x + y*y);
}
/**
* Equality test for vectors.
* @param rhs The vector to compare to
* @return true if the vectors have the same components, false
othewise.
*/
public boolean equals(Vector2d rhs) {
return (x == rhs.x) && (y == rhs.y);
}
}
* Vector2d.java
* Represents some operations on 2-D vectors for geometry and basic
* physics computations.
*
*/
class Vector2d {
private float x = 0, y = 0;
/**
* Default Constructor.
*
*/
public Vector2d() { x = 0; y = 0; }
/**
* Parameterized Constructor. Sets the member variables to
* the values passed in.
* @param xv The desired value of x
* @param yv The desired value of y
*/
public Vector2d(float xv, float yv) { x = xv; y = yv; }
public Vector2d(double xv, double yv) { x = (float)xv; y = (float)yv;
}
// Accessor/Settor functions.
public void X(float xv) { x = xv; }
public void Y(float yv) { y = yv; }
public float X() { return x; }
public float Y() { return x; }
/**
* Add two vectors
* @param rhs the right hand side vector to be added
* @return a new vector, the sum of this + rhs.
*/
public Vector2d add(Vector2d rhs) {
return new Vector2d(x + rhs.x, y + rhs.y);
}
/**
* Subtract two vectors.
* @param rhs the right hand side vector
* @return a new vector, this - rhs
*/
public Vector2d sub(Vector2d rhs) {
return new Vector2d(x - rhs.x, y - rhs.y);
}
/**
* Negate a vector
* @return A new vector equal to -this
*/
public Vector2d neg() {
return new Vector2d(-x, -y);
}
/**
* Multiply a vector by a constant.
* @param d the constant by which the vector will be scaled.
* @return a new vector equal to d*this
*/
public Vector2d scale(float d) {
return new Vector2d(x * d, y * d);
}
/**
* Determine the magnitude of a vector.
* @return Return the magnitude of this
*/
public float mag() {
return (float) Math.sqrt(x*x + y*y);
}
/**
* Equality test for vectors.
* @param rhs The vector to compare to
* @return true if the vectors have the same components, false
othewise.
*/
public boolean equals(Vector2d rhs) {
return (x == rhs.x) && (y == rhs.y);
}
}