c# 画三角形,并填充颜色
目前知道有两种方法:画多边形、GraphicsPath。但是用画多边形的方式画三角形不太好。老画不正的,截图放大就明显了。
代码
1 Point point1 = new Point(0, 0);
2 Point point2 = new Point(11, 0);
3 Point point3 = new Point(5, 8);
4 Point[] pntArr = {point1, point2, point3};
5
6 e.Graphics.FillPolygon(Brushes.Red, pntArr);
7
2 Point point2 = new Point(11, 0);
3 Point point3 = new Point(5, 8);
4 Point[] pntArr = {point1, point2, point3};
5
6 e.Graphics.FillPolygon(Brushes.Red, pntArr);
7
代码
1 int len = 10;
2 int x = 0;
3 int y = 0;
4 Point[] pntArr = new Point[3];
5 pntArr[0] = new Point(x, y);
6 pntArr[1] = new Point(x - len, y);
7 pntArr[2] = new Point(x - len / 2, (int)(len * Math.Sqrt(3) / 2 + y));
8 GraphicsPath path = new GraphicsPath();
9 path.AddLines(pntArr);
10 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
11 e.Graphics.FillPath(Brushes.Red, path);
12 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
13
2 int x = 0;
3 int y = 0;
4 Point[] pntArr = new Point[3];
5 pntArr[0] = new Point(x, y);
6 pntArr[1] = new Point(x - len, y);
7 pntArr[2] = new Point(x - len / 2, (int)(len * Math.Sqrt(3) / 2 + y));
8 GraphicsPath path = new GraphicsPath();
9 path.AddLines(pntArr);
10 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
11 e.Graphics.FillPath(Brushes.Red, path);
12 e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
13