Mauro Ganswer
Mauro Ganswer

Reputation: 1419

Computing the array of points describing the area around a curve in GDI+

Starting from a curve defined as a series of points drawn by the user (left in figure below), I would like to derive the points describing the area around that curve. To this end I'm using the Widen function from GraphisPath as shown here below:

PointF[] ComputeAreaAroundCurve(PointF[] curvePoints)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddLines(curvePoints);
    using(Pen pen = new Pen(Color.Black, 10))
        gp.Widen(pen);
    return gp.PathPoints;
}

If I then draw the result, I obtain the figure to the right where of course the intersecting portion (red arrow) is not taken. Any idea of how to compute instead the PointF[] that when drawn would include that portion too?

enter image description here

Upvotes: 1

Views: 188

Answers (1)

TaW
TaW

Reputation: 54453

The trick is to use two GraphicsPaths:

  • The first one is the one you use to get the outline points with the Widen call. It has to be in the (default) fillmode Alternate.

  • After you have returned the outline points opp you need to add them to a second GraphicsPath. This one must be set to FillMode.Winding.

The second GraphicsPath will fill the full outline including the crossing(s) and will also report points inside to be 'visible'..

gpWinding = new GraphicsPath();
gpWinding.FillMode = FillMode.Winding;

gpWinding.AddCurve(opp);

Now a MouseClick will work:

Text = gpWinding.IsVisible(e.Location) ? "Yes" : "No";

And filling it will fill all the outlined area:

e.Graphics.FillPath(Brushes.DarkKhaki, gpWinding );
e.Graphics.DrawPath(Pens.White, gpWinding );

enter image description here

Upvotes: 1

Related Questions