public final class Vector2 extends Object
Constructor and Description |
---|
Vector2()
Creates the vector
(0.0, 0.0) with name equal to the empty
string. |
Vector2(double x,
double y)
Creates the vector
(x, y) . |
Vector2(Vector2 other)
Creates a vector with the same components as another vector.
|
Modifier and Type | Method and Description |
---|---|
Vector2 |
add(Vector2 other)
Add a vector to this vector changing the components of this vector.
|
static Vector2 |
add(Vector2 a,
Vector2 b)
Returns a new
Vector2D equal to a + b . |
static Vector2 |
dirVector(double theta)
Returns the vector having magnitude one pointing in the direction
theta degrees from the x axis. |
boolean |
equals(Object obj)
Compares this vector with the given object.
|
double |
getX()
Returns the x component of the vector.
|
double |
getY()
Returns the y component of the vector.
|
int |
hashCode()
Returns a hash code for this vector.
|
double |
mag()
Returns the magnitude of this vector.
|
Vector2 |
multiply(double s)
Multiply this vector by a scalar amount changing the components of this
vector.
|
static Vector2 |
multiply(double s,
Vector2 a)
Returns a new
Vector2D equal to s * a . |
void |
set(double x,
double y)
Sets the x and y component of the vector.
|
void |
setX(double x)
Sets the x component of the vector.
|
void |
setY(double y)
Sets the y component of the vector.
|
boolean |
similarTo(Vector2 other,
double tol)
Determines if two vectors are almost equal (similar).
|
Vector2 |
subtract(Vector2 other)
Subtract a vector from this vector changing the components of this
vector.
|
static Vector2 |
subtract(Vector2 a,
Vector2 b)
Returns a new
Vector2D equal to a - b . |
String |
toString()
Returns a string representation of the vector.
|
public Vector2()
(0.0, 0.0)
with name equal to the empty
string.public Vector2(double x, double y)
(x, y)
.x
- the x-component of the vectory
- the y-component of the vectorpublic Vector2(Vector2 other)
other
- a vector to copy the components frompublic double getX()
public void setX(double x)
x
- the new value of the x component.public double getY()
public void setY(double y)
y
- the new value of the y component.public void set(double x, double y)
x
- the new value of the x component.y
- the new value of the y component.public Vector2 add(Vector2 other)
Mathematically, if this vector is a
and the other vector is
b
then invoking this method is equivalent to computing
a + b
and assigning the value back to a
.
other
- the vector to add to this vector.Vector2D
objectpublic Vector2 subtract(Vector2 other)
Mathematically, if this vector is a
and the other vector is
b
then invoking this method is equivalent to computing
a - b
and assigning the value back to a
.
other
- the vector to subtract this vector.Vector2D
objectpublic Vector2 multiply(double s)
Mathematically, if this vector is a
and the scalor is
s
then invoking this method is equivalent to computing
s * a
and assigning the value back to a
.
s
- the scalar value to multiply the vector byVector2D
objectpublic double mag()
public static Vector2 add(Vector2 a, Vector2 b)
Vector2D
equal to a + b
.a
- a vectorb
- another vectorVector2D
equal to a + b
public static Vector2 subtract(Vector2 a, Vector2 b)
Vector2D
equal to a - b
.a
- a vectorb
- another vectorVector2D
equal to a - b
public static Vector2 multiply(double s, Vector2 a)
Vector2D
equal to s * a
.s
- a scalara
- a vectorVector2D
equal to s * a
public static Vector2 dirVector(double theta)
theta
degrees from the x axis.
The components of the vector are equal to
(Math.cos(rad), Math.sin(rad))
where rad
is
theta
expressed in radians.
theta
- the direction that the vector is pointing in measured in
degrees from the x axispublic int hashCode()
public boolean equals(Object obj)
true
if and only if the argument is not null
and is a Vector2D
object having the same components as this
object.public String toString()
public boolean similarTo(Vector2 other, double tol)
other
- the other vector to comparetol
- the threshold length of the vector difference
(this - other)
true
if the length of (this - other)
is
less than tol
, and false
otherwise