Beispiel #1
0
        static private void DijkstraSearch(Map Map)
        {
            // gán Minstart to node của node start = 0
            Map.StartNode.MinStartToNode = 0;

            // tạo hàng đợi
            var prioQueue = new List <Node>();

            // thêm điểm startNode vào hàng đợi
            prioQueue.Add(Map.StartNode);

            while (prioQueue.Any())
            {
                // sắp xếp prioQueue theo chiều tăng dần MinStartToNode
                prioQueue = prioQueue.OrderBy(x => x.MinStartToNode.Value).ToList();
                // lấy điểm đầu danh sách ra
                var node = prioQueue.First();
                // xóa khỏi node duyệt
                prioQueue.Remove(node);
                // duyệt các node liền kề với node sắp xếp theo chiều tăng dần của độ dài khoảng cách từ node tới node liền kề
                foreach (var i in node.Connecttions)
                {
                    // nếu i được xét rồi thì next
                    if (i.Visited)
                    {
                        continue;
                    }
                    // nếu i.MinStartToNode chưa đc gán or i.MinStartToNode >  node.MinStartToNode + Pointsss.Distance(node.PointsssNode, i.PointsssNode);
                    if (i.MinStartToNode == null ||
                        node.MinStartToNode + Pointsss.Distance(node.PointsssNode, i.PointsssNode) < i.MinStartToNode)
                    {
                        // gán i.MinStartToNode =  node.MinStartToNode + Pointsss.Distance(node.PointsssNode, i.PointsssNode)
                        i.MinStartToNode = node.MinStartToNode + Pointsss.Distance(node.PointsssNode, i.PointsssNode);
                        // gán điểm liền trước i = node;
                        i.NearestToStart = node;
                        // nếu i chưa thêm vào queue thì thêm i vào
                        if (!prioQueue.Contains(i))
                        {
                            prioQueue.Add(i);
                        }
                    }
                }
                // gán  node.Visited = true điểm node đã đc xét;
                node.Visited = true;
                //nếu node là điểm cuối thì return
                if (node.PointsssNode == Map.EndNode.PointsssNode)
                {
                    return;
                }
            }
        }
