Recursive AI

Compose affine scale and shear in C#

OneMath is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

An affine transform combines a linear change, such as scale or shear, with a translation.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
 
var scale = Affine3D.CreateScale(
    new(2, 3, 1));
var shear = Affine3D.CreateFromLinear(new(
    1, 1, 0,
    0, 1, 0,
    0, 0, 1));
Console.WriteLine((shear * scale)
    .TransformPoint(new(1, 2, 0)));
Text
(8, 6, 0)

Unzip the example, then dotnet run.

The full program

The full program adds a translation of 10 along x, transforms both a point and a direction, then reverses scale and shear to show why the order matters.

C#
using RecursiveAI.OneMath;
 
var scale = Affine3D.CreateScale(new(2, 3, 1));
var shear = Affine3D.CreateFromLinear(new(
    1, 1, 0,
    0, 1, 0,
    0, 0, 1));
var move = Affine3D.CreateTranslation(new(10, 0, 0));
var transform = move * shear * scale;
Console.WriteLine(transform.TransformPoint(new(1, 2, 0)));
Console.WriteLine(transform.TransformVector(new(1, 2, 0)));
Console.WriteLine((move * scale * shear)
    .TransformPoint(new(1, 2, 0)));
Text
(18, 6, 0)
(8, 6, 0)
(16, 6, 0)

Variations

Undo a nonsingular affine transform

C#
using RecursiveAI.OneMath;
 
var transform = Affine3D.CreateScale(new(2, 3, 1));
if (transform.TryInvert(out var inverse))
    Console.WriteLine(inverse.TransformPoint(new(2, 6, 0)));
Text
(1, 2, 0)

Compose rigid transforms · Transform surface normals

The short answer

Use Affine3D.CreateScale for nonuniform scale, Affine3D.CreateFromLinear for a shear matrix, and multiply them in the reverse of the order you want applied. OneMath uses column vectors: shear * scale applies scale first, then shear. Add a translation on the left to move the resulting point afterward.

The example scales x by 2 and y by 3, then adds the scaled y coordinate to x. The input (1, 2, 0) becomes (2, 6, 0), then (8, 6, 0). The full program finally translates x by 10, producing (18, 6, 0). These are deliberately different scale factors so the example exposes composition-order mistakes.

What is an affine transformation?

In OneMath, Affine3D stores Linear, a Matrix3x3D, and Translation, a Vector3D. The point formula is Linear * point + Translation. That representation supports rotation, scale and shear in the linear part, followed by a displacement. Its equivalent four-by-four matrix has a bottom row of (0, 0, 0, 1), as specified by the type documentation.

Choose the affine type when independent axis scales or shears are part of the model. A rigid transform uses a rotation quaternion and a translation instead; it does not provide a general linear matrix for those changes.

Points and directions need different treatment. A position moves when the object is translated. A direction, such as an edge difference between two positions, does not. TransformPoint includes the translation; TransformVector uses only the linear part. In the full program they produce (18, 6, 0) and (8, 6, 0) from the same component values.

How do you write an affine transformation matrix for scale and shear?

The Matrix3x3D constructor accepts rows in order. The shear in the snippet has rows (1, 1, 0), (0, 1, 0), (0, 0, 1). Multiplying it by a column vector gives x′ = x + y, y′ = y and z′ = z. To change the shear amount, change the second entry of the first row; that entry is the coefficient of y added to x.

Composition is not interchangeable. With the example’s input, shear then scale gives (6, 6, 0) before translation, whereas scale then shear gives (8, 6, 0). The download prints both orders with the same final translation: (16, 6, 0) and (18, 6, 0). The evaluation-order documentation specifies that a * b applies b first, including how translations compose.

How do you invert an affine transformation?

Use TryInvert when a transform might collapse an axis. It returns a success flag, so the caller can avoid applying an invalid inverse. The variation uses reciprocal axis scales to recover the original point from its scaled position.

For a linear part L and a translation t, the inverse has linear part L⁻¹ and translation −L⁻¹t. Negating t alone is not enough when a scale or shear is also present. OneMath’s matrix inversion fails when the determinant is zero or nonfinite; it does not perform a conditioning test. A successful inversion therefore is not a promise that an almost-singular matrix is suitable for your application’s numerical accuracy requirements.

All onemath recipes