package rebelsky.twodim; /** * A vector in two space. * * @author AUTHOR * @version VERSION */ public class TwoDimVector1 { // +--------+------------------------------------------------------- // | Fields | // +--------+ double xcoord; double ycoord; // +--------------+------------------------------------------------- // | Constructors | // +--------------+ public TwoDimVector1(double x, double y) { this.xcoord = x; this.ycoord = y; } // TwoDimVector1(double,double) public TwoDimVector1() { this.xcoord = 0.0; this.ycoord = 0.0; } // TwoDimVector1() // +----------+----------------------------------------------------- // | Mutators | // +----------+ public void setX(double newX) { this.xcoord = newX; } // setX() public void setY(double newY) { this.ycoord = newY; } // setY() // +-----------+---------------------------------------------------- // | Observers | // +-----------+ public double dotProduct(TwoDimVector1 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 getDistanceFromOrigin() { return Math.sqrt(this.xcoord*this.xcoord + this.ycoord*this.ycoord); } // getDistanceFromOrigin() /** * Return a new vector that is a scaled version of the current vector. */ public TwoDimVector1 stretch(double factor) { TwoDimVector1 result = new TwoDimVector1(); result.setX(this.xcoord * factor); result.setY(this.ycoord * factor); return result; // Multiply the x coordinate by factor // Multiply the y coordinate by factor // Shove the two things into a vector and return it } // stretch(double) } // class TwoDimVector1