NagashTDN - 1 year ago 73
C# Question

# Drawing a triangle with rounded corners

So here's my code to draw a triangle:

``````Graphics y = CreateGraphics();
Pen yy = new Pen(Color.Red);
pictureBox1.Refresh();

Point x = new Point(Convert.ToInt32(textBox1.Text)*10, Convert.ToInt32(textBox2.Text)*10);
Point xx = new Point(Convert.ToInt32(textBox3.Text)*10, Convert.ToInt32(textBox4.Text)*10);
Point xxx = new Point(Convert.ToInt32(textBox5.Text)*10, Convert.ToInt32(textBox6.Text)*10);

Point[] u = { x, xx, xxx };
y = pictureBox1.CreateGraphics();
y.DrawPolygon(yy, u);
``````

Is there any way to round the corners on this? I read about it on google and it seems there's only a way to round rectangles, but not triangles.

Is there any command to do that for me or I have to do it manually?

Thanks for replies :)

It is not possible out of the box.

You can create rounded polygons like this:

• Change each line by making it shorter (on both ends) by your chosen number of pixels, now line AB is line A1B1..
• Create a `GraphicsPath` which consists of these lines and in between each pair of lines a curve that connects those new endpoints
• To better control the curves it would help to add a point in the middle between the original corners and the connection of the new endpoints

For a Triangle ABC this would mean to

• Create A1, A2, B1, B2, C1, C2
• Then calculate the middle points A3, B3, C3
• and finally adding to a GraphicsPath:
• Line A1B1, Curve B1B3B2, Line B2C1, Curve C1C3C2, Line C2A2 and Curve A2A3A1.

Here is a piece of code that uses this method:

A GraphicsPaths to be used in the Paint event:

``````GraphicsPath GP = null;
``````

A test method with a list of points, making up a triangle - (*) we overdraw by two points to make things easier; one could add the final lines and curves by ading a few lines of code instead..

``````private void TestButton_Click(object sender, EventArgs e)
{
Point A = new Point(5, 50);
Point B = new Point(250, 100);
Point C = new Point(50, 250);

List<Point> points = new List<Point>();

GP = roundedPolygon(points.ToArray(), 20);

panel1.Invalidate();
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
if (GP == null) return;
using (Pen pen = new Pen(Brushes.BlueViolet, 3f))
e.Graphics.DrawPath(pen, GP);
}

GraphicsPath roundedPolygon(Point[] points, int rounding)
{
GraphicsPath GP = new GraphicsPath();
List<Line> lines = new List<Line>();
for (int p = 0; p < points.Length - 1; p++)
lines.Add( shortenLine(new Line(points[p], points[p+1]), rounding) );
for (int l = 0; l < lines.Count - 1; l++)
{
supPoint(lines[l].P2,points[l+1], lines[l+1].P1),
lines[l+1].P1 });

}
return GP;
}

// a simple structure
struct Line
{
public Point P1; public Point P2;
public Line(Point p1, Point p2){P1 = p1; P2 = p2;}
}

// routine to make a line shorter on both ends
Line shortenLine(Line line, int byPixels)
{
float len = (float)Math.Sqrt(Math.Pow(line.P1.X - line.P2.X, 2)
+ Math.Pow(line.P1.Y - line.P2.Y, 2));
float prop = (len - byPixels) / len;
Point p2 = pointBetween(line.P1, line.P2, prop);
Point p1 = pointBetween(line.P1, line.P2, 1 - prop);
return new Line(p1,p2);
}

// with a proportion of 0.5 the point sits in the middle
Point pointBetween(Point p1, Point p2, float prop)
{
return new Point((int)(p1.X + (p2.X - p1.X) * prop),
(int)(p1.Y + (p2.Y - p1.Y) * prop));
}

// a supporting point, change the second prop (**) to change the rounding shape!
Point supPoint(Point p1, Point p2, Point p0)
{
Point p12 = pointBetween(p1, p2, 0.5f);
return pointBetween(p12, p0, 0.5f);       // **
}
``````

Example:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download