peter.novan
peter.novan

Reputation: 73

C# fill polygon (triangle)

I have problem with draw two polygons. I want to fill two triangles, but one is greater than the second. I am using UserControl in winforms. Code:

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 Where is problem?

Upvotes: 3

Views: 18049

Answers (2)

LarsTech
LarsTech

Reputation: 81610

Try setting the PixelOffsetMode property:

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

Result:

enter image description here

Upvotes: 3

pid
pid

Reputation: 11597

Try keeping the order counter-clockwise and start from the highest point:

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

Tell us if that helped. Sometimes those algorithms don't behave well on border conditions.

Upvotes: 0

Related Questions