package cse1030.drawing; /** * A class that rotates points about the origin. The amount of rotation is * controlled by a scalar parameter called the rotation angle. A positive * rotation angle causes points to rotate counterclockwise about the origin. * * @author CSE1030Z * */ public class Rotation extends Transformation { private double angle; /** * Construct a rotation transformation given its name and the angle * of rotation in radians. * * @param name the name of the transformation * @param radians the rotation angle in radians */ public Rotation(String name, double radians) { super(name); this.angle = radians; } /** * Transform a point changing its coordinates. For a rotation of * theta radians the x and y * coordinates of the point are transformed as follows: * *

* newx = cos(theta) * x - sin(theta) * y
* newy = sin(theta) * x + cos(theta) * y
* * @see cse1030.drawing.Transformation#transform(cse1030.drawing.Point2D) * * @param p * the point to transform */ @Override public void transform(Point2D p) { double c = Math.cos(this.angle); double s = Math.sin(this.angle); double x = p.getX(); double y = p.getY(); p.setX(c * x - s * y); p.setY(s * x + c * y); } /** * Apply the rotation to a point to produce a new point leaving the * original point unchanged. * * @see cse1030.drawing.Rotation#transform(cse1030.drawing.Point2D) * * @see cse1030.drawing.Transformation#apply(cse1030.drawing.Point2D) * * @param p the point to apply the transformation to * @return a new point equal to rotating the input point p */ @Override public Point2D apply(Point2D p) { Point2D q = new Point2D(p); this.transform(q); return q; } /** * Get a string representation of the rotation. The returned string is the * name of the rotation followed by the string " : rotation of " * followed by the angle in radians followed by the string * " radians". For example, a rotation of 90 degrees named * T1 has the following string representation: * *

* "T1 : rotation of 1.5707963267948966 radians" * * @see cse1030.drawing.Transformation#toString() * * @return the string representation of the rotation */ @Override public String toString() { return super.toString() + " : rotation of " + this.angle + " radians"; } }