示例#1
0
        /// <summary>
        /// Creates a new vertex
        /// </summary>
        /// <param name="name">The name of this vertex</param>
        /// <param name="position">The position of this vertex</param>
        public Vertex(String name, Vector2 position)
        {
            // Save the data
            this.name = name;
            this.position = position;

            // Defaults
            visited = false;
            permanent = false;
            adjacent = null;
            weight = 0;
        }
示例#2
0
        /// <summary>
        /// Helper method for drawing a single edge
        /// </summary>
        /// <param name="v1">The first vertex</param>
        /// <param name="v2">The second vertex</param>
        /// <param name="color">The color of the edge & weight</param>
        /// <param name="sb">The spritebatch to use when drawing</param>
        private void DrawOneEdge(Vertex v1, Vertex v2, Color color, SpriteBatch sb)
        {
            // Calculate the scale of the edge in pixels
            Vector2 scale = new Vector2(Vector2.Distance(v2.Position, v1.Position), EDGE_WIDTH);

            // Calculate the rotation
            float rotation = (float)Math.Atan2(v2.Position.Y - v1.Position.Y, v2.Position.X - v1.Position.X);

            // Draw
            sb.Draw(
                pixel,
                v1.Position,
                null,
                color,
                rotation,
                Vector2.Zero,
                scale,
                SpriteEffects.None,
                0.0f
            );

            // Get the edge's weight
            int weight = adjMatrix[vertNameToIndex[v1.Name], vertNameToIndex[v2.Name]];

            // Draw above the center
            Vector2 pos = v1.Position;
            pos.X += (v2.Position.X - v1.Position.X) / 2.0f;
            pos.Y += (v2.Position.Y - v1.Position.Y) / 2.0f;

            // Draw the text
            sb.DrawString(font, weight.ToString(), pos, Color.White);
        }
示例#3
0
 //Create a method that will search through the vertices list and find out which non-permanent node
 //contains the smallest label and has been visited
 public Vertex SmallestLabel()
 {
     //Create a local variable for smallest vertex
     Vertex smallest = new Vertex("foo", new Vector2(0, 0));
     smallest.Weight = int.MaxValue;
     for (int i = 0; i < vertices.Count; i++)
     {
         if (vertices[i].Weight < smallest.Weight && vertices[i].Permanent == false && vertices[i].Visited)
         {
             smallest = vertices[i];
         }
     }
     //Make smallest node permanent
     smallest.Permanent = true;
     //Highlight the shortest path
     edgeColor[vertNameToIndex[smallest.Name], vertNameToIndex[smallest.Adjacent.Name]] = HIGHLIGHT_EDGE_COLOR;
     //Add to the visitedlist
     visitedVerticesList.Add(smallest);
     return smallest;
 }
示例#4
0
        //Create a method that will examine each non-permanent node adjacent to the working node
        public void Label(Vertex current)
        {
            //Loop through the vertices in vertexList
            foreach (Vertex vert in vertices)
            {
                //If a vertex in the list matches the given room
                if (vert == current)
                {
                    //Go through the 2d array
                    for (int i = 0; i < vertices.Count; i++)
                    {
                        //If there is a connection and permanent is false
                        if (adjMatrix[vertNameToIndex[vert.Name], i] != 0 && vertices[i].Permanent == false && vertices[i].Adjacent == null)
                        {
                            //Label with total distance from the source and the name of the working node
                            //Total distance from source
                            vertices[i].Weight = adjMatrix[vertNameToIndex[vert.Name], i] + vert.Weight;

                            //Set adjacent vertex's adjacent vertex to working node
                            vertices[i].Adjacent = vert;
                            vertices[i].Visited = true;
                        }

                        //If there is a connection and if it has already been labeled
                        if (adjMatrix[vertNameToIndex[vert.Name], i] != 0 && vertices[i].Adjacent != null)
                        {
                            //Check to see if the cost computed using the working node is better
                            //than the current cost in the label
                            int newCost = adjMatrix[vertNameToIndex[vert.Name], i] + vert.Weight;
                            if (newCost < vertices[i].Weight)
                            {
                                //If so, change the label
                                //Change the total distance and current working node
                                vertices[i].Weight = newCost;
                                vertices[i].Visited = true;
                                vertices[i].Adjacent = vert;
                            }

                        }
                    }
                }
            }
        }
示例#5
0
        public void HighlightShortestPaths(Vertex v)
        {
            //Call Reset method to set all of the vertices weight to the largest int max value
            if (visitedVerticesList.Count == 0)
            {
                Reset();
            }
            //Keep doing the algorithm until it is finished
            while (Finished() != true)
            {
                //Check to see if the room is in the vertex list
                if (vertices.Contains(v))
                {
                    if (visitedVerticesList.Count == 0)
                    {
                        //Set the source vertex to the source node
                        source = v;
                        //Make source node permanent
                        source.Permanent = true;
                        source.Weight = 0;
                        //Call the label method
                        Label(source);
                    }
                    else
                    {
                        Label(current);
                    }

                }
                if (visitedVerticesList.Count == 0)
                {
                    visitedVerticesList.Add(source);
                }
                current = SmallestLabel();
            }
            //Call the clear list to clear the vertices in vistedVerticesList
            //so that the algorithm can be runned again
            visitedVerticesList.Clear();
        }
示例#6
0
 /// <summary>
 /// Adds a vertex to the graph
 /// </summary>
 /// <param name="vert">The vert to add</param>
 public void AddVertex(Vertex vert)
 {
     // Add the vertex if we're below the maximum
     if (vertices.Count < MAX_VERTICES)
     {
         vertNameToIndex.Add(vert.Name, vertices.Count);
         vertices.Add(vert);
     }
 }