Skip to content

Instantly share code, notes, and snippets.

@JimBobSquarePants
Last active April 6, 2022 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JimBobSquarePants/f61d4d420d17ab680ff374361b64a6e8 to your computer and use it in GitHub Desktop.
Save JimBobSquarePants/f61d4d420d17ab680ff374361b64a6e8 to your computer and use it in GitHub Desktop.
Skia to System.Numerics Port Question

I'm attempting to translate some code from Skia and I'm not sure exactly how SkMatrix works

https://github.com/google/skia/blob/1f193df9b393d50da39570dab77a0bb5d28ec8ef/src/core/SkPathBuilder.cpp#L459-L483

SkMatrix pointTransform;
pointTransform.setRotate(-angle);

SkPoint transformedMidPoint;
pointTransform.mapPoints(&transformedMidPoint, &midPointDistance, 1);
SkScalar squareRx = rx * rx;
SkScalar squareRy = ry * ry;
SkScalar squareX = transformedMidPoint.fX * transformedMidPoint.fX;
SkScalar squareY = transformedMidPoint.fY * transformedMidPoint.fY;

// Check if the radii are big enough to draw the arc, scale radii if not.
// http://www.w3.org/TR/SVG/implnote.html#ArcCorrectionOutOfRangeRadii
SkScalar radiiScale = squareX / squareRx + squareY / squareRy;
if (radiiScale > 1) {
    radiiScale = SkScalarSqrt(radiiScale);
    rx *= radiiScale;
    ry *= radiiScale;
}

pointTransform.setScale(1 / rx, 1 / ry);
pointTransform.preRotate(-angle);

SkPoint unitPts[2];
pointTransform.mapPoints(unitPts, srcPts, (int) SK_ARRAY_COUNT(unitPts));
SkVector delta = unitPts[1] - unitPts[0];

I'm trying to figure out what the equivalent of transforms would be that are applied to the matrix using System.Numerics.

  • setRotate
  • setScale
  • preRotate

Would the following be accurate?

static float DegToRad(float deg) => deg * (MathF.PI / 180f);

// setRotate ? 
Matrix3x2 pointTransform = Matrix3x2.CreateRotation(DegToRad(-angle));

// setScale ?
pointTransform.M11 = 1 / rx;
pointTransform.M22 = 1 / ry;

// preRotate ?
pointTransform *= Matrix3x2.CreateRotation(DegToRad(-angle));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment