WPF draw a shape inside of another shape

By | July 19, 2018
Questions:

I have to build aerodynamically a canvas with different rectangles and some of them has to be crossed. I do this by adding 2 lines as diagonals of the rectangle.
The problem is when i set the line to be thicker, the line will go over the rectangle contour like in the picture:
enter image description here

Is there a way to set the line to be only inside the rectangle?

Here is the code that i am using to add a rectangle and the lines:

private void DrawRectangle()
    {
        var rectangle = new Rectangle();
        rectangle.Height = 100;
        rectangle.Width = 100;
        rectangle.Fill = Brushes.Yellow;
        rectangle.Stroke = System.Windows.Media.Brushes.Blue;
        rectangle.StrokeThickness = 1;

        _canvas.Children.Add(rectangle);
    }

    private void DrawBroken(Rectangle rectangle, long left, long bottom)
    {
        DrawBrokenLine(0, 0, 100, 100);
        DrawBrokenLine(0, 100, 100, 0);
    }

    private void DrawBrokenLine(long x1, long y1, long x2, long y2)
    {
        var line = new Line();
        line.X1 = x1;
        line.Y1 = y1;
        line.X2 = x2;
        line.Y2 = y2;
        line.Stroke = Brushes.Indigo;
        line.StrokeThickness = 10;

        _canvas.Children.Add(line);
    }
Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *