Icemanind
Icemanind

Reputation: 48726

Drawing a triangle in GDI+ given a rectangle

I am trying to create a function that will create a triangle, given a Rectangle structure. I have the following code:

public enum Direction {
    Up,
    Right,
    Down,
    Left
}

private void DrawTriangle(Graphics g, Rectangle r, Direction direction)
{
    if (direction == Direction.Up) {
        int half = r.Width / 2;

        g.DrawLine(Pens.Black, r.X, r.Y + r.Height, r.X + Width, r.Y + r.Height); // base
        g.DrawLine(Pens.Black, r.X, r.Y + r.Height, r.X + half, r.Y); // left side
        g.DrawLine(Pens.Black, r.X + r.Width, r.Y + r.Height, r.X + half, r.Y); // right side
    }
}

This works, as long as the direction is up. But I have two problems. First of all, is there a way to always draw it up, but simply rotate it 0, 90, 180 or 270 degrees respectively, to keep from having to use four if statements? Secondly, how can I fill the triangle in with a black color?

Upvotes: 4

Views: 6926

Answers (2)

craftworkgames
craftworkgames

Reputation: 9957

You could draw a uniform triangle then rotate and scale it using a Matrix transformation to fit inside the rectangle but to be honest I think that's more work than just defining each point.

    private void DrawTriangle(Graphics g, Rectangle rect, Direction direction)
    {            
        int halfWidth = rect.Width / 2;
        int halfHeight = rect.Height / 2;
        Point p0 = Point.Empty;
        Point p1 = Point.Empty;
        Point p2 = Point.Empty;          

        switch (direction)
        {
            case Direction.Up:
                p0 = new Point(rect.Left + halfWidth, rect.Top);
                p1 = new Point(rect.Left, rect.Bottom);
                p2 = new Point(rect.Right, rect.Bottom);
                break;
            case Direction.Down:
                p0 = new Point(rect.Left + halfWidth, rect.Bottom);
                p1 = new Point(rect.Left, rect.Top);
                p2 = new Point(rect.Right, rect.Top);
                break;
            case Direction.Left:
                p0 = new Point(rect.Left, rect.Top + halfHeight);
                p1 = new Point(rect.Right, rect.Top);
                p2 = new Point(rect.Right, rect.Bottom);
                break;
            case Direction.Right:
                p0 = new Point(rect.Right, rect.Top + halfHeight);
                p1 = new Point(rect.Left, rect.Bottom);
                p2 = new Point(rect.Left, rect.Top);
                break;
        }

        g.FillPolygon(Brushes.Black, new Point[] { p0, p1, p2 });  
    }

Upvotes: 3

Alexei Levenkov
Alexei Levenkov

Reputation: 100575

Graphics.Transform and Matrix.Rotate to solve rotation part. Graphics.FillPolygon for filling triangle.

Approximate not compiled code from samples to corresponding methods below:

// Create a matrix and rotate it 45 degrees.
Matrix myMatrix = new Matrix();
myMatrix.Rotate(45, MatrixOrder.Append);
graphics.Transform = myMatrix;
graphics.FillPolygon(new SolidBrush(Color.Blue), points);

Upvotes: 1

Related Questions