Rectangle<num> transformRectangle(Rectangle<num> rectangle, [ Rectangle<num> returnRectangle ])

Source

Rectangle<num> transformRectangle(math.Rectangle<num> rectangle, [Rectangle<num> returnRectangle]) {

  num rl = rectangle.left.toDouble();
  num rr = rectangle.right.toDouble();
  num rt = rectangle.top.toDouble();
  num rb = rectangle.bottom.toDouble();

  // transform rectangle corners

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

  // find minima and maxima

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

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

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

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

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

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