package rebelsky.vec; /** * A vector in two space, implemented by (x,y) pair. * * @author CSC152 2004F * @version 0.5 of September 2004 */ public class Point extends Vec2D { // +--------+------------------------------------------------------- // | Fields | // +--------+ /** The x coordinate. */ double x; /** The y coordinate. */ double y; // +--------------+------------------------------------------------- // | Constructors | // +--------------+ /** * Construct the point (_x,_y). */ public Point(double _x, double _y) { this.x = _x; this.y = _y; } // Point(double, double) // +-----------+---------------------------------------------------- // | Observers | // +-----------+ /** * Get the X coordinate of this vector. */ public double getX() { return this.x; } // getX() /** * Get the Y coordinate of this vector. */ public double getY() { return this.y; } // getY() /** * Get the angle of this vector from the origin. */ public double getAngle() { // Special case: 0 for (0.0,0.0) if (Math.abs(this.x) + Math.abs(this.y) < 0.00001) return 0.0; return Math.asin(this.y/this.getRadius()); } // getAngle() /** * Get the radius of this vector. */ public double getRadius() { return Math.sqrt(this.x*this.x + this.y*this.y); } // getRadius() /** * Rotate this vector for some amount around the origin. */ public Vec2D rotate(double angle) { // New x: xcosangle - ysinangle, xsinangle + ycosangle return new Point(this.x*Math.cos(angle) - this.y*Math.sin(angle), this.x*Math.sin(angle) + this.y*Math.cos(angle)); } // rotate(double) /** * Increase the length of this vector by a factor of factor. */ public Vec2D stretch(double factor) { return new Point(factor*this.x, factor*this.y); } // stretch(double) /** * Compute the unit vector for this vector (radius 1, same * angle). */ public Vec2D unitVector() { double theta = this.getAngle(); return new Point(Math.cos(theta), Math.sin(theta)); } // stretch(double) /** * Convert this vector to a string. */ public String toString() { return "(x:" + this.x + ",y:" + this.y + ")"; } // toString() } // interface Vec2D