void MakeEdgeIntersection(Road road, Vector3 intersection)
    {
        road.MoveEnd(intersection);
        road.severed = true;
        Junction j = segFactory.CreateJunction(intersection, Quaternion.identity);

        j.SetColor(Color.white);
    }
    void SnapToNewJunction(Road road, Road otherRoad, Vector3 intersection, RoadMap roadMap)
    {
        Junction j = segFactory.CreateJunction(intersection, Quaternion.identity);

        j.SetColor(Color.yellow);
        road.attachedSegments.Add(j);
        road.MoveEnd(intersection);
        road.severed      = true;
        otherRoad.severed = true;

        SetUpNewIntersection(road, otherRoad, intersection, j, roadMap);
    }
    void SnapToExistingJunction(Road road, Junction otherJunction)
    {
        otherJunction.SetColor(Color.blue);
        road.MoveEnd(otherJunction.transform.localPosition);
        road.severed = true;

        // set up links between roads
        foreach (Road r in otherJunction.outgoing)
        {
            r.prev.Add(new Road.Neighbour(road, true));
            road.next.Add(new Road.Neighbour(r, true));
        }
        foreach (Road r in otherJunction.incoming)
        {
            r.next.Add(new Road.Neighbour(road, false));
            road.next.Add(new Road.Neighbour(r, false));
        }
        otherJunction.AddIncoming(road);
    }
    public bool Check(Road road, RoadMap roadMap)
    {
        if (stop)
        {
            return(false);      // delete this
        }
        foreach (int i in nums)
        {
            if (road.id == i)
            {
                print(road.id);
            }
        }

        if (segFactory == null)
        {
            Initialize();
        }

        int      actionPriority            = 0;
        Vector3  finalCrossingIntersection = Vector3.zero;
        Vector3  finalEdgeIntersection     = Vector3.zero;
        Vector3  finalNewIntersection      = Vector3.zero;
        Road     finalOtherRoad            = null;
        Junction finalOtherJunction        = null;

        // cut off road if outside map bounds

        bool    intersects;
        Vector3 edgeIntersection = InterestsMapBoundary(road, out intersects);

        if (intersects)
        {
            actionPriority = 3;
            if (finalEdgeIntersection == Vector3.zero ||
                Vector3.Distance(road.start, edgeIntersection) < Vector3.Distance(road.start, finalEdgeIntersection))
            {
                finalEdgeIntersection = edgeIntersection;
            }
        }

        Rect           searchBounds = GetSearchBounds(road);
        List <Segment> matches      = roadMap.QuadTree.Query(road.Bounds);

        foreach (Segment other in matches)
        {
            if (other.GetType() == typeof(Road))
            {
                // check for intersecting roads

                Road otherRoad = (Road)other;

                if (actionPriority <= 4)
                {
                    bool    found;
                    Vector3 crossingIntersection = DoRoadsIntersect(road, otherRoad, out found);
                    if (!(road.Parent.id == otherRoad.id) && !(road.Parent.id == otherRoad.Parent.id))
                    {
                        if (found)
                        {
                            actionPriority = 4;

                            if (ExceedsMinimumIntersectionAngle(road, otherRoad))
                            {
                                if (finalCrossingIntersection == Vector3.zero ||
                                    Vector3.Distance(road.start, crossingIntersection) < Vector3.Distance(road.start, finalCrossingIntersection))
                                {
                                    finalCrossingIntersection = crossingIntersection;
                                    finalOtherRoad            = otherRoad;
                                }
                            }
                            else
                            {
                                if (CityConfig.SHOW_FAILED_JUNCTIONS)
                                {
                                    Junction j = segFactory.CreateJunction(road.start, Quaternion.identity);
                                    j.SetColor(Color.red);
                                }

                                return(false);
                            }
                        }
                    }
                }

                if (actionPriority <= 1)
                {
                    // check for potential crossings within snap distance

                    Vector3 newIntersection = NearestPointOnLine(otherRoad.start, otherRoad.end - otherRoad.start, road.end);
                    if (Vector3.Distance(newIntersection, road.end) < CityConfig.ROAD_SNAP_DISTANCE)
                    {
                        actionPriority = 1;

                        if (ExceedsMinimumIntersectionAngle(road, otherRoad))
                        {
                            if (finalNewIntersection == Vector3.zero ||
                                Vector3.Distance(road.start, newIntersection) < Vector3.Distance(road.start, finalNewIntersection))
                            {
                                finalNewIntersection = newIntersection;
                                finalOtherRoad       = otherRoad;
                            }
                        }
                        else
                        {
                            if (CityConfig.SHOW_FAILED_JUNCTIONS)
                            {
                                Junction j = segFactory.CreateJunction(road.start, Quaternion.identity);
                                j.SetColor(Color.red);
                            }

                            return(false);
                        }
                    }
                }
            }
            else if (actionPriority <= 2 && other.GetType() == typeof(Junction))
            {
                Junction otherJunction = (Junction)other;

                // check for existing crossings within snap distance

                if (!road.attachedSegments.Contains(otherJunction) &&
                    Vector3.Distance(otherJunction.transform.localPosition, road.end) < CityConfig.ROAD_SNAP_DISTANCE
                    )
                {
                    actionPriority = 2;
                    if (finalOtherJunction == null ||

                        Vector3.Distance(road.start, otherJunction.transform.position) < Vector3.Distance(road.start, finalOtherJunction.transform.position))
                    {
                        finalOtherJunction = otherJunction;
                    }
                }
            }
        }

        if (actionPriority == 4)
        {
            MakeRoadIntersection(road, finalOtherRoad, finalCrossingIntersection, roadMap);
        }
        else if (actionPriority == 3)
        {
            MakeEdgeIntersection(road, finalEdgeIntersection);
        }
        else if (actionPriority == 2)
        {
            SnapToExistingJunction(road, finalOtherJunction);
        }
        else if (actionPriority == 1)
        {
            SnapToNewJunction(road, finalOtherRoad, finalNewIntersection, roadMap);
        }

        return(true);
    }