2016-07-01 87 views
1

從定義爲用戶繪製的一系列點的曲線(下圖左側)開始,我想要導出描述該曲線周圍區域的點。爲此我使用從GraphisPathWiden功能如下所示如下:計算描述GDI中曲線周圍區域的點陣列+

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 [],當繪製時也會包含那部分呢?

enter image description here

+0

您是否在繪製路徑時嘗試過'FillMode.Winding'? –

+0

是的,我在創建路徑時嘗試了FillMode.Winding,但沒有更改。另外請考慮我想點陣,因爲之後我需要執行其他操作(例如確定一個點是否包含在所描述的區域內) –

回答

1

的技巧是使用 GraphicsPaths

  • 第一一個是你用得到輪廓點與Widen呼叫之一。它必須處於(默認)填充模式Alternate

  • 後已返回opp你需要將它們添加到第二GraphicsPath輪廓點。這個必須設置爲FillMode.Winding

第二GraphicsPath將填寫完整的輪廓,包括交叉(一個或多個),也將內部報告中指出要「可見」 ..

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

enter image description here

相關問題