Matrices and Vectors (Basics)

www.CAD6.com

Most objects in CAD6 are defined by a number of definition points lying in a Cartesian coordinate system. Each definition point consists of two coordinates. If a calculation is made with such a definition point, it should be interpreted as a position vector relative to the internal origin.

 

If a calculation is made with the point P, the calculation is meant to be done with the vector [ P.x, P.y ], where P.x is the X-coordinate of P and P.y the Y-coordinate of P. A calculation instruction like:

 

P = P1 + 0.5 × P2

 

has to be read like:

 

P.x = P1.x + 0.5 × P2.x

P.y = P1.y + 0.5 × P2.y

 

Some entities contain matrices that represent transformations to be performed before displaying the entity. This is especially required when referencing external entities like blocks or characters. For such entities, the display matrix determines the position, size, rotation, and shearing of the referenced entity.

 

Conjugated matrices, i.e. matrices mirrored at their diagonal, will be marked by a 'T' at their upper right edge.

 

 

Operations

If a referenced entity is to be displayed, it has to be multiplied with the display matrix stored in the referencing entity. Usually, this can be done by simply multiplying all of the entity's definition points with this display matrix. Such a multiplication is handled like follows:

 

[  x  ]T  [ m11 m12 0.0 ]   [ x×m11+y×m21+m31 ]T

[  y  ] × [ m21 m22 0.0 ] = [ x×m12+y×m22+m32 ]

[ 1.0 ]   [ m31 m32 1.0 ]   [       1.0       ]

 

In complex entities, there might be the need to perform additional adaptation of object-specific values like the arc direction of Object 06 "Circular Arc".

 

The great advantage of matrices is the fact that all types of transformations can be handled equally as they can all be stored in the same type of matrix. So, any combination of transformation can be performed by a single matrix multiplication.

 

For the most important transformation types, their corresponding matrices will be listed below. If multiple transformations shall be combined, simply multiply the corresponding matrices and apply the resulting matrix to the entities. The matrices' multiplication will be easier if, instead of always multiplying two matrices, the new transformation is applied to an existing matrix. To do so, always start with the identity matrix (ident[]):

 

          [ 1.0 0.0 0.0 ]

ident[] = [ 0.0 1.0 0.0 ]

          [ 0.0 0.0 1.0 ]

 

Related interface command: MKI_MATRIX::Init.

 

The following descriptions of transformations do always show the matrices OLD and NEW from the equation NEW = OLD × transformation-matrix. The transformation matrix itself is not stated as it is not relevant.

 

 

Moving

A movement of vx and vy (move[vx,vy]) leads to the following result:

 

[ m11 m12 0.0 ]   move[vx,vy]   [   m11    m12  0.0 ]

[ m21 m22 0.0 ] --------------> [   m21    m22  0.0 ]

[ m31 m32 1.0 ]                 [ m31+vx m32+vy 1.0 ]

 

Related interface command: MKI_MATRIX::Move.

 

 

Scaling

A scaling of factor sx and sy relative to the coordinate space's origin (scale[sx,sy]) leads to the following result:

 

[ m11 m12 0.0 ]   scale[sx,sy]   [ m11×sx m12×sy 0.0 ]

[ m21 m22 0.0 ] ---------------> [ m21×sx m22×sy 0.0 ]

[ m31 m32 1.0 ]                  [ m31×sx m32×sy 1.0 ]

 

Related interface command: MKI_MATRIX::Scale.

 

A scaling of factor sx and sy relative to the initial matrix origin (scalefix[sx,sy]) leads to the following result:

 

[ m11 m12 0.0 ] scalefix[sx,sy]  [ m11×sx m12×sy 0.0 ]

[ m21 m22 0.0 ] ---------------> [ m21×sx m22×sy 0.0 ]

[ m31 m32 1.0 ]                  [ m31    m32    1.0 ]

 

Related interface command: MKI_MATRIX::ScaleFix.

 

 

Rotating

A rotation of angle ß relative to the coordinate space's origin (rot[ß]) leads to the following result:

 

[ m11 m12 0.0 ]

[ m21 m22 0.0 ]

[ m31 m32 1.0 ]

 

         |

rot[ß]        |

         V

 

[ m11×cos(ß)-m12×sin(ß) m11×sin(ß)+m12×cos(ß) 0.0 ]

[ m21×cos(ß)-m22×sin(ß) m21×sin(ß)+m22×cos(ß) 0.0 ]

[ m31×cos(ß)-m32×sin(ß) m31×sin(ß)+m32×cos(ß) 1.0 ]

 

Related interface command: MKI_MATRIX::Rotate.

 

A rotation of angle ß relative to the initial matrix origin (rotfix[ß]) leads to the following result:

 

[ m11 m12 0.0 ]

[ m21 m22 0.0 ]

[ m31 m32 1.0 ]

 

         |

rotfix[ß]        |

         V

 

[ m11×cos(ß)-m12×sin(ß) m11×sin(ß)+m12×cos(ß) 0.0 ]

[ m21×cos(ß)-m22×sin(ß) m21×sin(ß)+m22×cos(ß) 0.0 ]

[ m31                   m31                   1.0 ]

 

