Пример #1
0
        public static void LogAirport()
        {
            Vertex <Airport> aalborg = new Vertex <Airport>(new Airport("Aalborg"));

            Vertex <Airport> roenne = new Vertex <Airport>(new Airport("Rønne"));

            Vertex <Airport> billund = new Vertex <Airport>(new Airport("Billund"));

            Vertex <Airport> tirstrup = new Vertex <Airport>(new Airport("Tirstrup"));

            Vertex <Airport> odense = new Vertex <Airport>(new Airport("Odense"));

            Vertex <Airport> kastrup = new Vertex <Airport>(new Airport("Kastrup"));

            aalborg.AddNeighbour(odense);

            kastrup.AddNeighbour(tirstrup);

            aalborg.AddNeighbour(kastrup);

            roenne.AddNeighbour(kastrup);

            aalborg.AddNeighbour(roenne);

            billund.AddNeighbour(kastrup);

            aalborg.AddNeighbour(billund);

            Console.WriteLine(aalborg.PrintGraph(3));
        }
Пример #2
0
        public void SetUp()
        {
            _tree = new Tree <int>(2);

            var child1 = new Vertex <int>(7);

            child1.AddNeighbour(new Vertex <int>(5));
            child1.AddNeighbour(new Vertex <int>(3));
            var child2 = new Vertex <int>(4);

            child2.AddNeighbour(new Vertex <int>(6));
            _tree.Root.AddNeighbour(child1);
            _tree.Root.AddNeighbour(child2);
        }
Пример #3
0
        public virtual void AddEdge(Vertex <T> left, Vertex <T> right, TEdge item)
        {
            left.AddNeighbour(right);
            Edge <T, TEdge> edge = new(left, right, item);

            Edges.Add(edge);
        }
    // Connects the corners of 1 cell
    public void ConnectCorners()
    {
        Vertex upleft    = cornerPoints ["up left"];
        Vertex upright   = cornerPoints ["up right"];
        Vertex downleft  = cornerPoints ["down left"];
        Vertex downright = cornerPoints ["down right"];


        upleft.AddNeighbour(upright);
        upleft.AddNeighbour(downleft);

        upright.AddNeighbour(upleft);
        upright.AddNeighbour(downright);

        downleft.AddNeighbour(upleft);
        downleft.AddNeighbour(downright);

        downright.AddNeighbour(downleft);
        downright.AddNeighbour(upright);
    }
    // Connects 2 cells (connects the corner points each other and the cores)
    public void ConnectToOtherGraphVertex(GraphVertex other)
    {
        RegularCell otherCell = (RegularCell)other;

        List <Pair <Vertex> > pairList = new List <Pair <Vertex> > ();
        float delta = 0.2f;

        foreach (Vertex v1 in this.cornerPoints)
        {
            foreach (Vertex v2 in otherCell.cornerPoints)
            {
                if (Vector2.Distance(v1.Coo, v2.Coo) < delta)
                {
                    Pair <Vertex> pair = new Pair <Vertex> (v1, v2);
                    if (!pair.AlreadyExists(pairList))
                    {
                        pairList.Add(pair);
                    }
                }
            }
        }

        Debug.Assert(pairList.Count == 2);

        foreach (Pair <Vertex> p in pairList)
        {
            // Fusion of the pair elements and their respective neighborhood
            foreach (Vertex v in p.Ext1.Neighbours.ToArray())
            {
                // We add our neighbours to the other corner point
                p.Ext2.AddNeighbour(v);
            }
            // Replacement of the corner in our cell
            this.cornerPoints.Remove(p.Ext1);
            this.cornerPoints.Add(p.Ext2);
        }


        // Connection of the cores of each cells
        core.AddNeighbour(otherCell.core);
        otherCell.core.AddNeighbour(core);

        // Add to the connected cells lists
        if (!this.connectedCells.Contains(otherCell))
        {
            this.connectedCells.Add(otherCell);
        }
        if (!otherCell.connectedCells.Contains(this))
        {
            otherCell.connectedCells.Add(this);
        }
    }
    // METHODS
    // Replaces a corner by another and unites both neighborhoods
    private void ReplaceCorner(string corner, Vertex replacementCorner)
    {
        Vertex currentCorner = this.cornerPoints [corner];

        // Fusion of the neighborhoods
        foreach (Vertex v in currentCorner.Neighbours)
        {
            replacementCorner.AddNeighbour(v);
        }

        // Replacement
        this.cornerPoints[corner] = replacementCorner;
    }
