2D & Composite Transformations; Window-to-Viewport
Open in the Padhaiko app3.1 2D Transformations
In computer graphics, geometric transformations alter the position, orientation, size, or shape of geometric primitives. Transformations can be viewed from two equivalent mathematical perspectives:
- Object Transformation: The coordinate system remains fixed while the points defining the object are mapped to new spatial coordinates.
- Coordinate Transformation: The object remains fixed while the frame of reference (coordinate axes) moves or rotates.
The Need for Homogeneous Coordinates
In standard two-dimensional Cartesian coordinates, a point is represented as a column vector . Under this formulation:
- Rotation and Scaling are linear transformations represented as matrix multiplications: .
- Translation is an affine shift represented as vector addition: .
Because vector addition cannot be expressed as a matrix multiplication, combining a sequence of rotations, scalings, and translations into a single cumulative transformation matrix is impossible in Cartesian coordinates.
2D Homogeneous Coordinates
To unify all affine transformations into matrix multiplications, 2D points are represented in homogeneous coordinates by introducing a third non-zero scale factor :
In general, two homogeneous vectors and represent the same Cartesian point if and only if:
Under homogeneous coordinates, any affine transformation in the 2D plane is represented as a single matrix multiplication:
Fundamental 2D Transformations
1. Translation
Translation shifts an object by displacement distances and parallel to the and coordinate axes:
In homogeneous matrix form:
The inverse translation undoes the displacement:
2. Rotation About the Origin
A rotation moves a point along a circular arc centered at the origin through an angle (by convention, positive indicates counter-clockwise rotation).
Using polar coordinates with radius and initial angle :
After rotating by angle :
In homogeneous matrix form:
The inverse rotation is obtained by rotating by (which equals the matrix transpose ):
3. Scaling Relative to the Origin
Scaling alters the size of an object by scaling factors and along the coordinate directions:
In homogeneous matrix form:
- If , the transformation is uniform scaling (preserves geometric proportions).
- If , the transformation is differential scaling (distorts proportions).
- If or , the object is compressed; if , it is expanded.
The inverse scaling matrix is:
4. Reflection
Reflection produces a mirror image of an object across a specified axis of reflection:
| Reflection Axis | Coordinate Mapping | Homogeneous Matrix |
|---|---|---|
| -axis () | ||
| -axis () | ||
| Origin | (equivalent to rotation) | |
| Line | ||
| Line |
5. Shear
Shear distorts the shape of an object such that parallel lines remain parallel, but right angles are sheared:
- -Direction Shear (relative to the -axis): The coordinate changes proportionally to , while remains constant:
- -Direction Shear with Reference Line :
- -Direction Shear (relative to the -axis): The coordinate changes proportionally to , while remains constant:
- -Direction Shear with Reference Line :
3.2 2D Composite Transformations
A composite transformation is a sequence of successive affine transformations concatenated into a single cumulative matrix.
For a sequence of transformations followed by , then :
Properties of Transformation Composition
- Associativity: Matrix multiplication is associative:
Consequently, an arbitrary sequence of transformations can be pre-multiplied into a single composite matrix , allowing thousands of vertices to be transformed via one matrix multiplication per vertex.
- Non-Commutativity: Matrix multiplication is in general non-commutative:
The physical order in which transformations are applied is critical. For example, translating an object and then rotating it produces an entirely different location than rotating first and then translating.
Standard Composite Transformations
1. Rotation About an Arbitrary Pivot Point
Because the basic rotation matrix rotates objects around the coordinate origin , rotating an object around an arbitrary pivot point requires a three-step composite sequence:
- Translate the pivot point to the coordinate origin: .
- Perform rotation by angle about the origin: .
- Translate the origin back to the pivot point: .
2. Scaling Relative to an Arbitrary Fixed Point
Similarly, scaling about a fixed point prevents the object from translating away from its position during scaling:
- Translate the fixed point to the origin: .
- Scale relative to the origin: .
- Translate the origin back to the fixed point: .
3. Reflection About an Arbitrary Line
Reflecting an object about a general line requires aligning the arbitrary reflection line with one of the primary coordinate axes (such as the -axis):
- Translate the line vertically by so that its -intercept passes through the origin: .
- Rotate the line by angle to align it with the -axis: .
- Reflect across the -axis: .
- Undo the rotation by rotating back by : .
- Undo the translation by shifting back by : .
3.3 Window-to-Viewport Coordinate Transformation
In computer graphics pipelines, objects are modeled in real-world application units within a World Coordinate (WC) system. To display these objects on a physical output screen or window, the scene must be transformed into the Normalized Device Coordinates (NDC) or Device Coordinates (DC) of the display hardware.
World Window and Viewport
- World Window (Clipping Window): A rectangular area defined in world coordinates that selects the portion of the virtual scene to be viewed:
- Viewport: A rectangular area defined on the display device or in normalized device space where the contents of the world window are mapped and displayed:
Mathematical Derivation of Mapping Equations
The mapping must maintain the relative position of every point within the window such that its corresponding viewport point occupies the exact same proportional position:
Solving explicitly for the viewport coordinates :
where the scaling factors and are defined as:
Composite Matrix Formulation
The window-to-viewport transformation is mathematically equivalent to a three-step composite transformation:
- Translate the lower-left corner of the world window to the origin: .
- Scale the window dimensions to match the viewport dimensions: .
- Translate the origin to the lower-left corner of the viewport: .
Aspect Ratio Preservation
The aspect ratio of a rectangle is the ratio of its width to its height:
- If the aspect ratio of the world window equals the aspect ratio of the viewport (), then , and scaling is uniform. The displayed geometry is perfectly preserved.
- If , then , causing differential scaling. Circular objects distort into ellipses, and squares distort into non-square rectangles.
To maintain geometric fidelity without distortion, the application must either pad the viewport (letterboxing / pillarboxing) or expand the window to match the viewport's aspect ratio.
Worked Out Examples
1
A triangle has vertices , , and . Derive the composite transformation matrix for a counter-clockwise rotation of about pivot point , followed by uniform scaling by a factor of relative to the same pivot point. Compute the coordinates of the transformed triangle vertices.
Formulating the Sequence of Composite Matrices: The transformation sequence performed about pivot point consists of:
- Translate pivot point to origin: .
- Rotate by about origin: .
- Scale by about origin: .
- Translate origin back to pivot point: .
The cumulative transformation matrix is:
Evaluating the Matrix Products:
- Rotation matrix for ():
- Scale combined with rotation:
- Multiplied by :
- Multiplied on the left by :
Transforming the Triangle Vertices: Multiplying each vertex vector by :
- For :
(As expected, the pivot point remains invariant under pivot transformations).
- For :
- For :
The composite transformation matrix is:
The transformed vertices are , , and .
2
Derive the composite transformation matrix for reflecting 2D objects about the line . Apply this matrix to reflect the triangle with vertices , , and .
Determining the Line Equation and Composite Steps: The arbitrary reflection line is with slope and -intercept . The line makes an angle with the positive -axis. To reflect an object across :
- Translate vertically by so the line passes through the origin: .
- Reflect across the line : .
- Translate vertically back by : .
Evaluating the Composite Matrix Product:
Reflecting the Given Triangle Vertices: Multiplying each vertex by :
- For :
(Since , point lies directly on the mirror line and is invariant).
- For :
- For :
The reflection matrix is:
The reflected vertices are , , and .
3
A rectangular world window has extents and . It is mapped to a normalized viewport with and . Derive the window-to-viewport transformation matrix, and compute the viewport coordinates for triangle vertices , , and .
Evaluating Window and Viewport Dimensions:
- Window dimensions:
- Viewport dimensions:
- Scaling factors:
Constructing the Composite Matrix :
The scalar coordinate formulas are:
Transforming Vertices:
- For :
- For :
- For :
The window-to-viewport transformation matrix is:
The mapped viewport vertices are , , and .
Exercises
Exercise — 2D & Composite Transformations; Window-to-Viewport
Rotate the triangle with vertices , , and by clockwise () about the pivot point . Derive the composite transformation matrix and calculate the transformed coordinates.
Formulating the Composite Sequence: For a clockwise rotation of () about :
- Translate to origin: .
- Rotate by about origin: .
- Translate origin back to : .
Evaluating the Transformation Matrix: With and :
Multiplying:
Transforming the Vertices:
- For :
- For :
- For :
The composite matrix is:
The transformed vertices are , , and .
Scale a line segment connecting and by scaling factors relative to the fixed point . Derive the composite transformation matrix and calculate the new endpoints.
Formulating Fixed-Point Scaling Matrix: To scale relative to fixed point :
Multiplying matrices:
Transforming the Endpoints:
- For :
(The fixed point remains unchanged).
- For :
The composite scaling matrix is:
The transformed segment endpoints are and .
Derive the composite transformation matrix for reflecting points across the line . Compute the reflection of point .
Setting Up the Reflection Sequence: The line equation is (). The reflection matrix about the standard diagonal through the origin is:
To reflect across :
- Translate line vertically by : .
- Reflect across : .
- Translate vertically by : .
Evaluating the Transformation Matrix:
Reflecting Point :
Verification: The midpoint of and is , which lies directly on the mirror line .
The reflection matrix is:
The reflected point is .
Apply a -direction shear with shear factor relative to the reference line to a triangle with vertices , , and . Derive the shear matrix and determine the transformed coordinates.
Formulating the Shear Matrix with Reference Line: A -direction shear with reference line maps coordinates by:
With and :
In homogeneous matrix form:
Transforming Vertices:
- For :
(Vertices on the reference line remain unaffected).
- For :
- For :
The shear matrix is:
The transformed vertices are , , and .
A world window has extents and is mapped to a viewport .
- Check whether aspect ratio is preserved.
- Formulate the window-to-viewport transformation matrix.
- Map the point to viewport coordinates.
Aspect Ratio Check and Scaling Factor Calculation:
- World window dimensions:
- Viewport dimensions:
- Because , aspect ratio is strictly preserved with zero distortion.
- Scaling factors:
Constructing the Composite Transformation Matrix:
Mapping Point :
- Aspect ratio is preserved ().
- Transformation matrix: .
- Transformed viewport point: .
Prove mathematically that 2D geometric transformations are non-commutative in general. Demonstrate this by computing the composite matrices for:
- Translation followed by rotation .
- Rotation followed by translation .
Apply both composite transformations to the test point and compare the results.
Evaluating Translation Followed by Rotation (): When translation is applied first, followed by rotation , the matrix product is:
Evaluating:
Applying to :
Evaluating Rotation Followed by Translation (): When rotation is applied first, followed by translation , the matrix product is:
Evaluating:
Applying to :
Since and , matrix multiplication is non-commutative and the sequence of transformations strictly determines the final location.