Skip to content

Commit

Permalink
[coregraphics] Add new 'Scale' overload with 'MatrixOrder' enum
Browse files Browse the repository at this point in the history
- Fixes dotnet#4698: CGAffineTransform.Scale does not work like Swift's .scaledBy(x:y:)
  (dotnet#4698)
- 'Scale' monotouch-test now covers the new overload for the new multiplication order.
- Changed the Scale test's values so we have different values for 'x0' (it was always 0 before) and so it matches the test case from the bug report.
  • Loading branch information
VincentDondain committed Oct 18, 2018
1 parent dca1f47 commit 1fc2a8e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
22 changes: 21 additions & 1 deletion src/CoreGraphics/CGAffineTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,30 @@ public void Multiply (CGAffineTransform b)
x0 = a.x0 * b.xx + a.y0 * b.xy + b.x0;
y0 = a.x0 * b.yx + a.y0 * b.yy + b.y0;
}

public enum MatrixOrder {
Prepend = 0,
Append = 1,
}

public void Scale (nfloat sx, nfloat sy, MatrixOrder order)
{
switch (order) {
case MatrixOrder.Append: // The new operation is applied after the old operation.
this = Multiply (this, MakeScale (sx, sy)); // t' = t * [ sx 0 0 sy 0 0 ]
break;
case MatrixOrder.Prepend: // The new operation is applied before the old operation.
this = Multiply (MakeScale (sx, sy), this); // t' = [ sx 0 0 sy 0 0 ] * t – Swift equivalent
break;
default:
throw new ArgumentOutOfRangeException (nameof (order));
}
}

[Advice ("The new operation is applied after the old operation: t' = t * [ sx 0 0 sy 0 0 ].\nTo have the same behavior as the native Swift API, pass 'MatrixOrder.Prepend' to 'Scale (nfloat, nfloat, MatrixOrder)'.")]
public void Scale (nfloat sx, nfloat sy)
{
Multiply (MakeScale (sx, sy));
Scale (sx, sy, MatrixOrder.Append);
}

public static CGAffineTransform Scale (CGAffineTransform transform, nfloat sx, nfloat sy)
Expand Down
30 changes: 21 additions & 9 deletions tests/monotouch-test/CoreGraphics/AffineTransformTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,27 @@ public void StaticMultiply ()
[Test]
public void Scale ()
{
var transform = CGAffineTransform.MakeTranslation (0, 200);
transform.Scale (1, -1);

Assert.AreEqual ((nfloat) 1, transform.xx);
Assert.AreEqual ((nfloat) 0, transform.yx);
Assert.AreEqual ((nfloat) 0, transform.xy);
Assert.AreEqual ((nfloat) (-1), transform.yy);
Assert.AreEqual ((nfloat) 0, transform.x0);
Assert.AreEqual ((nfloat) (-200), transform.y0);
var transform1 = CGAffineTransform.MakeTranslation (1, 2);
// t' = t * [ sx 0 0 sy 0 0 ]
transform1.Scale (3, 4); // MatrixOrder.Prepend by default

Assert.AreEqual ((nfloat) 3, transform1.xx);
Assert.AreEqual ((nfloat) 0, transform1.yx);
Assert.AreEqual ((nfloat) 0, transform1.xy);
Assert.AreEqual ((nfloat) 4, transform1.yy);
Assert.AreEqual ((nfloat) 3, transform1.x0);
Assert.AreEqual ((nfloat) 8, transform1.y0);

var transform2 = CGAffineTransform.MakeTranslation (1, 2);
// t' = [ sx 0 0 sy 0 0 ] * t – Swift equivalent
transform2.Scale (3, 4, CGAffineTransform.MatrixOrder.Prepend);

Assert.AreEqual ((nfloat)3, transform2.xx);
Assert.AreEqual ((nfloat)0, transform2.yx);
Assert.AreEqual ((nfloat)0, transform2.xy);
Assert.AreEqual ((nfloat)4, transform2.yy);
Assert.AreEqual ((nfloat)1, transform2.x0);
Assert.AreEqual ((nfloat)2, transform2.y0);
}

[Test]
Expand Down

0 comments on commit 1fc2a8e

Please sign in to comment.