Flip an SKPath

jmainhartjmainhart USMember ✭✭
edited October 19 in SkiaSharp

I'm using slightly modified resize code from the DrawXamagon example in my application. 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):

Flipped to:

Sign In or Register to comment.