Search code examples
imagexamarinimage-resizingskiaskiasharp

Flip an SKPath using SkiaSharp


I'm using slightly modified resize code based on example I found. However, on resize the everything is flipped. I would like to either flip it back or prevent it from flipping in the first place.

Here is my resize code:

private static void ResizePath(SKPath buildingPath, IEnumerable<Room> rooms)
{
    var info = new SKImageInfo(512, 600, SKImageInfo.PlatformColorType, SKAlphaType.Premul);
    var drawSpaceRect = SKRect.Create(info.Size);
    //I need to find the size of the path
    var buildingPathRect = buildingPath.TightBounds;
    //I want to find the largest rectangle that can fit on my canvas maintaining the path's aspect ratio
    var sketchRect = drawSpaceRect.AspectFit(buildingPathRect.Size);
    //Now I need to transform the path to draw within the sketchRect
    //First translate original path to its own origin
    var firstTranslateM = SKMatrix.MakeTranslation(-buildingPathRect.Left, -buildingPathRect.Top);
    //Next handle scaling.  Since I maintained aspect ratio, I should be able to use either
    //width or height to figure out scaling factor
    var scalingFactor = sketchRect.Width/buildingPathRect.Width;
    var scaleM = SKMatrix.MakeScale(scalingFactor, scalingFactor);
    //Next I need to handle translation so path is centered on canvas
    var secondTranslateM = SKMatrix.MakeTranslation(sketchRect.Left, sketchRect.Top);
    //Finally I need to handle transforming the path to rotate 180 degrees
    var rotationMatrix = SKMatrix.MakeRotationDegrees(180, sketchRect.MidX, sketchRect.MidY);
    //Now combine the translation, scaling, and rotation into a single matrix by matrix multiplication/concatentation
    var transformM = SKMatrix.MakeIdentity();
    SKMatrix.PostConcat(ref transformM, firstTranslateM);
    SKMatrix.PostConcat(ref transformM, scaleM);
    SKMatrix.PostConcat(ref transformM, secondTranslateM);
    SKMatrix.PostConcat(ref transformM, rotationMatrix);
    //Now apply the transform to the path
    foreach (var r in rooms)
    {
        r.Path.Transform(transformM);
    }
}

Here is an example of what I want (ignore the line numbers):

enter image description here

Flipped to:

enter image description here

Any help would be appreciated.


Solution

  • This transformation should do what you are looking for. The terminology would be flip horizontal or reflect horizontal.

        var Ma = new SKMatrix {Values = new float[] {-1, 0, 0, 1, 0, 0, 0, 0, 0}};
        pathToFlip.Transform(Ma);