For a transform, defined by viewport and window rectangles whose bottom-left and top-right coordinates are represented by (X1, Y1), (X2, Y2), (X3, Y3), and (X4, Y4), respectively, the matrix elements are determined as shown in the following example. The point (X3-1/2, Y3-1/2) transforms to (X1-1/2, Y1-1/2), and the point (X4+1/2, Y4+1/2) transforms to (X2+1/2, Y2+1/2). Therefore:
M12 = 0 M21 = 0 If X4 >= X3 then M11 = (X2-X1+1) / (X4-X3+1) M41 = (X1*X4-X3*X2+1/2 * (X2-X4 + X1-X3)) / (X4-X3+1) If X4 < X3 then M11 = (X2-X1+1) / (X4-X3-1) M41 = (X1*X4-X3*X2-1/2 * (X2+X4+X1+X3)) / (X4-X3-1) If Y4 >= Y3 then M22 = (Y2-Y1+1) / (Y4-Y3+1) M42 = (Y1*Y4-Y3*Y2+1/2 * (Y2-Y4 + Y1-Y3)) / (Y4-Y3+1) If Y4 < Y3 then M22 = (Y2-Y1+1) / (Y4-Y3-1) M42 = (Y1*Y4-Y3*Y2-1/2 * (Y2+Y4+Y1+Y3)) / (Y4-Y3-1)
Note: X4 is always greater than X3 and Y4 is always greater than Y3.
In the case of device transforms, (X3, Y3) is always (0, 0), Y4 is always greater than Y3, and the device space coordinates (X2, Y2) are exclusive. This simplifies the formula to:
M12 = 0 M21 = 0 M11 = (X2-X1) / (X4+1) M41 = (X1*X4+1/2 * (X2-1-X4+X1)) / (X4+1) = X1+1/2 (M11-1) M22 = (Y2-Y1) / (Y4+1) M42 = (Y1*Y4+1/2 * (Y2-1-Y4+Y1)) / (Y4+1) = Y1+1/2 (M22-1)