Search code examples
c#graphicsmatrixrectanglesgraphicspath

Matrix and graphicspath


I enlarged a graphicspath using matrix. How can I set that the new path would be exactly over the smaller one and not at the side of it? like the Inflate with rectangles.


Solution

  • At its core, GraphicsPath is a number of points with control points that specify how to blend these (when tension is applied). When you apply a matrix operation to the graphics path it updates all these points based on the operation you are performing in the matrix.

    That in mind, if I understand the question correctly you might start by getting the bounds of the graphics path:

    var graphicsBounds = myPath.GetBounds();
    

    From there you can create a matrix that offsets the bounds to be centered at (0, 0), scales (which will scale in both x/y direction), then offset back to the original location on the screen. This should expand the path symmetrically at the center of the original bounds. That matrix code looks something like:

    Matrix myMatrix = new Matrix();
    
    myMatrix.TranslateTransform(graphicsBounds.X + graphicsBounds.Width/2.0f, graphicsBounds.Y + graphicsBounds.Height/2.0f);
    myMatrix.Scale(scaleX, scaleY);
    myMatrix.TranslateTransform(-graphicsBounds.X - graphicsBounds.Width/2.0f, -graphicsBounds.Y - graphicsBounds.Height/2.0f);
    

    Remember too - matrix order (default) is applied in reverse. So you should read these matrix operations from bottom to top. This might not be exactly right, but at least it should help. Good luck!