Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Transform

A 3x3 transformation matrix that is used to rotate, scale, and translate geometry.

In many cases, you don't need to create this transformation matrix yourself, you can instead use the transformation functions available to all Geometry objects. That said, in some cases, you may still want to create your own matrix, particularly if you're doing your own custom transformations, or you're applying the same transformation many times. All geometry objects have a method to apply a transformation matrix (see: Vector.transform as an example).

Example

import { Transform, Vector } from 'shapetypes';

// Create a new vecctor
const vector = new Vector(3, 4);
console.log(vector.length);
// => 5

// Scale vector using a transform matrix
const matrix = Transform.scale(2);
const scaled = vector.transform(matrix);
console.log(vector.length);
// => 10

// Scale vector using the direct method
const otherScaled = vector.scale(2);
console.log(otherScaled.length);
// => 10

Hierarchy

  • Transform

Index

Constructors

constructor

  • new Transform(M00: number, M10: number, M20: number, M01: number, M11: number, M21: number, M02: number, M12: number, M22: number): Transform
  • Creates a 3x3 transformation matrix.

    The constructor specifies the 9 entries of the matrix. Conceptually, the matrix looks like:

    • M00 M10 M20
    • M01 M11 M21
    • M02 M12 M22

    Parameters

    • M00: number
    • M10: number
    • M20: number
    • M01: number
    • M11: number
    • M21: number
    • M02: number
    • M12: number
    • M22: number

    Returns Transform

Accessors

M00

  • get M00(): number

M01

  • get M01(): number

M02

  • get M02(): number

M10

  • get M10(): number

M11

  • get M11(): number

M12

  • get M12(): number

M20

  • get M20(): number

M21

  • get M21(): number

M22

  • get M22(): number

determinant

  • get determinant(): number

Create Methods

Static changeBasis

  • Creates a transform matrix that transforms the geometry from one coordinate system to another while keeping the geometry in the same position. In other words, if the geometry is described relative to planeFrom, after applying this translation, it will be in the same position but described relative to planeTo.

    Example

    import { Plane, Point, Transform, Vector } from 'shapetypes';
    
    // Setup the transformation
    const from = new Plane(new Point(3, 4), Vector.worldX());
    const to = Plane.worldXY();
    const tran = Transform.changeBasis(from, to);
    
    // Change the point's coordinates from the `from` plane to the `to` plane.
    const p = new Point(1, 2);
    const after = tran.transformPoint(p);
    console.log(after.toString());
    // => [4,6]

    Parameters

    • planeFrom: Plane

      The coordinate system the geometry is currently described in.

    • planeTo: Plane

      The coordinate system to describe the geometry in.

    Returns Transform

Static fromDiagonal

  • fromDiagonal(base?: number, diagonal?: number): Transform
  • Creates a transform matrix where the diagonal elements (M00, M11, M22) are set to one value and the other elements are set to another value.

    Parameters

    • Default value base: number = 0

      The value for all elements except M00, M11, M22.

    • Default value diagonal: number = 1

      The value for elements M00, M11, M22.

    Returns Transform

Static identity

Static planeToPlane

  • Creates a transform matrix that moves the geometry from one plane to another. The resulting geometry will be in the same place relative to planeTo as it was relative to planeFrom.

    Example

    import { Plane, Point, Transform, Vector } from 'shapetypes';
    
    // Setup the transformation
    const from = new Plane(new Point(3, 4), Vector.worldX());
    const to = Plane.worldXY();
    const tran = Transform.planeToPlane(from, to);
    
    // Move point so it is in the same place relative to plane `to` as it was for `from`
    const p = new Point(4, 6);
    const after = tran.transformPoint(p);
    console.log(after.toString());
    // => (1,2)

    Parameters

    • planeFrom: Plane

      The plane to move from.

    • planeTo: Plane

      The plane to move to.

    Returns Transform

Static rotate

  • Creates a transform matrix that rotates an object about a point.

    Parameters

    • angle: number

      The angle of rotation (in radians). If the environment's y-axis points upwards, the direction is counter-clockwise.

    • Optional pivot: Point | undefined

      The pivot point for rotation. If undefined, the object will be rotated about 0,0.

    Returns Transform

Static scale

  • Creates a transform matrix that scales the geometry by a specified amount along the x- and y-axis.

    Parameters

    • x: number

      The amount to scale the object along the x-axis. If less than 1, the object will shrink. If greater than 1, it will grow.

    • Optional y: undefined | number

      The amount to scale the object along the y-axis. If undefined, uses value from x.

    • Optional center: Point

      The center of scaling. All objects will shrink towards and grow away from this point. If undefined, it will use 0,0.

    Returns Transform

Static translate

  • Creates a transform matrix that moves an object along a vector.

    Parameters

    • move: Vector

      The direction to move the object.

    • Optional distance: number | undefined

      The distance to move the object. If set to undefined, it will use length of move vector.

    Returns Transform

Other Methods

equals

  • Checks whether another transform matrix has exactly the same values. Returns true if it does.

    Parameters

    • otherMatrix: Transform

      The matrix to compare against.

    Returns boolean

inverse

  • inverse(): { result: Transform; success: boolean }
  • Inverts the matrix and returns the resulting matrix. Generally, applying the inverse matrix to an object will undo the impact of the original matrix. So if the original matrix rotated the object 90 degrees, the inverted matrix will rotate the object 90 degrees in the opposite direction.

    note

    In some cases, it isn't possible to invert the matrix. If it isn't possible, the success return value will be false.

    Returns { result: Transform; success: boolean }

    • Readonly result: Transform

      If the inversion was successful, it will contain the inverted matrix. Otherwise contains the original matrix.

    • Readonly success: boolean

      If the inversion was successful, it will be true.

multiply

toString

  • toString(): string
  • Gets the matrix as a string in the format: [M00,M10,M20,M01,M11,M21,M02,M12,M22].

    Returns string

withValues

  • withValues(M00: number | undefined, M10: number | undefined, M20: number | undefined, M01: number | undefined, M11: number | undefined, M21: number | undefined, M02: number | undefined, M12: number | undefined, M22: number | undefined): Transform
  • Creates a copy of the Transform matrix with certain values replaced.

    Parameters

    • M00: number | undefined

      The new value for M00. If undefined, it will use the existing value in the matrix.

    • M10: number | undefined

      The new value for M10. If undefined, it will use the existing value in the matrix.

    • M20: number | undefined

      The new value for M20. If undefined, it will use the existing value in the matrix.

    • M01: number | undefined

      The new value for M01. If undefined, it will use the existing value in the matrix.

    • M11: number | undefined

      The new value for M11. If undefined, it will use the existing value in the matrix.

    • M21: number | undefined

      The new value for M21. If undefined, it will use the existing value in the matrix.

    • M02: number | undefined

      The new value for M02. If undefined, it will use the existing value in the matrix.

    • M12: number | undefined

      The new value for M12. If undefined, it will use the existing value in the matrix.

    • M22: number | undefined

      The new value for M22. If undefined, it will use the existing value in the matrix.

    Returns Transform

Generated using TypeDoc