FromLine() public static method

public static FromLine ( Line l ) : Road
l Voronoi.Line
return Road
Ejemplo n.º 1
0
        /// <summary>
        /// Build a road inside a district cell
        /// </summary>
        public List <Road> BuildRoad(DistrictCell cell, bool generateInnerRoads, int subdivisions)
        {
            //Edges are the bounds of the road and will be part of the road as well
            var edges = cell.Edges.ToList();

            //find the longest line in the cell as a start line
            var longest = FindLongestLineInCell(edges);

            //a cell edge can be shared so first remove the line from one of the shared cells
            //all edges of the cell are part of the road
            var roads = new List <Road>();

            foreach (var edge in cell.Edges)
            {
                //add the original side edge
                var road = Road.FromLine(edge);

                roads.Add(road);
            }

            var innerRoads = new List <Road>();

            if (generateInnerRoads && subdivisions > 0)
            {
                //Create another subdivision
                roads = GenerateRoad(roads, innerRoads, Road.FromLine(longest.Key), Road.FromLine(longest.Value), subdivisions - 1);
            }

            //set up a reference to the parent cell
            foreach (var road in roads)
            {
                road.ParentCell = cell;
            }

            return(roads);
        }
Ejemplo n.º 2
0
        private void HandlePossibleRoadIntersections(Line line, Road startLine, Road endLine, List <Road> borders, List <Road> innerRoads)
        {
            bool flipped = false;

            //check for possible intersection,
            //if an intersection happens the end point becomes the point of intersection
            //has to be reverse because otherwise the lines will intersect with the edges
            for (var i = 0; i < innerRoads.Count; i++)
            {
                var l = innerRoads[i];

                //if the line intersects
                if (!line.IntersectsWith(l))
                {
                    continue;
                }

                //find the intersection point
                bool parallel = false;
                var  ip       = line.FindIntersectionPoint(l, ref parallel);

                //parrallel lines don't intersect so ignore
                if (parallel)
                {
                    continue;
                }

                //Create the new line from the intersection
                line = CreateIntersectedLine(line, ip, ref flipped);


                //Split the line that is intersected with in 2 new lines
                if (flipped)
                {
                    startLine = innerRoads[i];
                }
                else
                {
                    endLine = innerRoads[i];
                }


                //stop at the first intersection
                break;
            }

            //Because of the new line the start and end line will have to be split in 2 new lines
            //the total of new lines will be 3

            //Adjust the borders
            borders.Remove(startLine);
            borders.Remove(endLine);

            //create the split lines
            borders.Add(new Road(startLine.Start, line.Start));
            borders.Add(new Road(line.Start, startLine.End));

            borders.Add(new Road(endLine.Start, line.End));
            borders.Add(new Road(line.End, endLine.End));

            //Add the new road to the inner roads
            innerRoads.Add(Road.FromLine(line));

            innerRoads.Remove(startLine);
            innerRoads.Remove(endLine);
        }