[英]Remove holes from a geometry
我正在從圖形的繪制部分收集不同的幾何圖形,並將它們組合成一個幾何圖形,以顯示圖形 object 周圍的選擇突出邊框。 組合幾何由許多矩形(也可能是其他形狀)的聯合組成。 有時,當我的圖形繪制圍繞一個沒有繪制任何內容的空白點時,組合的矩形可能會在生成的幾何圖形中造成一個洞。
我怎樣才能去掉那個洞? 我只需要幾何的最外層輪廓,不需要內部的空白部分。
此代碼聯合幾何:
outline = Geometry.Combine(outline, new RectangleGeometry(rect), GeometryCombineMode.Union, null);
您可以首先創建幾何的多邊形近似作為 PathGeometry,其中包含用於其內部和外部多邊形的多個圖形。
然后 select 適當的 Figure 包含另一個 PathGeometry 的外部多邊形 - 這似乎是最后一個。 不確定這是否總是正確的。 如果不確定,您肯定會以某種方式檢索它們的邊界框和 select 最大的一個。
var outlinePath = outline.GetOutlinedPathGeometry();
var singleOutline = new PathGeometry();
singleOutline.Figures.Add(outlinePath.Figures.Last());
根據 Clemens 的回答,我編寫了以下代碼:
/// <summary>
/// Removes figures from a path geometry that are contained by other figures. These are
/// holes in an outline that shall be removed to simplify the form. Separate figures that
/// do not overlap are kept.
/// </summary>
/// <param name="geometry">The path geometry with multiple figures.</param>
private void RemoveInnerFigures(PathGeometry geometry)
{
var boundsList = new List<Rect>();
var dummyPathGeom = new PathGeometry();
foreach (var figure in geometry.Figures)
{
dummyPathGeom.Figures.Add(figure.Clone());
boundsList.Add(dummyPathGeom.Bounds);
dummyPathGeom.Figures.Clear();
}
for (int i = 0; i < geometry.Figures.Count; i++)
{
for (int j = 0; j < geometry.Figures.Count; j++)
{
if (i == j) continue;
if (boundsList[j].Contains(boundsList[i]))
{
// Figure i is contained within figure j, remove i
geometry.Figures.RemoveAt(i);
boundsList.RemoveAt(i);
i--;
break;
}
}
}
}
在幾何完成后僅應用一次似乎很重要。 在添加幾何的每一位后調用它(如問題代碼所示)將導致丟失部分。 我沒有調查它的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.