Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过相交对任意数量的多边形进行分组-C#_C#_Linq - Fatal编程技术网

通过相交对任意数量的多边形进行分组-C#

通过相交对任意数量的多边形进行分组-C#,c#,linq,C#,Linq,我有多个多边形列表,每个多边形代表一个物理对象。例如: 列表A可以表示一个带孔的矩形。此列表中的一条曲线是矩形的轮廓,另一条曲线是孔 我想要一个返回列表列表的方法,其中每个列表包含相交的所有对象 我已经有了一个方法,可以返回两个对象是否相交: bool\u curveloopsinterselect(List curveLoopsA,List curveLoopsB){…} 如果两个列表中的任意两条曲线接触,将返回true 下面是我到目前为止的代码,但它只给了我一个通行证。我想我需要多个过程,所

我有多个多边形列表,每个多边形代表一个物理对象。例如:

列表A
可以表示一个带孔的矩形。此列表中的一条曲线是矩形的轮廓,另一条曲线是孔

我想要一个返回列表列表的方法,其中每个列表包含相交的所有对象

我已经有了一个方法,可以返回两个对象是否相交:

bool\u curveloopsinterselect(List curveLoopsA,List curveLoopsB){…}

如果两个列表中的任意两条曲线接触,将返回true

下面是我到目前为止的代码,但它只给了我一个通行证。我想我需要多个过程,所以如果对象A和B相交,B和C相交,那么它们将形成集合{A,B,C}。我需要任意数量的过程,有时对象根本不会相交,或者是不同集合的一部分,比如{A,B,C}和{D,E}和{F}

public List<CurveLoop> _MergeCurveLoops(List<List<CurveLoop>> elementCurveLoops, View view)
{
    // ...
    // Preprocessing

    var listOfLists = new List<List<CurveLoop>>();
    foreach (var elementCurveLoop in elementCurveLoops)
    {
        var newList = elementCurveLoops.FindAll(x => _CurveLoopsIntersect(x, elementCurveLoop));
        listOfLists.Add(newList);
    }
}