Related interface command: MKI_MATRIX::RotateFix.

 

 

Shearing

A horizontal shearing with slope s (hshear[s]) leads to the following result:

 

[ m11 m12 0.0 ]   hshear[s]   [ m11 m12+m11×s 0.0 ]

[ m21 m22 0.0 ] ------------> [ m21 m22+m21×s 0.0 ]

[ m31 m32 1.0 ]               [ m31 m32+m31×s 1.0 ]

 

A vertical shearing with slope s (vshear[s]) leads to the following result:

 

[ m11 m12 0.0 ]   vshear[s]   [ m11+m12×s m12 0.0 ]

[ m21 m22 0.0 ] ------------> [ m21+m22×s m22 0.0 ]

[ m31 m32 1.0 ]               [ m31+m32×s m32 1.0 ]

 

Related interface commands: MKI_MATRIX::DistortX and MKI_MATRIX::DistortY.

 

 

Mirroring

A mirroring along the X-axis (xref[]) leads to the following result:

 

[ m11 m12 0.0 ]   xref[]   [ m11 -m12 0.0 ]

[ m21 m22 0.0 ] ---------> [ m21 -m22 0.0 ]

[ m31 m32 1.0 ]            [ m31 -m32 1.0 ]

 

A mirroring along the Y-axis (yref[]) leads to the following result:

 

[ m11 m12 0.0 ]   yref[]   [ -m11 m12 0.0 ]

[ m21 m22 0.0 ] ---------> [ -m21 m22 0.0 ]

[ m31 m32 1.0 ]            [ -m31 m32 1.0 ]

 

Mirroring can also be implemented as a scaling of (-1.0,1.0) or (1.0,-1.0) respectively:

 

xref[] = scale[ 1.0,-1.0 ]

yref[] = scale[ -1.0,1.0 ]

 

Related interface command: MKI_MATRIX::MirrorX and MKI_MATRIX::MirrorY.

 

 

Inverting

In order to invert the effect of the transformations stored in a matrix, this matrix can be inverted directly. Due to the simplified representation of the 3×3 matrix, this inversion (inv[]) can be done as follows:

 

det = m11×m22 - m12×m21

 

[ m11 m12 0.0 ]

[ m21 m22 0.0 ]

[ m31 m32 1.0 ]

 

        |

 inv[]  |

        V

 

[         m22/det              -m12/det       0.0 ]

[        -m21/det               m11/det       0.0 ]

[ (m21×m32-m22×m31)/det (m12×m31-m11×m32)/det 1.0 ]

 

This inversion is obviously only possible if det is nonzero.

 

Related interface command: MKI_MATRIX::Invert.

 

 

Multiplication

In order to combine multiple subsequent operations, the corresponding transformation matrices can be multiplied into one compound matrix which is then applied. Due to the simplified representation of the 3×3 matrices, this multiplication (m×n) can be done as follows:

 

[ m11 m12 0.0 ]   [ n11 n12 0.0 ]

[ m21 m22 0.0 ] × [ n11 n12 0.0 ]

[ m31 m32 1.0 ]   [ n11 n12 0.0 ]

 

         |

         |

         V

 

[ m11×n11+m12×n21     m11×n12+m12×n22     0.0 ]

[ m21×n11+m22×n21     m21×n12+m22×n22     0.0 ]

[ m31×n11+m32×n21+n31 m31×n12+m32×n22+n32 1.0 ]

 

Please note that n×m is NOT EQUAL to m×n, i.e. the order of the transformations matters!

 

Related interface command: MKI_MATRIX::Multiply.

 

 

Separation

If a matrix shall be resolved into its basic transformational elements (scaling, horizontal shearing, rotation, movement), this can be done by means of the following calculation steps:

 

len1 = sqrt( m11×m11+m12×m12 )

len2 = sqrt( m21×m21+m22×m22 )

det  = m11×m22-m12×m21

 

[ m11 m12 0.0 ]

[ m21 m22 0.0 ]

[ m31 m32 1.0 ]

 

         =

 

ident [] ×

scale [ len1, abs( det / len2 ) ] ×

hshear[ atan2( m22, m21 ) - atan2( m12, m11 ) ] ×

rot   [ atan2( m12, m11 ) ] ×

move  [ m31, m32 ]

 

This extraction is obviously only possible if det is nonzero.

 

Related interface command: MKI_MATRIX::Separate and MKI_MATRIX::SeparateText (for text objects which use text height and horizontal compression instead of x and y scaling)

 

 

Assembly

If a matrix shall be assembled from its basic transformational elements (scaling, horizontal shearing, rotation, movement), this can be done by means of the following calculation steps:

 

                  ident [] ×

[ m11 m12 0.0 ]   scale [ fx, fy ] ×

[ m21 m22 0.0 ] = hshear[ s ] ×

[ m31 m32 1.0 ]   rot   [ ß ] ×

                  move  [ dx, dy ]

 

Related interface command: MKI_MATRIX::Assemble and MKI_MATRIX::AssembleText (for text objects which use text height and horizontal compression instead of x and y scaling).

 

CAD6interface 2024.2 - Copyright 2024 Malz++Kassner® GmbH