/**
* 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);
}
}
Monique Y. Mudama - 02 Feb 2006 04:58 GMT
Is there a question carefully hidden somewhere in all that code?
I will say that I like all the javadoc comments.
> /**
> * Vector2d.java
[quoted text clipped - 80 lines]
> }
> }

Signature
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Chris Uppal - 02 Feb 2006 10:44 GMT
> class Vector2d {
> private float x = 0, y = 0;
> [... etc ...]
There are several more-or-less questionable things about this code, but you
don't say what your actual problem is.
-- chris
opalpa@gmail.com opalinski from opalpaweb - 02 Feb 2006 15:12 GMT
> public float Y() { return x; }
Cheers.
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb - 02 Feb 2006 15:29 GMT
Like Chris said, there are a number of other questionable things about
your code:
# equals is not the equals that is expected. You use an uncommon
signature.
# you have equals but no hashCode
# the constructor that takes doubles is ... well doubles don't fit
into floats all the time
# you have settors but otherwise you take care with add, sub, neg,
and scale to make class immutable. Get rid of settors and make class
immutable.
# IMO your comments are poor. They do not introduce any information.
Yeah, it's a default constructor, how does it leave Vector2d? Yeah,
add adds vectors, what does that mean? Yeah, scale scales, what does
that mean? etc. Sometimes for scientific code I will give a
reference to a popular text in one spot like "The vector opertions in
code below are defined as in "University Physics" by Freedman and
Young, 9th Edition, pages 11 through 22".
Opalinski
opalpa@gmail.com
http://www.geocities.com/opalpaweb/