tags:

views:

316

answers:

1

I am drawing a triangle with the following code

int x = x coordinate for center;
int ax = x coordinate for left;
int bx = x coordinate for right;
int top = y coordinate for top;
int bottom = y coordinate for bottom;

//           (x, top)
//(ax, bottom)      (bx, bottom) 

GraphicsPath path = new GraphicsPath();
// _
path.AddLine(ax, bottom, bx, bottom);
// /
path.AddLine(ax, bottom, x, top);
// \
path.AddLine(bx, bottom, x, top);
// order of drawing is _ / \ (bottom line, left side, right side)

When I call DrawPath, it always draws my lines, no matter the order. But when I call FillPath, it does nothing. Only when my order is / _ \ or \ _ / does my triangle actually fill. Why is this?

+1  A: 

It turns out that the answer I posted originally didn't really solve the problem and that it worked on my machine because of an additional change I introduced, which is to change the FillMode:

GraphicsPath path = new GraphicsPath(FillMode.Winding);

When you use the Winding mode, the algorithm will detect a closed path even if you didn't add the lines in order.

Vojislav Stojkovic
Thanks for the explination, that sounds reasonable, however in testing that didn't work
Bob
My mistake. There's one additional detail in my test that I forgot to include, so I'll edit my answer accordingly.
Vojislav Stojkovic