asdfkjasdfjk
asdfkjasdfjk

Reputation: 3894

How to color vertices of a triangle in c#

I can draw triangle. Here is my code

    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);

How can I color 3 vertices of the triangle differently. Here i am using only blue color. But I want one vertex to be red, another one blue and another one green. How can I do that?

Upvotes: 1

Views: 2334

Answers (1)

Scott Mermelstein
Scott Mermelstein

Reputation: 15397

After drawing your polygon, draw circles representing your vertices on top. If you were using only one color, I would recommend putting it into a for loop, but if you're changing brush colors, you might as well do it individually.

Add this to your code:

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));

Edit

I understand from your comments that you were looking to make gradients from one vertex to the next. Here's the way to make a path gradient, based on the MSDN docs. (If you didn't want to fill the inside with a gradient, you could just use linear gradients.)

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

Then just fill the polygon with the new brush:

surface.FillPolygon(gradientBrush, points);

Upvotes: 2

Related Questions