Package org.djutils.draw
Class Transform2d
- java.lang.Object
-
- org.djutils.draw.Transform2d
-
- All Implemented Interfaces:
Cloneable
public class Transform2d extends Object implements 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-2021 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
Constructors Constructor Description Transform2d()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object obj)
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.Transform2d
reflectX()
The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0).Transform2d
reflectY()
The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0).Transform2d
rotation(double angle)
The rotation around the origin with an angle in radians.Transform2d
scale(double sx, double sy)
Scale all coordinates with a factor for x, and y.Transform2d
shear(double sx, double sy)
The 2d shear leaves the xy-coordinate plane for z=0 untouched.String
toString()
double[]
transform(double[] xy)
Apply the stored transform on the xy-vector and return the transformed vector.Iterator<Point2d>
transform(Iterator<Point2d> pointIterator)
Apply the stored transform on the provided point and return a point with the transformed coordinate.Bounds2d
transform(Bounds2d boundingRectangle)
Apply the stored transform on the provided Bounds2d and return a new Bounds2d with the bounds of the transformed coordinates.Point2d
transform(Point2d point)
Apply the stored transform on the provided point and return a point with the transformed coordinate.Transform2d
translate(double tx, double ty)
Transform coordinates by a vector (tx, ty).Transform2d
translate(Point2d point)
Translate coordinates by a the x and y values contained in a Point2d.
-
-
-
Method Detail
-
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
public Transform2d translate(double tx, double ty)
Transform coordinates by a vector (tx, ty).- 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
public Transform2d translate(Point2d point)
Translate coordinates by a the x and y values contained in a Point2d.- Parameters:
point
- Point2d; the point containing the x and y translation values- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
scale
public Transform2d scale(double sx, double sy)
Scale all coordinates with a factor for x, and y. A scale factor of 1 leaves the coordinate unchanged.- 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
public Transform2d rotation(double angle)
The rotation around the origin with an angle in radians.- 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
public Transform2d shear(double sx, double sy)
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.- 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
public Transform2d reflectX()
The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0).- Returns:
- Transform2d; the new transformation matrix after applying this transform
-
reflectY
public Transform2d reflectY()
The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0).- 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
public Point2d transform(Point2d point)
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
public Iterator<Point2d> transform(Iterator<Point2d> pointIterator)
Apply the stored transform on the provided point and return a point with the transformed coordinate.- Parameters:
pointIterator
- Iterator<Point2d>; generates the points to be transformed- Returns:
- Iterator<Point2d>; an iterator that will generator all transformed points
-
transform
public Bounds2d transform(Bounds2d boundingRectangle)
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
-
-