示例#1
0
    private void CreateEdgeOnRoad(Road road)
    {
        Node node1 = null, node2 = null;

        foreach (var edge in _edgeGenerator.Edges)
        {
            // проверяем, пересекается ли линия, созданная узлами startNode и endNode, с другими ветвями
            if (Math2D.LineSegmentsIntersection(
                    road.Point1,
                    road.Point2,
                    edge.Node1.Position,
                    edge.Node2.Position,
                    out Vector2 intersectionPoint))
            {
                // пересечение есть, значит на месте пересечения ставим новый узел
                var newNode = _nodeGenerator.CreateNode(intersectionPoint);
                // делим пересеченую ветвь на две части
                _edgeGenerator.SplitEdge(edge, newNode);

                // если пересечение призошло в начале/конце создаваемой дороги
                if (road.Point1 == intersectionPoint)
                {
                    node1 = newNode;
                    continue;
                }

                if (road.Point2 == intersectionPoint)
                {
                    node2 = newNode;
                    continue;
                }

                // продолжаем строить дорогу на оставшихся участках
                CreateEdgeOnRoad(new Road(road.Point1, intersectionPoint));
                CreateEdgeOnRoad(new Road(road.Point2, intersectionPoint));

                // покидаем метод, тк дорога уже разделена и ее достраивает рекурсия
                return;
            }
        }

        if (node1 == null)
        {
            node1 = _nodeGenerator.CreateNode(road.Point1);
        }
        if (node2 == null)
        {
            node2 = _nodeGenerator.CreateNode(road.Point2);
        }

        _edgeGenerator.CreateEdge(node1, node2);
    }