Exemple #1
0
        public void testRandomMap()
        {
            List <IPosition_Connected_Edit> list = RandomIntPositions.generateRandomIntPositions(10, 10, 50, 2, 4, 16, 0.6, 0.3, 0.1);
            IPosition_Connected_Edit        adj;
            StringBuilder sb = new StringBuilder();

            //Console.WriteLine(open.Count);
            sb.AppendLine(list.Count.ToString());
            foreach (IPosition_Connected_Edit p in list)
            {
                //Console.Write(p.ToString()+"\t");
                sb.Append(p.ToString() + "\t");
                IPositionSet_Connected_AdjacencyEdit adjSet = p.GetAdjacencyPositionSetEdit();
                adjSet.InitToTraverseSet();
                while (adjSet.NextPosition())
                {
                    adj = adjSet.GetPosition_Connected_Edit();
                    //Console.Write(adj.ToString() + ":" + p.GetDistanceToAdjacency().ToString() + "\t");
                    sb.Append(adj.ToString() + ":" + adjSet.GetDistanceToAdjacency().ToString() + "\t");
                }
                //Console.WriteLine();
                sb.AppendLine();
            }
            Console.WriteLine(sb.ToString());
        }
Exemple #2
0
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            IPosition_Connected_Edit p, adj;
            StringBuilder            sb = new StringBuilder();

            sb.AppendLine("Position:\tAdjacency:");
            for (int i = 0; i < mapWidth; i++)
            {
                for (int j = 0; j < mapHeight; j++)
                {
                    if (map.Exists(i, j))
                    {
                        p = map.GetPosition(i, j);
                        sb.Append(p.ToString());
                        sb.Append("\t");
                        IPositionSet_Connected_AdjacencyEdit adjSet = p.GetAdjacencyPositionSetEdit();
                        adjSet.InitToTraverseSet();
                        while (adjSet.NextPosition())
                        {
                            adj = adjSet.GetPosition_Connected_Edit();
                            sb.Append(adj.ToString() + ":" + adjSet.GetDistanceToAdjacency().ToString() + "\t");
                        }
                        sb.AppendLine();
                    }
                }
            }
            IPosition_Connected start = map.GetStartPosition();
            IPosition_Connected end   = map.GetEndPosition();

            if (start != null)
            {
                sb.Append("Start position:" + start.ToString());
            }
            if (end != null)
            {
                sb.Append("End position:" + end.ToString());
            }
            sb.AppendLine(SearchForPath(astar, false));
            if (path != null)
            {
                sb.AppendLine();
                sb.AppendLine("Path length:" + path.Count);
                foreach (IPosition_Connected iter in path)
                {
                    sb.Append(iter.ToString() + iter.GetAttachment().ToString() + "\t");
                }
            }
            infoForm.SetText(sb.ToString());
            infoForm.Show();
            infoForm.Activate();
            if (colorThePathToolStripMenuItem.Checked)
            {
                drawMap();
            }
        }
Exemple #3
0
        public void RemovePosition_Connected(IPosition_Connected p)
        {
            IPositionSet_Connected_AdjacencyEdit adjSet = ((IPosition_Connected_Edit)p).GetAdjacencyPositionSetEdit();

            adjSet.InitToTraverseSet();
            while (adjSet.NextPosition())
            {
                IPosition_Connected_Edit adj = adjSet.GetPosition_Connected_Edit();
                adj.GetAdjacencyPositionSetEdit().RemoveAdjacency(p);
                adjSet.RemoveAdjacency(adj);
            }
            list.Remove(p);
        }
