transformRectangle method Null safety

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 num d1 = m03 * rl + m13 * rt + m33;
  final num x1 = (m00 * rl + m10 * rt + m30) / d1;
  final num y1 = (m01 * rl + m11 * rt + m31) / d1;
  final num d2 = m03 * rr + m13 * rt + m33;
  final num x2 = (m00 * rr + m10 * rt + m30) / d2;
  final num y2 = (m01 * rr + m11 * rt + m31) / d2;
  final num d3 = m03 * rr + m13 * rb + m33;
  final num x3 = (m00 * rr + m10 * rb + m30) / d3;
  final num y3 = (m01 * rr + m11 * rb + m31) / d3;
  final num d4 = m03 * rl + m13 * rb + m33;
  final num x4 = (m00 * rl + m10 * rb + m30) / d4;
  final num y4 = (m01 * rl + m11 * rb + m31) / d4;

  // 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(left, top, width, heigth);
    return returnRectangle;
  } else {
    return Rectangle<num>(left, top, width, heigth);
  }
}