WolframLang: Geometric Transformation

By Xah Lee. Date: . Last updated: .

When programing graphics, especially when doing things like plane tiling and patterns , or transformation of the plane , or group symmetries, often you need to do rotation, scaling, shear, reflection, or apply many translational copies of the graphics, or other non-linear transform (e.g. fish lens transform, blackhole).

You want to be able to apply this transformation to points inside graphics, or compose them as group operations.

Symbolic Representation of Geometric Transform Functions

the following, each represents a transform function symbolically:

• `RotationTransform`
• `TranslationTransform`
• `ScalingTransform`
• `ShearingTransform`
• `ReflectionTransform`
• `RescalingTransform`
• `AffineTransform`
• `LinearFractionalTransform`

they all return a `TransformationFunction[data]`

• `TransformationFunction[data][vector]` apply f to the vector.
• `TransformationFunction[data][vectorList]` apply f to a list of vectors.
• To apply the `TransformationFunction` to graphics primitives, use `GeometricTransformation`
• To turn the `TransformationFunction` into a matrix, use `TransformationMatrix`

These functions are primarily good for if you are doing geometric transformations in the context of group theory. Namely, wallpaper group, space group, point group, coxeter group, isometry group, etc. It allows you to compose group operations, and finally apply it to a point or to points in graphics.

Composition of Transformations

use `Composition` to compose them.

On order, the result of `Composition[a,b,c]` is `a[b[c]]` . That is, from righ to left, or right associative.

```(* show the input and output of transform functions, and composition on them *)

Clear[ myRot, myMove, myF ];

myRot = RotationTransform[10 Degree ];

myMove = TranslationTransform[ {1,0} ];

myF =
Composition[ RotationTransform[10 Degree ], TranslationTransform[ {1,0} ]  ];

Print @
{myRot, myMove, myF};

Print @
myF[{a,b}];

Print @
myF[{1,0}];
```

Applying a Geometric Transform to Graphics Primitives or Graphics Object

use `GeometricTransformation` to apply a transformation function to Graphics Primitives .

• `GeometricTransformation[graPrims, TransformationFunction[data]]` → applies f to graPrims. graPrims should be graphics primitives, not `Graphics` object.
• `GeometricTransformation[args]` → return itself unchanged. It display effect when it is inside `Graphics` or `Graphics3D`.
```graprims = {Circle[], Rectangle[{0, 0}]};

(*  GeometricTransformation first arg should be graphics primitive or list of *)
(* result is itself, unchanged. *)
GeometricTransformation[ graprims , ShearingTransform[Pi/4, {1, 0}, {0, 1}]]

(* has effect when it in inside Graphics or Graph3D *)
Graphics[ GeometricTransformation[ graprims , ShearingTransform[Pi/4, {1, 0}, {0, 1}]]]

(* if GeometricTransformation's args is Graphics[...], no transform is done. *)
result =
GeometricTransformation[ Graphics[graprims]  , ShearingTransform[Pi/4, {1, 0}, {0, 1}]]

(* if you shown result inside Graphics[...], it's an error. *)
Graphics[ result , Axes -> True ]
```

Matrix for Geometric Transformation

sometimes, you just need the matrix for a transformation.

these functions are good for if you want a matrix directly, e.g. if you are doing physics or linear algebra numerically.

once you get a matrix M, you can apply it to a point by just

`M . p`

3d rotation related:

matrix for nD:

`TransformationMatrix` converts a symbolic `TransformationFunction` to a matrix.

Translate (Efficient for Tiling the Plane or Space)

`Translate[graPrims, vector]`

move graPrims by vector

```(* return itself *)
Translate[Circle[], {1, 0}]

(* only show effect when inside Graphics *)
Graphics[Translate[Circle[], {1, 0}], Axes -> True]
```
`Translate[graPrims, vectorList]`

make many copies of graPrims by vectorList

this is a very efficient way to represent multiple, identical copies of an arbitrarily complex shape.

use Translate to tile a plane:

```(* octogon *)
graPrims = Line@Table[{Cos[x], Sin[x]}, {x, 0, 2 Pi, 2 Pi/8.}];

grids = CoordinateBoundsArray[{{0, 10}, {0, 10}}];

(* use Translate to copy to grid, much more efficient than using Map *)
xpattern = Translate[graPrims, Flatten[grids, 1]];

Graphics[ xpattern , Frame -> True ]
```

arg to Translate should not be a Graphics object:

```(* arg to Translate should not be a Graphics object *)

graObj = Circle[];

(* does not translate *)
Translate[ Graphics[ graObj, Axes -> True], {1, 0} ]

(* error *)
Graphics @
Translate[ Graphics[ graObj, Axes -> True], {1, 0} ]
```

Smart Function: Rotate

xtodo work in progress

`Rotate` is a smart function, that can rotate text, or other input in a smart way. Typically it's used for graphics design. Not good for mathematical programing or programing geometry.

Its input can be anything, and will smartly do rotation or other to the input, and display it.

Its input can also be Graphics Primitives, but will display a rotated version of text:

Its input can also be a `Graphics` object. The result is rotated graphics, including things such as axes:

If you compare `Rotate` vs `GeometricTransformation` and `RotationTransform`, For example,

`Rotate[ graPrims , 30 Degree, {0,0}]`

is roughly the same as

`GeometricTransformation[ graPrims , RotationTransform[ 30 Degree ] ]`

when their result is shown inside `Graphics`. but the `Rotate` version will directly display the result of literal rotated text of its input.

```(* compare Rotate vs GeometricTransformation *)

graPrims = {Line[ {{0,0}, {1,0}} ]};

re1 = Rotate[ graPrims , 30 Degree, {0,0}]

re2 = GeometricTransformation[ graPrims , RotationTransform[ 30 Degree ] ]

Graphics[{re1, re2}, Axes -> True]
```

Scale

xtodo work in progress