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?
Graphics.Transform and Matrix.Rotate to solve rotation part. Graphics.FillPolygon for filling triangle.
Approximate not compiled code from samples to corresponding methods below: