setToReflectionXY method

AffineTransformation setToReflectionXY(
  1. double x,
  2. double y
)

Sets this transformation to be a reflection about the line defined by vector (x,y). The transformation for a reflection is computed by:

d = sqrt(x2 + y2)  
sin = y / d;
cos = x / d;

Tref = Trot(sin, cos) x Tscale(1, -1) x Trot(-sin, cos)

@param x the x-component of the reflection line vector @param y the y-component of the reflection line vector @return this transformation, with an updated matrix

Implementation

AffineTransformation setToReflectionXY(double x, double y) {
  if (x == 0.0 && y == 0.0) {
    throw ArgumentError("Reflection vector must be non-zero");
  }

  /**
   * Handle special case - x = y.
   * This case is specified explicitly to avoid roundoff error.
   */
  if (x == y) {
    m00 = 0.0;
    m01 = 1.0;
    m02 = 0.0;
    m10 = 1.0;
    m11 = 0.0;
    m12 = 0.0;
    return this;
  }

  // rotate vector to positive x axis direction
  double d = math.sqrt(x * x + y * y);
  double sin = y / d;
  double cos = x / d;
  rotateSinCos(-sin, cos);
  // reflect about the x-axis
  scale(1, -1);
  // rotate back
  rotateSinCos(sin, cos);
  return this;
}