/** Class: Vector2D Description: A class for storing and manipulating 2D vectors Methods are included for adding and subtracting vectors, multiplying and dividing by a scalar, getting the magnitude, the direction, and collision detection (if it's a velocity). Created: 10/4/2006 Last Modified: 10/22/2007 By: Fred Green */ public class Vector2D { private float x, y; /** Default constructor: Initialized vector to 0. */ public Vector2D () { reset(); } /** Constructor: Initialize vector to x and y components. Example: Vector2D v = new Vector2D(3.2, -6.4); creates a vector with x-component 3.2 and y-component -6.4. */ public Vector2D (float x, float y) { this.x = x; this.y = y; } /** Constructor: Initialize this vector to copy of argument. */ public Vector2D (Vector2D toCopy) { this.x = toCopy.x; this.y = toCopy.y; } /** Method: reset() Sets this vector to 0. Example: Assuming v is a Vector2D object, v.reset(); sets all components of v to 0. */ public void reset () { x = 0; y = 0; } /** Method: magnitude() Returns: the magnitude of this vector. Example: Vector2D v = new Vector2D(2, 2); float a = v.magnitude(); will store the value sqrt(8) in a. */ public float magnitude () { return (float) Math.sqrt(x*x + y*y); } /** Method: getX() Returns: the x-component of this vector. Example: Vector2D v = new Vector2D(2, 3); float a = v.getX(); will store the value 2 in a. */ public float getX () { return x; } /** Method: getY() Returns: The y-component of this vector. Example: Vector2D v = new Vector2D(2, 3); float a = v.getY(); will store the value 3 in a. */ public float getY() { return y; } /** Method: setX() Sets the x-component of this vector to the argument. Example: Vector2D v = new Vector2D(2, 3); v.setX(7); float a = v.getX(); will store the value 7 in a. */ public void setX (float x) { this.x = x; } /** Method: setY() Sets the y-component of this vector to the argument. */ public void setY (float y) { this.y = y; } /** Method: plus() Returns: a Vector2D which is the sum of this vector with the argument. Example: Vector2D p = new Vector2D(3, 7); Vector2D v = new Vector2D(5, 2); Vector2D s = p.plus(v); stores a vector with components 8 and 9 in s. */ public Vector2D plus (Vector2D w) { Vector2D sum = new Vector2D(); sum.x = x + w.x; sum.y = y + w.y; return sum; } /** Method: minus() Returns: a Vector2D which is the difference of this vector with the argument. */ public Vector2D minus (Vector2D w) { Vector2D diff = new Vector2D(); diff.x = x - w.x; diff.y = y - w.y; return diff; } /** Method: times() Returns: a Vector 2D which is the product of this vector with the argument. Example: Vector2D v = new Vector2D(2, 3); Vector2D w = v.times(6); will store a vector with components 12 and 18 in w. */ public Vector2D times (float n) { Vector2D product = new Vector2D(); product.x = x*n; product.y = y*n; return product; } /** Method: multiplyBy() Sets this vector to its product with the argument. Example: Vector2D v = new Vector2D(2, 3); v.multiplyBy(6); will put the values 12 and 18 in v. */ public void multiplyBy (float n) { x = x*n; y = y*n; } /** Method: dividedBy() Returns: a Vector2D which is this vector divided by the argument. Example: Vector2D v = new Vector2D(8, 10); Vector2D w = v.dividedBy(2); will store a vector with components 4 and 5 in w. */ public Vector2D dividedBy (float n) { Vector2D quotient = new Vector2D(); quotient.x = x/n; quotient.y = y/n; return quotient; } /** Method: divideBy() Sets this vector to its quotient with the argument. (Similar to multiplyBy(), but it divides instead of multiplies.) */ public void divideBy (float n) { x = x/n; y = y/n; } /** Method: dotProduct() Returns: the dot product of this vector with the argument. */ public float dotProduct (Vector2D w) { return x*w.x + y*w.y; } /** Method: direction() Returns: a unit Vector2D pointing in the same direction as this one. */ public Vector2D direction () { Vector2D quotient = new Vector2D (this); return quotient.dividedBy(magnitude()); } /** Method: constrain() Contrains the x and y components to be within the limits set by the arguments. */ public void constrain (float minX, float maxX, float minY, float maxY) { if (x <= minX) { x = minX; } if (x >= maxX) { x = maxX; } if (y <= minY) { y = minY; } if (y >= maxY) { y = maxY; } } /** Method: detectCollision() Detects a collision with the sides of the screen (or box) with dimensions width X height. The position, width, and height are also required arguments. This will only work if this vector is to be interpreted as a velocity, and if the collision is with horizontal or vertical edges. */ public void detectCollision (Vector2D position, float objectWidth, float objectHeight, float width, float height) { if (position.getX() + objectWidth/2 >= width || position.getX() <= objectWidth/2) x *= -1; if (position.getY() + objectHeight/2 >= height || position.getY() <= objectHeight/2) y *= -1; } /** Method: toString() Returns: a string representation of the vector. */ public String toString() { return "(" + x + "," + y + ")"; } }