setToReflection method

AffineTransformation setToReflection(
  1. double x0,
  2. double y0,
  3. double x1,
  4. double y1,
)

Sets this transformation to be a reflection about the line defined by a line (x0,y0) - (x1,y1).

@param x0 the X ordinate of one point on the reflection line @param y0 the Y ordinate of one point on the reflection line @param x1 the X ordinate of another point on the reflection line @param y1 the Y ordinate of another point on the reflection line @return this transformation, with an updated matrix

Implementation

AffineTransformation setToReflection(
    double x0, double y0, double x1, double y1) {
  if (x0 == x1 && y0 == y1) {
    throw ArgumentError("Reflection line points must be distinct");
  }
  // translate line vector to origin
  setToTranslation(-x0, -y0);

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