Skip to content

Commit

Permalink
Add TransformationMatrix.Transform(double, double)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Nov 18, 2023
1 parent 3a96af3 commit 4537ec3
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/UglyToad.PdfPig.Core/TransformationMatrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public readonly struct TransformationMatrix
/// <summary>
/// Create a new <see cref="TransformationMatrix"/> with the X and Y scaling values set.
/// </summary>
public static TransformationMatrix GetScaleMatrix(double scaleX, double scaleY) => new TransformationMatrix(scaleX, 0, 0,
public static TransformationMatrix GetScaleMatrix(double scaleX, double scaleY) => new TransformationMatrix(
scaleX, 0, 0,
0, scaleY, 0,
0, 0, 1);

Expand Down Expand Up @@ -84,22 +85,27 @@ public static TransformationMatrix GetRotationMatrix(double degreesCounterclockw
/// The value at (0, 0) - The scale for the X dimension.
/// </summary>
public readonly double A;

/// <summary>
/// The value at (0, 1).
/// </summary>
public readonly double B;

/// <summary>
/// The value at (1, 0).
/// </summary>
public readonly double C;

/// <summary>
/// The value at (1, 1) - The scale for the Y dimension.
/// </summary>
public readonly double D;

/// <summary>
/// The value at (2, 0) - translation in X.
/// </summary>
public readonly double E;

/// <summary>
/// The value at (2, 1) - translation in Y.
/// </summary>
Expand Down Expand Up @@ -223,10 +229,20 @@ public TransformationMatrix(double a, double b, double r1, double c, double d, d
[Pure]
public PdfPoint Transform(PdfPoint original)
{
var x = A * original.X + C * original.Y + E;
var y = B * original.X + D * original.Y + F;
(double x, double y) xy = Transform(original.X, original.Y);
return new PdfPoint(xy.x, xy.y);
}

return new PdfPoint(x, y);
/// <summary>
/// Transform a point using this transformation matrix.
/// </summary>
/// <param name="x">The original point X coordinate.</param>
/// <param name="y">The original point Y coordinate.</param>
/// <returns>A new point which is the result of applying this transformation matrix.</returns>
[Pure]
public (double x, double y) Transform(double x, double y)
{
return (A * x + C * y + E, B * x + D * y + F);
}

/// <summary>
Expand Down Expand Up @@ -404,7 +420,7 @@ public TransformationMatrix Multiply(TransformationMatrix matrix)
var r3 = (E * matrix.row1) + (F * matrix.row2) + (row3 * matrix.row3);

return new TransformationMatrix(a, b, r1,
c, d, r2,
c, d, r2,
e, f, r3);
}

Expand Down

0 comments on commit 4537ec3

Please sign in to comment.