Beispiel #2
0
 private bool checkcatlistcanh(Pointsss P1, Pointsss P2)
 {
     if (lstcanh.Count == 0)
     {
         return(true);
     }
     foreach (var i in lstcanh)
     {
         if (checkcatnhau(P1, P2, i.diem1, i.diem2) == false)
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #3
0
        private bool checkcatnhau(Pointsss p1, Pointsss p2, Pointsss p3, Pointsss p4)
        {
            float a    = (float)(p1.Y - p2.Y) / (float)(p1.X - p2.X);
            float b    = (float)p1.Y - a * (float)p1.X;
            var   hs34 = (p3.Y - a * p3.X + b) * (p4.Y - a * p4.X + b);

            float a34  = (float)(p3.Y - p4.Y) / (float)(p3.X - p4.X);
            float b34  = (float)p3.Y - a * (float)p3.X;
            var   hs12 = (p1.Y - a34 * p1.X + b34) * (p2.Y - a34 * p2.X + b34);

            if (hs34 < -0.0001 && hs12 < -0.0001)
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
 public Node()
 {
     PointsssNode = new Pointsss();
     Connecttions = new List <Node>();
     Visited      = false;
 }
Beispiel #5
0
 public static double Distance(Pointsss A, Pointsss B)
 {
     return(Math.Sqrt(Math.Pow(A.X - B.X, 2) + Math.Pow(A.Y - B.Y, 2)));
 }
Beispiel #6
0
 public canh(Pointsss pointsssNode1, Pointsss pointsssNode2) : this()
 {
     this.diem1 = pointsssNode1;
     this.diem2 = pointsssNode2;
 }
Beispiel #7
0
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (tt != 1)
            {
                return;
            }
            int x = (e as MouseEventArgs).X;
            int y = (e as MouseEventArgs).Y;
            var P = new Pointsss();

            P.X = x;
            P.Y = y;
            if (P.Y > pictureBox1.Height || P.Y < 0 || P.X > pictureBox1.Width || P.X < 0)
            {
                return;
            }
            if (e.Button == MouseButtons.Left)
            {
                map.lstNode.ForEach(it =>
                {
                    if (Pointsss.Distance(it.PointsssNode, P) < 20)
                    {
                        it.PointsssNode.X       = P.X;
                        it.PointsssNode.Y       = P.Y;
                        it.PointsssNode.isclick = true;
                        map.lstNode.ForEach(itt =>
                        {
                            if (itt.PointsssNode != it.PointsssNode)
                            {
                                itt.PointsssNode.isclick = false;
                            }
                        });
                        draw();
                        return;
                    }
                    if (it.PointsssNode.isclick == true)
                    {
                        it.PointsssNode.X = P.X;
                        it.PointsssNode.Y = P.Y;
                        draw();
                        return;
                    }
                });
            }
            map.lstNode.ForEach(it =>
            {
                if (Pointsss.Distance(it.PointsssNode, P) < 20)
                {
                    it.PointsssNode.isclick = true;
                    map.lstNode.ForEach(itt =>
                    {
                        if (itt.PointsssNode != it.PointsssNode)
                        {
                            itt.PointsssNode.isclick = false;
                        }
                    });
                    draw();
                    return;
                }
            });
        }
Beispiel #8
0
        void draw()
        {
            Bitmap   bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
            Graphics g   = Graphics.FromImage(bmp);

            g.Clear(Color.White);



            foreach (var i in map.lstNode)
            {
                g.DrawEllipse(Pens.Red, new Rectangle((int)i.PointsssNode.X - 5, (int)i.PointsssNode.Y - 5, 2 * 5, 2 * 5));
                using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    PointF pointF1 = new PointF((int)i.PointsssNode.X - 10, (int)i.PointsssNode.Y + 10);
                    g.DrawString((i.PointsssNode.Id + 1).ToString(), font1, Brushes.Red, pointF1);
                }
                var pen = new Pen(Brushes.Gray, 3);
                if (i.Connecttions.Count != 0)
                {
                    var p1 = new PointF((float)i.PointsssNode.X, (float)i.PointsssNode.Y);
                    foreach (var j in i.Connecttions)
                    {
                        var p2  = new PointF((float)j.PointsssNode.X, (float)j.PointsssNode.Y);
                        var pdd = new PointF(19 * (p2.X - p1.X) / 20 + p1.X, 19 * (p2.Y - p1.Y) / 20 + p1.Y);
                        g.DrawLine(pen, p1, p2);
                        g.DrawLine(new Pen(Brushes.Red, 3), pdd, p2);
                        using (Font font1 = new Font("Arial", 8, FontStyle.Italic, GraphicsUnit.Pixel))
                        {
                            PointF pointF1 = new PointF((int)(i.PointsssNode.X + j.PointsssNode.X) / 2 + 10, (int)(i.PointsssNode.Y + j.PointsssNode.Y) / 2 - 10);
                            g.DrawString(Pointsss.Distance(i.PointsssNode, j.PointsssNode).ToString("#0.00") + "px", font1, Brushes.Black, pointF1);
                        }
                    }
                }
                if (i == map.StartNode)
                {
                    g.FillEllipse(Brushes.Yellow, new Rectangle((int)i.PointsssNode.X - 10, (int)i.PointsssNode.Y - 10, 2 * 10, 2 * 10));
                }
                if (i == map.EndNode)
                {
                    g.FillEllipse(Brushes.BlueViolet, new Rectangle((int)i.PointsssNode.X - 10, (int)i.PointsssNode.Y - 10, 2 * 10, 2 * 10));
                }
                if (i.PointsssNode.isclick == true)
                {
                    g.FillEllipse(Brushes.Black, new Rectangle((int)i.PointsssNode.X - 10, (int)i.PointsssNode.Y - 10, 2 * 10, 2 * 10));
                }
            }

            if (map.ShortesPath.Count > 0)
            {
                foreach (var i in map.ShortesPath)
                {
                    if (i != map.EndNode && i != map.StartNode)
                    {
                        g.FillEllipse(Brushes.Black, new Rectangle((int)i.PointsssNode.X - 5, (int)i.PointsssNode.Y - 5, 2 * 5, 2 * 5));
                    }
                }
                var pen = new Pen(Brushes.YellowGreen, 2);
                for (int i = 0; i < map.ShortesPath.Count - 1; i++)
                {
                    var p1 = new PointF((float)map.ShortesPath[i].PointsssNode.X, (float)map.ShortesPath[i].PointsssNode.Y);
                    var p2 = new PointF((float)map.ShortesPath[i + 1].PointsssNode.X, (float)map.ShortesPath[i + 1].PointsssNode.Y);
                    g.DrawLine(pen, p1, p2);
                }
            }
            foreach (var i in map.lstNode)
            {
                if (i.Connecttions.Count != 0)
                {
                    var p1 = new PointF((float)i.PointsssNode.X, (float)i.PointsssNode.Y);
                    foreach (var j in i.Connecttions)
                    {
                        var p2   = new PointF((float)j.PointsssNode.X, (float)j.PointsssNode.Y);
                        var edge = (float)Pointsss.Distance(i.PointsssNode, j.PointsssNode);
                        var pdd  = new PointF((edge - 15) * (p2.X - p1.X) / edge + p1.X, (edge - 15) * (p2.Y - p1.Y) / edge + p1.Y);
                        g.DrawLine(new Pen(Brushes.Red, 4), pdd, p2);
                    }
                }
            }

            pictureBox1.Image = bmp;
            g.Dispose();
        }