如何在c#中为三角形的顶点着色

时间:2013-03-27 21:30:22

标签: c# graphics

我可以画三角形。这是我的代码

    Graphics surface;
    surface = this.CreateGraphics();
    SolidBrush brush = new SolidBrush(Color.Blue);
    Point[] points = { new Point(100, 100), new Point(75, 50), new Point(50, 100) };
    surface.FillPolygon(brush, points);

如何以不同方式为三角形的3个顶点着色。在这里我只使用蓝色。但我希望一个顶点是红色,另一个是蓝色,另一个是绿色。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

绘制多边形后,在顶部绘制表示顶点的圆圈。如果您只使用一种颜色,我建议将其放入for循环中,但如果您要更改画笔颜色,也可以单独进行。

将此添加到您的代码中:

SolidBrush blueBrush = new SolidBrush(Color.Blue); // same as brush above, but being consistent
SolidBrush redBrush = new SolidBrush(Color.Red); 
SolidBrush greenBrush = new SolidBrush(Color.Green);

int vertexRadius = 1; // change this to make the vertices bigger

surface.fillEllipse(redBrush, new Rectangle(100 - vertexRadius, 100 - vertexRadius, vertexRadius * 2, vertexRadius * 2));
surface.fillEllipse(blueBrush, new Rectangle(75 - vertexRadius, 50 - vertexRadius, vertexRadius * 2, vertexRadius * 2));
surface.fillEllipse(greenBrush, new Rectangle(50 - vertexRadius, 100 - vertexRadius, vertexRadius * 2, vertexRadius * 2));

修改

我从您的评论中了解到您希望从一个顶点到下一个顶点进行渐变。以下是基于MSDN文档制作路径渐变的方法。 (如果你不想用渐变填充内部,你可以使用linear gradients。)

PathGradientBrush gradientBrush = new PathGradientBrush(points);
Color[] colors = { Color.Red, Color.Blue, Color.Green };
gradientBrush.CenterColor = Color.Black;
gradientBrush.SurroundColors = colors;

然后用新刷子填充多边形:

surface.FillPolygon(gradientBrush, points);
相关问题