Exemple #4
0
 public bool IsConnected(IPosition_Connected_Edit p1, IPosition_Connected_Edit p2)
 {
     if (p1 != null && p2 != null)
     {
         IPositionSet_Connected_AdjacencyEdit adjSet = p1.GetAdjacencyPositionSetEdit();
         adjSet.InitToTraverseSet();
         while (adjSet.NextPosition())
         {
             if (p2.Equals(adjSet.GetPosition_Connected_Edit()))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemple #5
0
        public void RemovePosition(float x, float y)
        {
            IPosition_Connected_Edit p = FindPosition(x, y);

            if (p != null)
            {
                mapAry[(int)x, (int)y].Remove(p);
                list.Remove(p);

                IPositionSet_Connected_AdjacencyEdit adjSet = p.GetAdjacencyPositionSetEdit();
                adjSet.InitToTraverseSet();
                while (adjSet.NextPosition())
                {
                    IPosition_Connected_Edit adj = adjSet.GetPosition_Connected_Edit();
                    adj.GetAdjacencyPositionSetEdit().RemoveAdjacency(p);
                    adjSet.RemoveAdjacency(adj);
                }
            }
        }
Exemple #6
0
        protected void drawMap()
        {
            buffer.Clear(panel1.BackColor);

            IPosition_Connected_Edit adj;

            //先画节点间连接,再画节点,让节点覆盖在连接上,看起来比较清晰
            foreach (IPosition_Connected_Edit p in map.GetPositionList())
            {
                IPositionSet_Connected_AdjacencyEdit adjSet = p.GetAdjacencyPositionSetEdit();
                adjSet.InitToTraverseSet();
                while (adjSet.NextPosition())
                {
                    adj = adjSet.GetPosition_Connected_Edit();
                    drawConnection(linePen, p.GetX(), p.GetY(), adj.GetX(), adj.GetY());
                }
            }
            foreach (IPosition_Connected_Edit p in map.GetPositionList())
            {
                drawNode(p.GetX(), p.GetY());
            }

            refreshTags();
        }
        public void TraversalEveryLevelAndBuild(IM2MStructure m2mStructure)
        {
            IPart rootPart = m2mStructure.GetLevel(0).GetPartRefByPartIndex(0, 0);

            IPositionSet bottonLevelPositionSet = m2mStructure.GetBottonLevelPositionSetByAncestorPart(rootPart, 0);

            bottonLevelPositionSet.InitToTraverseSet();

            while (bottonLevelPositionSet.NextPosition())
            {
                ((IPosition_Connected)(bottonLevelPositionSet.GetPosition())).SetAttachment(new Tag_M2M_Position());
            }

            //遍历每一层
            for (int levelSequence = m2mStructure.GetLevelNum() - 1; levelSequence >= 0; levelSequence--)
            {
                Queue <PositionAndPositon> partAndPositonQueue = new Queue <PositionAndPositon>();

                bool isPart = true;

                ILevel       currentLevel             = m2mStructure.GetLevel(levelSequence);
                IPositionSet currrentLevelPositionSet = null;

                //如果处于最底层
                if (levelSequence == m2mStructure.GetLevelNum() - 1)
                {
                    isPart = false;
                }

                if (levelSequence == 0)
                {
                    IPositionSetEdit temp = new Position_Implement.PositionSetEdit_ImplementByICollectionTemplate();
                    temp.AddPosition(rootPart);
                    currrentLevelPositionSet = temp;
                }
                else
                {
                    currrentLevelPositionSet = m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0);
                }

                currrentLevelPositionSet.InitToTraverseSet();

                //遍历每一个分块
                while (currrentLevelPositionSet.NextPosition())
                {
                    List <IPart_Connected> SubPartList = new List <IPart_Connected>();

                    IPart_Multi currentMultiPart = (IPart_Multi)(currrentLevelPositionSet.GetPosition());

                    //计算part的边界
                    float minSequenceX;
                    float maxSequenceX;
                    float minSequenceY;
                    float maxSequenceY;
                    if (isPart)
                    {
                        float unitNumInLength = (int)(m2mStructure.GetLevel(levelSequence + 1).GetUnitNumInHeight() / currentLevel.GetUnitNumInHeight());
                        minSequenceX = currentMultiPart.GetX() * unitNumInLength;
                        maxSequenceX = minSequenceX + unitNumInLength;
                        minSequenceY = currentMultiPart.GetY() * unitNumInLength;
                        maxSequenceY = minSequenceY + unitNumInLength;
                    }
                    else
                    {
                        minSequenceX = currentLevel.ConvertPartSequenceXToRealValue(currentMultiPart.GetX());
                        maxSequenceX = minSequenceX + currentLevel.GetGridWidth();
                        minSequenceY = currentLevel.ConvertPartSequenceYToRealValue(currentMultiPart.GetY());
                        maxSequenceY = minSequenceY + currentLevel.GetGridHeight();
                    }

                    IPositionSet childPositionSetOfCurrentPart = m2mStructure.GetChildPositionSetByParentPart(levelSequence, currentMultiPart);
                    childPositionSetOfCurrentPart.InitToTraverseSet();

                    //遍历每一个子分块或点
                    while (childPositionSetOfCurrentPart.NextPosition())
                    {
                        if (isPart)
                        {
                            IPart_Multi childPartMulti = (IPart_Multi)childPositionSetOfCurrentPart.GetPosition();
                            foreach (IPosition_Connected currentPosition_Connected in childPartMulti.GetSubPartSet())
                            {
                                BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected);
                            }
                        }
                        else
                        {
                            IPosition_Connected currentPosition_Connected = (IPosition_Connected)childPositionSetOfCurrentPart.GetPosition();
                            BFSInOnePoint(partAndPositonQueue, isPart, SubPartList, currentMultiPart, minSequenceX, maxSequenceX, minSequenceY, maxSequenceY, currentPosition_Connected);
                        }
                    }
                    //遍历每一个子分块结束

                    currentMultiPart.SetSubPartSet(SubPartList);
                }
                //遍历完某层的每个分块

                //处理还没建立链接的点和分块对
                foreach (PositionAndPositon pap in partAndPositonQueue)
                {
                    IPosition_Connected position_Connected = pap.position;
                    IPart_Connected     currentParentPart  = GetParentPart(position_Connected, isPart);
                    IPart_Connected     currentParentPart2 = GetParentPart(pap.positionInExistPart, isPart);

                    float distance = CalculateTheDistanceBetweenTwoPosition(currentParentPart2, currentParentPart);

                    IPositionSet_Connected_AdjacencyEdit positionSet_Connected_Adjacency = currentParentPart2.GetAdjacencyPositionSetEdit();

                    bool isContain = false;

                    positionSet_Connected_Adjacency.InitToTraverseSet();
                    while (positionSet_Connected_Adjacency.NextPosition())
                    {
                        if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart)
                        {
                            isContain = true;
                            break;
                        }
                    }

                    if (isContain)
                    {
                        continue;
                    }
                    else
                    {
                        positionSet_Connected_Adjacency.AddAdjacency(currentParentPart, distance);
                    }

                    positionSet_Connected_Adjacency = currentParentPart.GetAdjacencyPositionSetEdit();

                    isContain = false;

                    positionSet_Connected_Adjacency.InitToTraverseSet();
                    while (positionSet_Connected_Adjacency.NextPosition())
                    {
                        if (positionSet_Connected_Adjacency.GetPosition() == currentParentPart2)
                        {
                            isContain = true;
                            break;
                        }
                    }

                    if (isContain)
                    {
                        continue;
                    }
                    else
                    {
                        positionSet_Connected_Adjacency.AddAdjacency(currentParentPart2, distance);
                    }
                }

                #region code for algorithm demo
                if (GetPartSetInSpecificLevel != null)
                {
                    GetPartSetInSpecificLevel(m2mStructure.GetLevel(levelSequence), levelSequence, m2mStructure.GetDescendentPositionSetByAncestorPart(levelSequence, rootPart, 0));
                }
                #endregion
            }
            //遍历每一层结束
        }