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 d1 = (m03 * rl + m13 * rt + m33);
num x1 = (m00 * rl + m10 * rt + m30) / d1;
num y1 = (m01 * rl + m11 * rt + m31) / d1;
num d2 = (m03 * rr + m13 * rt + m33);
num x2 = (m00 * rr + m10 * rt + m30) / d2;
num y2 = (m01 * rr + m11 * rt + m31) / d2;
num d3 = (m03 * rr + m13 * rb + m33);
num x3 = (m00 * rr + m10 * rb + m30) / d3;
num y3 = (m01 * rr + m11 * rb + m31) / d3;
num d4 = (m03 * rl + m13 * rb + m33);
num x4 = (m00 * rl + m10 * rb + m30) / d4;
num y4 = (m01 * rl + m11 * rb + m31) / d4;
// 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(left, top, width, heigth);
return returnRectangle;
} else {
return new Rectangle<num>(left, top, width, heigth);
}
}