Пример #7
0
    public bool InitGraph(int shortestPath, int Nodes)
    {
        int h = shortestPath + 2;
        int w = shortestPath + 2;

        while (h * w < Nodes * ratio)
        {
            int coin = UnityEngine.Random.Range(0, 1);

            if (coin == 1)
            {
                h++;
            }
            else
            {
                w++;
            }
        }

        for (int i = 0; i < h + 1; i++)
        {
            vertices.Add(new List <Vertex>());

            for (int j = 0; j < w + 1; j++)
            {
                vertices[i].Add(new Vertex(idnum, i, j));
                idnum++;
            }
        }

        int StartX = (int)Math.Floor(((double)w) / 2.0 - ((double)shortestPath) / 2.0);
        int StartY = (int)Math.Floor((double)(h) / 2.0);

        for (int i = 0; i < shortestPath + 1; i++)
        {
            vertices[StartY][StartX + i].SetLive(true);
            vertices[StartY][StartX + i].Y = StartY;
            vertices[StartY][StartX + i].X = StartX + i;

            if (i != 0)
            {
                vertices[StartY][StartX + i].AddNeighbour(vertices[StartY][StartX + i - 1], 0);
            }

            if (i == shortestPath)
            {
                Start  = vertices[StartY][StartX];
                Finish = vertices[StartY][StartX + i];
            }
        }


        for (int i = 0; i < Nodes - shortestPath - 1; i++)
        {
            bool generated = false;

            while (!generated)
            {
                for (int j = 0; j < 10; j++)
                {
                    int y = UnityEngine.Random.Range(0, vertices.Count - 1);
                    int x = UnityEngine.Random.Range(0, vertices[y].Count - 1);

                    int tcx = x;
                    int tcy = y;

                    //Ha foglalt nézd meg a szomszédokat
                    if (vertices[y][x].GetLive())
                    {
                        if (y + 1 < vertices.Count && vertices[y + 1][x].GetLive())
                        {
                            tcy = y + 1;
                        }

                        else if (y - 1 >= 0 && vertices[y - 1][x].GetLive())
                        {
                            tcy = y - 1;
                        }


                        else if (x + 1 < vertices[y].Count && vertices[y][x + 1].GetLive())
                        {
                            tcx = x + 1;
                        }


                        else if (x - 1 >= 0 && vertices[y][x - 1].GetLive())
                        {
                            tcx = x - 1;
                        }

                        else
                        {
                            continue;
                        }
                    }

                    //Ha nem foglalt akkor nézd meg hogy van e kapcsolódó szomszéd
                    if (CheckForPossibleNeighbour(tcy, tcx))
                    {
                        vertices[tcy][tcx].SetLive(true);
                        vertices[tcy][tcx].X = tcx;
                        vertices[tcy][tcx].Y = tcy;
                        break;
                    }
                }

                generated = true;
            }
        }


        //Build LiveVertices list
        for (int i = 0; i < vertices.Count; i++)
        {
            for (int j = 0; j < vertices[i].Count; j++)
            {
                if (vertices[i][j].GetLive())
                {
                    LiveVertices.Add(vertices[i][j]);
                }
            }
        }



        //Gráfél sorsolás
        //Szabályok: Él csak közvetlen szomszéd közt mehet. Nem mehet kereszt él, ezt csekkolni kell

        for (int i = 0; i < LiveVertices.Count; i++)
        {
            Vertex v = LiveVertices[i];

            //lehetséges szomszédok száma
            int possibleNeigbours = 4;


            List <int> nb = new List <int>();
            //Lehetséges szomszéd pozíciók
            nb.Add(0);
            nb.Add(1);
            nb.Add(2);
            nb.Add(3);

            for (int j = 3; j >= 0; j--)
            {
                //Akkor lehet egy irányba szomszédot adni, ha már nincs ott szomszédja, ha lehet arra szomszédja, tehát egy vonalban van még egy csúcs, és ha a szomszéd behúzása nem eredményezne kereszt élt
                if (v.CheckNeighbour(j) || PossibleNeighbourInDir(v, j) == null || CheckIfCrossEdge(v, j))
                {
                    possibleNeigbours--;
                    nb.RemoveAt(j);
                }
            }

            //Patt helyzet, itt nem lehet éle a csúcsnak úgy, hogy a síkba rajzolhatóságot ne sértené
            if (possibleNeigbours == 0 && v.GetDegree() == 0)
            {
                continue;
            }

            int newnbrs = UnityEngine.Random.Range(0, possibleNeigbours);

            //Ha nincs él de lehetne viszont 0 élt sorsoltunk, rakjunk be egyet
            if (v.GetDegree() == 0 && newnbrs == 0)
            {
                newnbrs++;
            }


            //Kisorsojuk a kellő szomszédot
            for (int j = 0; j < newnbrs; j++)
            {
                //Index a szomszéd pozíciós listában
                int nindex = UnityEngine.Random.Range(0, nb.Count - 1);

                //Hozzáadjuk a megfelelő irányban elhelyezkedő szomszédot a megfelelő irányú szomszéd slotban

                v.AddNeighbour(PossibleNeighbourInDir(v, nb[nindex]), nb[nindex]);

                //kivesszük mint lehetséges pozíciót
                nb.RemoveAt(nindex);
            }
        }

        //Jázmin Gráf Felépítése
        for (int i = 0; i < LiveVertices.Count; i++)
        {
            Vertex v = LiveVertices[i];

            for (int j = 0; j < 4; j++)
            {
                if (v.CheckNeighbour(j))
                {
                    Vertex n = v.GetNeighbour(j);

                    if (v.jnode == null)
                    {
                        Debug.Log("Blip");
                    }

                    if (n.jnode == null)
                    {
                        Debug.Log("Boip");
                    }

                    v.jnode.neighbours.Add(n.jnode, new doorData(new Vector2(0.0f, 0.0f), new Vector2(0.0f, 0.0f)));
                }
            }
        }

        /*
         *
         *
         * Innen kezdve randomizálom a kezdő és végpontot
         *
         */

        List <Vertex> degreeonevertices = new List <Vertex>();
        List <Vertex> randomlist        = new List <Vertex>();

        for (int i = 0; i < LiveVertices.Count; i++)
        {
            if (LiveVertices[i].GetDegree() == 1)
            {
                degreeonevertices.Add(LiveVertices[i]);
                randomlist.Add(LiveVertices[i]);
            }
        }

        while (randomlist.Count != 0)
        {
            int index = UnityEngine.Random.Range(0, randomlist.Count - 1);

            List <Vertex> Traversal = new List <Vertex>();
            List <Vertex> Visited   = new List <Vertex>();
            List <Vertex> Waiting   = new List <Vertex>();

            Vertex root = randomlist[index];


            Waiting.Add(root);
            randomlist.RemoveAt(index);



            while (Waiting.Count != 0)
            {
                Vertex v = Waiting[0];
                Waiting.RemoveAt(0);

                for (int i = 0; i < 4; i++)
                {
                    if (v.CheckNeighbour(i) && !Visited.Contains(v.GetNeighbour(i)))
                    {
                        Waiting.Add(v.GetNeighbour(i));
                    }
                }

                Visited.Add(v);
                Traversal.Add(v);
            }

            bool found      = false;
            bool graphbreak = false;

            for (int i = degreeonevertices.Count - 1; i >= 0; i--)
            {
                int ind = Traversal.IndexOf(degreeonevertices[i]);

                int runind = ind;

                int route = 0;

                while (runind != 0)
                {
                    bool ggraph = false;
                    //Console.WriteLine("Teve");
                    for (int j = 0; j < runind; j++)
                    {
                        if (Traversal[j] == Traversal[runind].GetNeighbour(0) || Traversal[j] == Traversal[runind].GetNeighbour(1) || Traversal[j] == Traversal[runind].GetNeighbour(2) || Traversal[j] == Traversal[runind].GetNeighbour(3))
                        {
                            runind = j;
                            route++;
                            ggraph = true;
                            break;
                        }
                    }

                    if (!ggraph)
                    {
                        graphbreak = true;
                        break;
                    }
                }

                if (graphbreak)
                {
                    break;
                }

                if (route >= shortestPath)
                {
                    found = true;

                    Start  = root;
                    Finish = degreeonevertices[i];
                    break;
                }
            }

            if (found)
            {
                return(true);
            }
        }

        return(false);
    }
Пример #8
0
 public void Add(Vertex <T> parent, Vertex <T> child)
 {
     parent.AddNeighbour(child);
 }
Пример #9
0
 private void CreateEdge(Vertex v1, Vertex v2)
 {
     v1.AddNeighbour(v2).SetGameObject(InstantiateEdge(v1.GetGameObject(), v2.GetGameObject()));
 }