cse1030.math

## Class Vector2D

• ```public final class Vector2D
extends Object```
A class that represents two dimensional vectors. The class provides some basic mathematical operations such as addition and subtraction.
Author:
CSE1030_W13_14
• ### Constructor Summary

Constructors
Constructor and Description
`Vector2D()`
Creates the vector `(0.0, 0.0)` with name equal to the empty string.
```Vector2D(double x, double y)```
Creates the vector `(x, y)` with name equal to the empty string.
```Vector2D(double x, double y, String name)```
Creates the vector `(x, y)` with the given name.
`Vector2D(Vector2D other)`
Creates a vector with the same coordinates and name as another vector.
• ### Method Summary

Methods
Modifier and Type Method and Description
`Vector2D` `add(Vector2D other)`
Add a vector to this vector changing the coordinates of this vector.
`static Vector2D` ```add(Vector2D a, Vector2D b)```
Returns a new `Vector2D` equal to `a + b`.
`static Vector2D` `dirVector(double degrees)`
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.
`String` `getName()`
Returns the name of the vector.
`double` `getX()`
Returns the x coordinate of the vector.
`double` `getY()`
Returns the y coordinate of the vector.
`int` `hashCode()`
Returns a hash code for this vector.
`double` `mag()`
Returns the magnitude of this vector.
`Vector2D` `multiply(double s)`
Multiply this vector by a scalar amount changing the coordinates of this vector.
`static Vector2D` ```multiply(double s, Vector2D a)```
Returns a new `Vector2D` equal to `s * a`.
`void` `setName(String name)`
Sets the name of the vector.
`void` `setX(double x)`
Sets the x coordinate of the vector.
`void` `setY(double y)`
Sets the y coordinate of the vector.
`boolean` ```similarTo(Vector2D other, double tol)```
Determines if two vectors are almost equal (similar).
`Vector2D` `subtract(Vector2D other)`
Subtract a vector from this vector changing the coordinates of this vector.
`static Vector2D` ```subtract(Vector2D a, Vector2D b)```
Returns a new `Vector2D` equal to `a - b`.
`String` `toString()`
Returns a string representation of the vector.
• ### Methods inherited from class java.lang.Object

`getClass, notify, notifyAll, wait, wait, wait`
• ### Constructor Detail

• #### Vector2D

`public Vector2D()`
Creates the vector `(0.0, 0.0)` with name equal to the empty string.
• #### Vector2D

```public Vector2D(double x,
double y)```
Creates the vector `(x, y)` with name equal to the empty string.
• #### Vector2D

`public Vector2D(Vector2D other)`
Creates a vector with the same coordinates and name as another vector.
• #### Vector2D

```public Vector2D(double x,
double y,
String name)```
Creates the vector `(x, y)` with the given name. If `name == null` a `NullPointerException` is thrown.
Throws:
`NullPointerException` - if `name == null`
• ### Method Detail

• #### getX

`public double getX()`
Returns the x coordinate of the vector.
Returns:
the x coordinate of the vector.
• #### setX

`public void setX(double x)`
Sets the x coordinate of the vector.
Parameters:
`x` - the new value of the x coordinate.
• #### getY

`public double getY()`
Returns the y coordinate of the vector.
Returns:
the y coordinate of the vector.
• #### setY

`public void setY(double y)`
Sets the y coordinate of the vector.
Parameters:
`y` - the new value of the y coordinate.
• #### getName

`public String getName()`
Returns the name of the vector.
Returns:
the name of the vector.
• #### setName

`public void setName(String name)`
Sets the name of the vector. If `name == null` a `NullPointerException` is thrown.
Throws:
`NullPointerException` - if `name == null`
Parameters:
`name` - the new name of the vector.

`public Vector2D add(Vector2D other)`
Add a vector to this vector changing the coordinates of this vector.

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`.

Parameters:
`other` - the vector to add to this vector.
Returns:
this `Vector2D` object
• #### subtract

`public Vector2D subtract(Vector2D other)`
Subtract a vector from this vector changing the coordinates of this vector.

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`.

Parameters:
`other` - the vector to subtract this vector.
Returns:
this `Vector2D` object
• #### multiply

`public Vector2D multiply(double s)`
Multiply this vector by a scalar amount changing the coordinates of this vector.

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`.

Parameters:
`s` - the scalar value to multiply the vector by
Returns:
this `Vector2D` object
• #### mag

`public double mag()`
Returns the magnitude of this vector.
Returns:
the magnitude of this vector.

```public static Vector2D add(Vector2D a,
Vector2D b)```
Returns a new `Vector2D` equal to `a + b`.
Parameters:
`a` - a vector
`b` - another vector
Returns:
a new `Vector2D` equal to `a + b`
• #### subtract

```public static Vector2D subtract(Vector2D a,
Vector2D b)```
Returns a new `Vector2D` equal to `a - b`.
Parameters:
`a` - a vector
`b` - another vector
Returns:
a new `Vector2D` equal to `a - b`
• #### multiply

```public static Vector2D multiply(double s,
Vector2D a)```
Returns a new `Vector2D` equal to `s * a`.
Parameters:
`s` - a scalar
`a` - a vector
Returns:
a new `Vector2D` equal to `s * a`
• #### dirVector

`public static Vector2D dirVector(double degrees)`
Returns the vector having magnitude one pointing in the direction `theta` degrees from the x axis.
Parameters:
`degrees` - the direction that the vector is pointing in measured in degrees from the x axis
Returns:
the unit vector pointing in the given direction
• #### hashCode

`public int hashCode()`
Returns a hash code for this vector.
Overrides:
`hashCode` in class `Object`
Returns:
a hash code for this vector
• #### equals

`public boolean equals(Object obj)`
Compares this vector with the given object. The result is `true` if and only if the argument is not `null` and is a `Vector2D` object having the same coordinates as this object.
Overrides:
`equals` in class `Object`
Parameters:
`obj` - the object to compare this vector against
Returns:
`true` if the given object represents a `Vector2D` equivalent to this point, `false` otherwise
• #### toString

`public String toString()`
Returns a string representation of the vector. The string is the name of the vector, followed by the comma separated coordinates of the vector inside parentheses.
Overrides:
`toString` in class `Object`
Returns:
a string representation of the vector
• #### similarTo

```public boolean similarTo(Vector2D other,
double tol)```
Determines if two vectors are almost equal (similar). Two vectors are similar if the magnitude of their vector difference is smaller than the specified tolerance.
Parameters:
`other` - the other vector to compare
`tol` - the threshold length of the vector difference `(this - other)`
Returns:
`true` if the length of `(this - other)` is less than `tol`, and `false` otherwise