transformRectangle method

Rectangle<num> transformRectangle(
  1. Rectangle<num> rectangle, [
  2. Rectangle<num>? returnRectangle
])

Implementation

Rectangle<num> transformRectangle(math.Rectangle<num> rectangle,
    [Rectangle<num>? returnRectangle]) {
  final num rl = rectangle.left.toDouble();
  final num rr = rectangle.right.toDouble();
  final num rt = rectangle.top.toDouble();
  final num rb = rectangle.bottom.toDouble();

  // transform rectangle corners

  final x1 = rl * a + rt * c;
  final y1 = rl * b + rt * d;
  final x2 = rr * a + rt * c;
  final y2 = rr * b + rt * d;
  final x3 = rr * a + rb * c;
  final y3 = rr * b + rb * d;
  final x4 = rl * a + rb * c;
  final y4 = rl * b + rb * d;

  // find minima and maxima

  var left = x1;
  if (left > x2) left = x2;
  if (left > x3) left = x3;
  if (left > x4) left = x4;

  var top = y1;
  if (top > y2) top = y2;
  if (top > y3) top = y3;
  if (top > y4) top = y4;

  var right = x1;
  if (right < x2) right = x2;
  if (right < x3) right = x3;
  if (right < x4) right = x4;

  var bottom = y1;
  if (bottom < y2) bottom = y2;
  if (bottom < y3) bottom = y3;
  if (bottom < y4) bottom = y4;

  final width = right - left;
  final heigth = bottom - top;

  if (returnRectangle is Rectangle) {
    returnRectangle.setTo(tx + left, ty + top, width, heigth);
    return returnRectangle;
  } else {
    return Rectangle<num>(tx + left, ty + top, width, heigth);
  }
}