绘制带圆角的三角形

Nag*_*TDN 1 c#

所以这是我绘制三角形的代码:

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);
Run Code Online (Sandbox Code Playgroud)

有没有办法围绕这个角落?我在谷歌上看到它,它似乎只有一种方法来圆角矩形,但不是三角形.

是否有任何命令要对我这样做,或者我必须手动执行此操作?

谢谢你的回复:)

TaW*_*TaW 7

开箱即用是不可能的.

您可以像这样创建圆角多边形:

  • 通过使所选像素数更短(两端)来更改每一行,现在AB行是A1B1行.
  • 创建一个GraphicsPath由这些线组成的线,并在每对线之间创建一条连接这些新端点的曲线
  • 为了更好地控制曲线,有助于在原始角点和新端点的连接之间添加一个点

对于三角形ABC来说,这意味着

  • 创建A1,A2,B1,B2,C1,C2
  • 然后计算中间点A3,B3,C3
  • 最后添加到GraphicsPath:
    • 线A1B1,曲线B1B3B2,线B2C1,曲线C1C3C2,线C2A2和曲线A2A3A1.

在此输入图像描述

以下是使用此方法的一段代码:

要在Paint事件中使用的GraphicsPath:

GraphicsPath GP = null;
Run Code Online (Sandbox Code Playgroud)

一个带有一个点列表的测试方法,组成一个三角形 - (*)我们透支两点以使事情变得更容易; 可以通过几行代码来添加最终的线条和曲线.

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>();
    points.Add(A);
    points.Add(B);
    points.Add(C);
    points.Add(A);  // *
    points.Add(B);  // *

    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++)
    {
        GP.AddLine(lines[l].P1, lines[l].P2);
        GP.AddCurve(new Point[] { lines[l].P2, 
                                  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);       // **
}
Run Code Online (Sandbox Code Playgroud)

例:

在此输入图像描述


Dam*_*ver 6

我认为,如果你适当地调整自己的坐标,并指定Width为您的Pen大于1并设置LineJoin属性Rounded,您应该获得与圆角的三角形.