Package org.djutils.draw
Class Transform2d
java.lang.Object
org.djutils.draw.Transform2d
- All Implemented Interfaces:
Cloneable
Transform2d contains a MUTABLE transformation object that can transform points (x,y) based on e.g, rotation and translation.
It uses an affine transform matrix that can be built up from different components (translation, rotation, scaling,
reflection, shearing).
Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.
- Author:
- Alexander Verbraeck, Peter Knoppers
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
double[]
getMat()
Get a safe copy of the affine transformation matrix.int
hashCode()
protected static double[]
mulMatMat
(double[] m1, double[] m2) Multiply a 3x3 matrix (stored as a 9-value array by row) with another 3x3-matrix.protected static double[]
mulMatVec
(double[] m, double[] v) Multiply a 3x3 matrix (stored as a 9-value array by row) with a 4-value vector.protected static double[]
mulMatVec2
(double[] m, double[] v) Multiply a 3x3 matrix (stored as a 9-value array by row) with a 3-value vector and a 1 for the 3rd value.reflectX()
The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0).reflectY()
The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0).rotation
(double angle) The rotation around the origin with an angle in radians.scale
(double sx, double sy) Scale all coordinates with a factor for x, and y.shear
(double sx, double sy) The 2d shear leaves the xy-coordinate plane for z=0 untouched.toString()
double[]
transform
(double[] xy) Apply the stored transform on the xy-vector and return the transformed vector.Apply the stored transform on the points generated by the provided pointIterator.Apply the stored transform on the provided Bounds2d and return a new Bounds2d with the bounds of the transformed coordinates.Apply the stored transform on the provided point and return a point with the transformed coordinate.translate
(double tx, double ty) Transform coordinates by a vector (tx, ty).Translate coordinates by a the x and y values contained in a Point2d.
-
Constructor Details
-
Transform2d
public Transform2d()
-
-
Method Details
-
mulMatVec
protected static double[] mulMatVec(double[] m, double[] v) Multiply a 3x3 matrix (stored as a 9-value array by row) with a 4-value vector.- Parameters:
m
- double[]; the matrixv
- double[]; the vector- Returns:
- double[3]; the result of m x v
-
mulMatVec2
protected static double[] mulMatVec2(double[] m, double[] v) Multiply a 3x3 matrix (stored as a 9-value array by row) with a 3-value vector and a 1 for the 3rd value.- Parameters:
m
- double[]; the matrixv
- double[]; the vector- Returns:
- double[2]; the result of m x (v1, v2, 1), with the last value left out
-
mulMatMat
protected static double[] mulMatMat(double[] m1, double[] m2) Multiply a 3x3 matrix (stored as a 9-value array by row) with another 3x3-matrix.- Parameters:
m1
- double[]; the first matrixm2
- double[]; the second matrix- Returns:
- double[9]; the result of m1 x m2
-
getMat
public double[] getMat()Get a safe copy of the affine transformation matrix.- Returns:
- double[]; a safe copy of the affine transformation matrix
-
translate
Transform coordinates by a vector (tx, ty). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Parameters:
tx
- double; the translation value for the x-coordinatesty
- double; the translation value for the y-coordinates- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
translate
Translate coordinates by a the x and y values contained in a Point2d. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Parameters:
point
- Point2d; the point containing the x and y translation values- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
scale
Scale all coordinates with a factor for x, and y. A scale factor of 1 leaves the coordinate unchanged. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Parameters:
sx
- double; the scale factor for the x-coordinatessy
- double; the scale factor for the y-coordinates- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
rotation
The rotation around the origin with an angle in radians. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Parameters:
angle
- double; the angle to rotate the coordinates with with around the origin- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
shear
The 2d shear leaves the xy-coordinate plane for z=0 untouched. An x-coordinate with a value of 1 is translated by sx, and an x-coordinate with another value is translated by x*sx. Similarly, a y-coordinate with a value of 1 is translated by xy and a y-coordinate with another value is translated by y*sy. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Parameters:
sx
- double; the shear factor in the x-directionsy
- double; the shear factor in the y-direction- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
reflectX
The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
reflectY
The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
transform
public double[] transform(double[] xy) Apply the stored transform on the xy-vector and return the transformed vector. For speed reasons, no checks on correct size of the vector is done.- Parameters:
xy
- double[]; double[2] the provided vector- Returns:
- double[2]; the transformed vector
-
transform
Apply the stored transform on the provided point and return a point with the transformed coordinate.- Parameters:
point
- Point2d; the point to be transformed- Returns:
- Point2d; a point with the transformed coordinates
-
transform
Apply the stored transform on the points generated by the provided pointIterator.- Parameters:
pointIterator
- Iterator<Point2d>; generates the points to be transformed- Returns:
- Iterator<Point2d>; an iterator that will generator all transformed points
-
transform
Apply the stored transform on the provided Bounds2d and return a new Bounds2d with the bounds of the transformed coordinates. All 4 corner points have to be transformed, since we do not know which of the 4 points will result in the lowest and highest x and y coordinates.- Parameters:
boundingRectangle
- Bounds2d; the bounds to be transformed- Returns:
- Bounds2d; the new bounds based on the transformed coordinates
-
hashCode
public int hashCode() -
equals
-
toString
-