private bool _CurveLoopsIntersect(List<CurveLoop> curveLoopsA, List<CurveLoop> curveLoopsB)
{
    foreach (var curveLoopA in curveLoopsA)
    {
        foreach (var curveA in curveLoopA)
        {
             foreach (var curveLoopB in curveLoopsB)
             {
                 foreach (var curveB in curveLoopB)
                     {
                     var result = curveA.Intersect(curveB);

                     if (result == SetComparisonResult.Overlap ||
                         result == SetComparisonResult.Subset ||
                         result == SetComparisonResult.Superset ||
                         result == SetComparisonResult.Equal)
                     {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
}
公共列表\u合并曲线操作(列表元素曲线操作,视图)
{
// ...
//预处理
var listOfLists=新列表();
foreach(ElementCurveLops中的变量elementCurveLoop)
{
var newList=elementCurveLoop.FindAll(x=>_CurveLoopsIntersect(x,elementCurveLoop));
添加(新列表);
}
}
private bool_CurveLoopsIntersect(列表curveLoopsA,列表curveLoopsB)
{
foreach(curveLoopsA中的var curveLoopA)
{
foreach(曲线OPA中的var curveA)
{
foreach(CurveLopSB中的var CurveLopB)
{
foreach(CurveLopB中的var curveB)
{
var结果=曲线相交(曲线B);
如果(结果==SetComparisonResult.Overlap||
结果==SetComparisonResult.Subset||
结果==SetComparisonResult.Superset||
结果==SetComparisonResult.Equal)
{
返回true;
}
}
}
}
}
返回false;
}

这可以使用一些类似于psuedu的代码来实现

set = a,b,c, ...

While(set not empty) {
Create newSet 
Add set.first to new list
Remove set.first from set // this line isnt necessary if a curve doesnt intersect with self


For (i = 0 , i < newset.length , i++) 
 {        
      newSet.add(set.FindAll(x => _CurveLoopsIntersect(x, newSet[i]));
      set.removeRange(newSet);  // this line may have error that the first element doesnt exist in set
 }
 Add newSet to set of sets
set=a,b,c。。。
While(设置为非空){
创建新闻集
将set.first添加到新列表
删除set.first from set//如果曲线不与自身相交,则不需要此线
对于(i=0,i_CurveLoopsIntersect(x,newSet[i]));
set.removeRange(newSet);//此行可能有错误,因为集合中不存在第一个元素
}
将新闻集添加到集合集

}

这可以使用一些类似于psuedu的代码来实现

set = a,b,c, ...

While(set not empty) {
Create newSet 
Add set.first to new list
Remove set.first from set // this line isnt necessary if a curve doesnt intersect with self


For (i = 0 , i < newset.length , i++) 
 {        
      newSet.add(set.FindAll(x => _CurveLoopsIntersect(x, newSet[i]));
      set.removeRange(newSet);  // this line may have error that the first element doesnt exist in set
 }
 Add newSet to set of sets
set=a,b,c。。。
While(设置为非空){
创建新闻集
将set.first添加到新列表
删除set.first from set//如果曲线不与自身相交,则不需要此线
对于(i=0,i_CurveLoopsIntersect(x,newSet[i]));
set.removeRange(newSet);//此行可能有错误,因为集合中不存在第一个元素
}
将新闻集添加到集合集

}谢谢,你给了我正确的方向。你是对的,使用集合是正确的方法。我将集合与递归函数结合使用(类似于while循环)

我写的代码如下:

    static List<Polygon> _RecursiveMergePolygons(List<Polygon> polygons, View view)
    {
        HashSet<Polygon> initialSet = new HashSet<Polygon>(polygons);

        HashSet<Polygon> finalSet = new HashSet<Polygon>(polygons);

        foreach (var polygon in initialSet)
        {
            // Should always return at least 1 instance
            var polys = polygons.FindAll(x => _PolygonsIntersect(x, polygon));

            // if it's greater than 1, then merge them and restart the recursion, otherwise continue
            if (polys.Count > 1)
            {
                foreach (var poly in polys)
                {
                    finalSet.Remove(poly);
                }
                var mergedPolygon = new Polygon(polys, view);
                finalSet.Add(mergedPolygon);
                break;
            }
        }
        if (finalSet.Count == initialSet.Count)
        {
            return finalSet.ToList();
        }
        return _RecursiveMergePolygons(finalSet.ToList(), view);
    }
静态列表\u递归合并多边形(列表多边形,视图)
{
HashSet initialSet=新的HashSet(多边形);
HashSet finalSet=新的HashSet(多边形);
foreach(初始值集中的变量多边形)
{
//应始终返回至少一个实例
var polys=polygons.FindAll(x=>_PolygonsIntersect(x,polygon));
//如果大于1,则合并它们并重新启动递归,否则继续
如果(多边形计数>1)
{
foreach(多边形中的变量poly)
{
最终集。移除(多边形);
}
var mergedPolygon=新多边形(多边形,视图);
最终集添加(合并多边形);
打破
}
}
if(finalSet.Count==initialSet.Count)
{
返回finalSet.ToList();
}
返回_RecursiveMergePolygons(finalSet.ToList(),视图);
}

谢谢,你给了我正确的方向。你是对的,使用集合是正确的方法。我将集合与递归函数结合使用(类似于while循环)

我写的代码如下:

    static List<Polygon> _RecursiveMergePolygons(List<Polygon> polygons, View view)
    {
        HashSet<Polygon> initialSet = new HashSet<Polygon>(polygons);

        HashSet<Polygon> finalSet = new HashSet<Polygon>(polygons);

        foreach (var polygon in initialSet)
        {
            // Should always return at least 1 instance
            var polys = polygons.FindAll(x => _PolygonsIntersect(x, polygon));

            // if it's greater than 1, then merge them and restart the recursion, otherwise continue
            if (polys.Count > 1)
            {
                foreach (var poly in polys)
                {
                    finalSet.Remove(poly);
                }
                var mergedPolygon = new Polygon(polys, view);
                finalSet.Add(mergedPolygon);
                break;
            }
        }
        if (finalSet.Count == initialSet.Count)
        {
            return finalSet.ToList();
        }
        return _RecursiveMergePolygons(finalSet.ToList(), view);
    }
静态列表\u递归合并多边形(列表多边形,视图)
{
HashSet initialSet=新的HashSet(多边形);
HashSet finalSet=新的HashSet(多边形);
foreach(初始值集中的变量多边形)
{
//应始终返回至少一个实例
var polys=polygons.FindAll(x=>_PolygonsIntersect(x,polygon));
//如果大于1,则合并它们并重新启动递归,否则继续
如果(多边形计数>1)
{
foreach(多边形中的变量poly)
{
最终集。移除(多边形);
}
var mergedPolygon=新多边形(多边形,视图);
最终集添加(合并多边形);
打破
}
}
if(finalSet.Count==initialSet.Count)
{
返回finalSet.ToList();
}
返回_RecursiveMergePolygons(finalSet.ToList(),视图);
}