Skip to content

Instantly share code, notes, and snippets.

@jnm2
Created September 28, 2025 19:11
Show Gist options
  • Save jnm2/5d2eab3cd9461e55c7af3233f8948472 to your computer and use it in GitHub Desktop.
Save jnm2/5d2eab3cd9461e55c7af3233f8948472 to your computer and use it in GitHub Desktop.
private static SKMatrix MapUnitSquareToGivenPoints(SKPoint topLeft, SKPoint topRight, SKPoint bottomLeft, SKPoint bottomRight)
{
var rightDiff = bottomRight - topRight;
var bottomDiff = bottomRight - bottomLeft;
var determinant = rightDiff.X * bottomDiff.Y - bottomDiff.X * rightDiff.Y;
var topDiff = topRight - topLeft;
var leftDiff = bottomLeft - topLeft;
var persp0 = -(bottomDiff.X * topDiff.Y - bottomDiff.Y * topDiff.X) / determinant;
var persp1 = (rightDiff.X * leftDiff.Y - rightDiff.Y * leftDiff.X) / determinant;
return new SKMatrix
{
ScaleX = persp0 * topRight.X + topDiff.X,
SkewX = persp1 * bottomLeft.X + leftDiff.X,
TransX = topLeft.X,
SkewY = persp0 * topRight.Y + topDiff.Y,
ScaleY = persp1 * bottomLeft.Y + leftDiff.Y,
TransY = topLeft.Y,
Persp0 = persp0,
Persp1 = persp1,
Persp2 = 1,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment