package rebelsky.vec; /** * A vector in two space, implemented using Polar coordinates. * * @author CSC152 2004F * @version 1.0 */ public class Polar extends Vec2D { // +--------+------------------------------------------------------- // | Fields | // +--------+ /** * The angle between the vector and the y=0 line. */ double theta; /** * The distance from the point to the origin. */ double radius; // +--------------+------------------------------------------------- // | Constructors | // +--------------+ /** * Build a new vector to (x,y). */ public Polar(double _radius, double _theta) { this.radius = _radius; this.theta = _theta; } // Polar(double,double) // +-----------+---------------------------------------------------- // | Observers | // +-----------+ /** * Get the X coordinate of this vector. */ public double getX() { return Math.cos(this.theta)*this.radius; } // getX() /** * Get the Y coordinate of this vector. */ public double getY() { return Math.sin(this.theta)*this.radius; } // getY() /** * Get the angle of this vector from the origin. */ public double getAngle() { return this.theta; } // getAngle() /** * Get the radius of this vector. */ public double getRadius() { return this.radius; } // getRadius() /** * Rotate this vector for some amount around the origin. */ public Vec2D rotate(double angle) { double newtheta = this.theta + angle; if (newtheta > 2*Math.PI) newtheta = newtheta - 2*Math.PI; if (newtheta < 0) newtheta = newtheta + 2*Math.PI; return new Polar(this.radius, newtheta); } // rotate(double) /** * Stretch this vector by some amount. */ public Vec2D stretch(double factor) { return new Polar(this.radius * factor, this.theta); } // stretch(double) /** * Compute the unit vector for this vector (radius 1, same * angle). */ public Vec2D unitVector() { return new Polar(1.0, this.theta); } // unitVector() /** * Convert this vector to a string. */ public String toString() { return ""; } // toString() } // class Polar