Algorithm for translating list of wallsections into coherent polygon

梦想的初衷 提交于 2019-12-06 16:09:00

问题


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

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

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

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.

Here is an example:

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


回答1:


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

Usage:

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

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


来源:https://stackoverflow.com/questions/34263601/algorithm-for-translating-list-of-wallsections-into-coherent-polygon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!