我使用的ClipperLib的C#版本v6.4.2。 我有很多方塊組成漁網。我也有一個矩形。 我想得到一個結果,其中只有矩形內部的正方形被返回並且部分重疊被剪裁。 的主體是綠色和剪輯是在紅: ClipperLib剪輯多個正方形與矩形產生1個結果
我會預計將有15個全結果是正方形和13個修剪的正方形。
這是我正在使用的代碼:
var startX = 100;
var startY = 250;
const int numPolygons = 10;
var subj = new Polygons(numPolygons * numPolygons);
for (var i = 0; i < numPolygons; i++)
{
for (var j = 0; j < numPolygons; j++)
{
var square = new Polygon(4)
{
new IntPoint(startX, startY),
new IntPoint(startX + 10, startY),
new IntPoint(startX + 10, startY - 10),
new IntPoint(startX, startY - 10)
};
subj.Add(square);
// Moving to the right
startX = startX + 10;
}
// Moving down
startY = startY - 10;
startX = 100;
}
var clip = new Polygons(1);
clip.Add(new Polygon(4));
clip[0].Add(new IntPoint(165, 215));
clip[0].Add(new IntPoint(255, 215));
clip[0].Add(new IntPoint(255, 155));
clip[0].Add(new IntPoint(165, 155));
var solution = new Polygons();
var c = new Clipper.Clipper();
c.AddPaths(subj, PolyType.ptSubject, true);
c.AddPaths(clip, PolyType.ptClip, true);
c.Execute(ClipType.ctIntersection, solution, PolyFillType.pftEvenOdd, PolyFillType.pftEvenOdd);
Debug.WriteLine("solution.Count: " + solution.Count);
當運行上述代碼是大約需要0.5秒。因爲結果看起來像剪輯和主題切換我已經切換它們。結果是一樣的,但現在只需要0.1秒。所以額外的東西就完成了。我認爲這是合併的廣場。
我不想要的結果合併。我怎樣才能防止呢?或者也許我的代碼有問題?
實際上,我使用的是地理參考文件。其中一個有220萬個可以旋轉的正方形,但是彼此相鄰。邊框是一個包含許多點的地塊邊界。使用GDAL/GEOS進行裁剪不起作用。 40分鐘後。我取消它。一次只用一把剪刀和一個主題(方形)做3分鐘。所以性能足夠好。我會接受你的回答。 –
加入少量的:我改變了'的foreach subject'到'Parallel.ForEach'和使用'System.Collections.Concurrent.ConcurrentBag()'來存儲所有的解決方案,現在的時間是隻有52秒! –
保羅,快船(仍在開發中,但紮實,更快,不太可能正式發佈之前,現在很多改變)的最新版本沒有任何多邊形合併所以應該做你想要的... https://sourceforge.net/ p/polyclipping/code/HEAD/tree/sandbox/Clipper2 /。或者,您可以在舊Clipper的ExecuteInternal方法中註釋掉JoinCommonEdges()語句。 –