将壁面列表转换为连贯多边形的算法 [英] Algorithm for translating list of wallsections into coherent polygon

查看:20
本文介绍了将壁面列表转换为连贯多边形的算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个成对描述多边形的点列表,如下所示:

I have a list of points that in pairs describe a polygon, like this:

<0,0><0,1><0,1><1,0><1,0><1,1><1,1><0,0> 这是一个正方形.请注意,每对点描述一条线,因此我们的正方形由线组成

<0,0><0,1><0,1><1,0><1,0><1,1><1,1><0,0> which is a square. Note that each pair of points describes a line so our square is made up out of the lines

<<0,0><0,1>><<0,1><1,0>><<1,0><1,1>>

<1,1><0,0>>

<<0,0><0,1>><<0,1><1,0>><<1,0><1,1>><<1,1><0,0>>

然而,我必须绘制这些多边形,当所讨论的点都按顺序排列并且没有孔时,这些多边形可以正常工作.不幸的是,当输入类似于

I do however have to draw these polygons which works fine when the points in question are all properly in order and there are no holes. Unfortunatly this sometimes goes wrong when the input is like

<0,0><0,1><1,1><0,0><0,1><1,0><1,0><1,1> 并且生成的多边形很奇怪,或者当

<0,0><0,1><1,1><0,0><0,1><1,0><1,0><1,1> and the resulting polygon is weird or when there are several holes in the

<0,0><0,2><0,2><2,0><2,0><2,2><2,2><0,0><1,1><1,1.5><1,1.5><1.5,1.5><1.5,1.5><1,1>

<0,0><0,2><0,2><2,0><2,0><2,2><2,2><0,0><1,1><1,1.5><1,1.5><1.5,1.5><1.5,1.5><1,1>

在这些情况下,使用 drawpoly(points) 绘制这些多边形的天真做法是行不通的.

In these situations the naitive thing of drawing these polygons with a drawpoly(points) is not going to work.

这是在 c# 中,输入实际上是一个 List,其中 GeoData 包含 2 个点(和其他一些 misq 数据).对于输出,我正在考虑制作一个 List 和一个 List> ,其中第一组点是外线,第二组点是孔,这行得通吗?我确实需要对多边形进行一些额外的计算,然后只是绘图,但我认为使用特殊的孔列表会最简单.

This is in c# and the input is in reality a List<GeoData> where GeoData contains 2 points (and some other misq data). For the output I was thinking of making a List and a List> where the first set of points are the outer line and the second list is the holes, would that work? I do need to do some extra computations with the polygons other then just drawing but I think that will be easiest with a special list of holes.

这是一个例子:

左边是我目前得到的,右边是输入.

On the left is what I currently get on the right is the input.

推荐答案

从您的示例中,我看到您绘制了一个单个多边形.您应该为每个单独的多边形多次调用方法绘制多边形 (drawpoly(points)).

From your example I see that you draw a single polygon. You should call the method draw polygon (drawpoly(points)) multiple times for each separate polygon.

我认为绘制而不是墙壁更容易,尊重原则亲吻.

I think it's easier to draw the holes instead the wall, respecting the principle KISS.

为此,您可以将要绘制的多边形(孔)存储在列表中.如果我们对数据进行分析,我们会看到粗体数据显示了多边形的开始结束.

To do that you can store the polygons (holes) you want to draw in a list. If we do an analysis about data, we see that bolded data show the begining and the end of an polygon.

<0,0><0,2><0,2><2,0><2,0><2,2><2,2><0,0><1,1><1,1.5><1,1.5><1.5,1.5><1.5,1.5><1,1>

我们用代码表示,如下所示:

And we represent this in code, as shown below:

public List<List<GeoData>> Split(List<GeoData> points)
{
    List<List<GeoData>> polygons = new List<List<GeoData>>();
    GeoData firstPoint = null;
    List<GeoData> currentPolygon;

    foreach(var point in points)
    {
        if(firstPoint == null)
        {
            firstPoint = point;
            currentPolygon = new List<GeoData>();
            currentPolygon.Add(point);
        }
        else
        {
             currentPolygon.Add(point);
             if(point == firstPoint)
             {
                  firstPoint = null;
                  polygons.Add(currentPolygon);
             }
        }
    }
    return polygons;
}

用法:

List<List<GeoData>> polygons = Split(points);

foreach(var polygon in polygons) 
{
    drawpoly(polygon);
}

这篇关于将壁面列表转换为连贯多边形的算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