C#填充多边形(三角形)

时间:2014-02-10 18:14:23

标签: c# winforms drawing draw

绘制两个多边形时遇到问题。 我想填充两个三角形,但一个大于第二个。 我在winforms中使用UserControl。 代码:

Point[] DOWN = new Point[] {new Point(0, 0), new Point(10, 0), new Point(5, 5)};
Point[] UP = new Point[] { new Point(0, 15), new Point(10, 15), new Point(5, 10) };

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19));       
            e.Graphics.FillPolygon(brush, DOWN);
            e.Graphics.FillPolygon(brush, UP);
            brush.Dispose();
        }

This is result of fill polygons 哪里有问题?

2 个答案:

答案 0 :(得分:2)

尝试设置PixelOffsetMode属性:

e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19))) {
  e.Graphics.FillPolygon(brush, DOWN);
  e.Graphics.FillPolygon(brush, UP);
}

结果:

enter image description here

答案 1 :(得分:0)

尝试逆时针保持顺序并从最高点开始:

new Point(5, 10), new Point(10, 15), new Point(0, 15)

告诉我们这是否有帮助。有时这些算法在边界条件下表现不佳。