简体   繁体   English

从边缘形成多边形

[英]Forming Polygons Out of Edges

I am currently working with a graph, modeling a.network of streets.我目前正在使用图表对街道网络进行建模。 The graph consists of an array of edges, marking that two streets are connected.该图由一组边组成,标记两条街道相连。 The edges are a simple data structure containing an array of two streets.边是一个简单的数据结构,包含两条街道的数组。

Using this, I am hoping to find a way to locate cycles within the map. Specifically, I am looking to create "lots" in areas completely surrounded by roads.使用它,我希望找到一种在 map 内定位周期的方法。具体来说,我希望在完全被道路包围的区域中创建“地段”。

在此处输入图像描述

So far, I have found a lot of information regarding finding cycles in graphs, but nothing specific to my problem.到目前为止,我已经找到了很多关于在图表中查找周期的信息,但没有针对我的问题的具体信息。 Can I use a cyclical graph to solve my problem?我可以使用循环图来解决我的问题吗? If not, how should this problem be solved?如果不是,应该如何解决这个问题? Thank you!谢谢!

Edit: Additional information about source data编辑:有关源数据的附加信息

The data consists of a custom class called RoadSegment, shown below.数据由名为 RoadSegment 的自定义 class 组成,如下所示。

在此处输入图像描述

Each RoadSegment consists of a start position, an end position, and a list of all of the other RoadSegments it is connected to.每个 RoadSegment 都包含一个起点 position、一个终点 position,以及它所连接的所有其他 RoadSegment 的列表。 (The other variables are irrelevant for this task) (其他变量与此任务无关)

Every place in which two RoadSegments are connected is defined in a class called RoadEdge.两个 RoadSegment 连接的每个地方都定义在一个名为 RoadEdge 的 class 中。

在此处输入图像描述

The program contains a list of all RoadEdges.该程序包含所有 RoadEdges 的列表。

This is more of a computational geometry problem than graph theory.与图论相比,这更像是一个计算几何问题。 I would tackle it with this algorithm.我会用这个算法来解决它。

 Calculate position of all intersections
 LOOP over intersections
    Select line segment starting at intersection and heading most closely east to west
    Follow line segment to next intersection ( if none continue to next intersection )
    Turn right
    Follow line segment to next intersection ( if none continue to next intersection )
    ...
    IF reached starting intersection then add to plot list

I have not received any response from you, so I have implemented this using some simplifying assumptions - that all valid plots have 4 sides and the roads are approximately orthogonal to each other.我没有收到你的任何回复,所以我使用一些简化的假设来实现这个——所有有效的地块都有 4 条边,道路彼此大致正交。

Here is the C++ code这是 C++ 代码

std::vector<std::vector<cxy>> cCity::plotfinder()
{
    // vector of plots
    std::vector<std::vector<cxy>> vplot;

    // loop over intersections
    for (auto &inter : myIntersection)
    {
        // check if this is the north east corner of a quadrilateral plot

        std::vector<cxy> plot;
        plot.push_back(inter.myLocation);

        // find road from intersection going in approx east west direction
        auto road = inter.EastToWest();

        // find next intersection on road
        cIntersection next1;
        if (!nextIntersection(next1, inter, road))
            continue;   // intersection does not exist, so this is not a plot

        // add to plot
        plot.push_back(next1.myLocation);

        // continue to check the remaining two plot edges, if they exist

        road = next1.NorthToSouth();
        cIntersection next2;
        if (!nextIntersection(next2, next1, road))
            continue;
        plot.push_back(next2.myLocation);

        road = next2.WestToEast();
        cIntersection next3;
        if (!nextIntersection(next3, next2, road))
            continue;
        plot.push_back(next3.myLocation);

        // found a good plot, add to list
        vplot.push_back(plot);
    }

    return vplot;
}

You can find this code and a complete test application at https://github.com/JamesBremner/CityPlotter您可以在https://github.com/JamesBremner/CityPlotter找到此代码和完整的测试应用程序

Output from a sample run Output 来自样本运行

road from -100, 0 to 100, 0
road from -100, 10 to 100, 10
road from 0, 100 to 0, -100
road from 10, 100 to 10, -100

1 plots found
0 10, 10 10, 10 0, 0 0,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM