计算描述 GDI+ 曲线周围区域的点数组

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

从定义为用户绘制的一系列点的曲线开始(下图左侧),我想导出描述该曲线周围区域的点。为此,我使用 GraphisPath 中的 Widen 函数,如下所示:

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

如果我再画出结果,我得到右边的图,当然没有取相交部分(红色箭头)。知道如何计算绘制时也会包含该部分的 PointF[] 吗?

诀窍是使用两个 GraphicsPaths:

  • 第一个是你用来获得大纲点和Widen的那个称呼。它必须处于(默认)填充模式 Alternate.

  • 返回轮廓点后 opp 您需要将它们添加到 second GraphicsPath。这个必须设置为FillMode.Winding

第二个 GraphicsPath 将填充包括交叉点在内的完整轮廓,并且还将报告内部点为 'visible'..

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

gpWinding.AddCurve(opp);

现在 MouseClick 可以工作了:

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

填充它会填满所有轮廓区域:

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