package rebelsky.twodim; import java.io.PrintWriter; /** * A vector in two space. * * @author AUTHOR * @version VERSION */ public class TwoDimVector { // +--------+------------------------------------------------------- // | Fields | // +--------+ double xcoord; double ycoord; // +--------------+------------------------------------------------- // | Constructors | // +--------------+ public TwoDimVector(double x, double y) { this.xcoord = x; this.ycoord = y; } // TwoDimVector(double,double) // +----------+----------------------------------------------------- // | Mutators | // +----------+ // +-----------+---------------------------------------------------- // | Observers | // +-----------+ public double dotProduct(TwoDimVector other) { return ((this.xcoord*other.xcoord) + (this.ycoord*other.ycoord)); } // dotProduct public double getX() { return this.xcoord; } // getX() public double getY() { return this.ycoord; } // getY() public double getAngle() { return Math.asin(this.ycoord/this.getDistanceFromOrigin()); // return Math.atan(this.ycoord/this.xcoord); } // getAngle() public double getDistanceFromOrigin() { return Math.sqrt(this.xcoord*this.xcoord + this.ycoord*this.ycoord); } // getDistanceFromOrigin() public boolean isLessThan(TwoDimVector other) { return this.getDistanceFromOrigin() < other.getDistanceFromOrigin(); } // isLessThan(TwoDimVector) /** * Return a new vector that is a scaled version of the current vector. */ public TwoDimVector stretch(double factor) { return new TwoDimVector(this.xcoord*factor, this.ycoord*factor); // Multiply the x coordinate by factor // Multiply the y coordinate by factor // Shove the two things into a vector and return it } // stretch(double) public TwoDimVector unitVector() { double radius = this.getDistanceFromOrigin(); TwoDimVector result = new TwoDimVector(this.xcoord/radius, this.ycoord/radius); return result; } // unitVector() public String toString() { return "(" + Double.toString(this.xcoord) + "," + Double.toString(this.ycoord) + ")"; } // toString() public void printYourself(PrintWriter pen) { pen.println("xcoordinate: " + this.getX()); pen.println("ycoordinate: " + this.getY()); pen.println("theta: " + this.getAngle()); pen.println("radius: " + this.getDistanceFromOrigin()); } } // class TwoDimVector