package cse1030.drawing; /** * Scale transformations cause objects to stretch or shrink in size. * Scale transformations are controlled by two scalar values: * *
sx
: the scale factor in the horizontal directionsy
: the scale factor in the vertical direction
* A positive value of sx
greater than 1
causes the object
* to stretch in the horizontal direction.
*
*
* A positive value of sx
less than 1
causes the object
* to shrink in the horizontal direction.
*
*
* A positive value of sy
greater than 1
causes the object
* to stretch in the vertical direction.
*
*
* A positive value of sy
less than 1
causes the object
* to shrink in the vertical direction.
*
* @author CSE1030Z
*
*/
public class Scale extends Transformation {
private double sx;
private double sy;
/**
* Create a scale transformation that scales in the horizontal
* and vertical directions.
*
* @param name the name of the transformation
* @param sx the scale factor in the horizontal direction
* @param sy the scale factor in the vertical direction
*/
public Scale(String name, double sx, double sy) {
super(name);
this.sx = sx;
this.sy = sy;
}
/**
* Transform a point changing its coordinates. For a scale of
* sx
and sy
the
* coordinates of the point are transformed as follows:
*
*
* newx = sx * x
* newy = sy * y
*
* @see cse1030.drawing.Transformation#transform(cse1030.drawing.Point2D)
*
* @param p
* the point to transform
*/
@Override
public void transform(Point2D p) {
double x = p.getX();
double y = p.getY();
p.setX(this.sx * x);
p.setY(this.sy * y);
}
/**
* Apply the scale to a point to produce a new point leaving the
* original point unchanged.
*
* @see cse1030.drawing.Scale#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 scaling 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 scale. The returned string is the
* name of the scale followed by the string " : scale of factors "
* followed by sx
followed by " and " followed by
* sy
. For example, a scale of factors sx = 1.55
* and sy = 3.008
named
* T1
has the following string representation:
*
*
* "T1 : scale of factors 1.55 and 3.008"
*
* @see cse1030.drawing.Transformation#toString()
*
* @return the string representation of the shear
*/
@Override
public String toString() {
return super.toString() + " : scale, sx = " + this.sx + ", sy = " + this.sy;
}
}