示例#1
0
        //Hiển thị độ dài đoạn thẳng
        private void btnDisplayKC_Click(object sender, EventArgs e)
        {
            // ném ngoại lệ
            if (readyPoints == false || readyEdges == false)
            {
                string            message = "Nhập đầy đủ SỐ ĐIỂM và SỐ ĐƯỜNG NỐI !";
                string            caption = "Lỗi thiếu đầu vào";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Error);
                return;
            }

            if (displayDistances == true)
            {
                displayDistances = false;
            }
            else
            {
                displayDistances = true;
            }

            ready = true;
            SanChoi.Invalidate();
            SanChoi.Refresh();
        }
示例#2
0
        // tạo các điểm
        private void btCreatePoints_Click(object sender, EventArgs e)
        {
            // Nút chỉ hoạt động nếu chương trình chưa chạy, ở chế độ khởi tạo
            if (running == true || fix == true)
            {
                return;
            }


            // ngoai lệ
            if (txtSoDiem.Text == "")
            {
                return;
            }
            if (int.Parse(txtSoDiem.Text) > 20)
            {
                string            message = "Nhập vào số điểm không quá 20";
                string            caption = "Nhập sai dữ liệu";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Error);
                return;
            }



            readyPoints        = true;
            btState1.BackColor = Color.Lime;

            numberPoints = int.Parse(txtSoDiem.Text);
            Random rd = new Random();
            int    t  = 1;

            while (true)
            {
                if (t > numberPoints)
                {
                    break;
                }
                //    TaoDiem(t);
                aPoint[t].X = rd.Next(0, 200);
                aPoint[t].Y = rd.Next(0, 200);

                t++;
            }

            ready = true;
            SanChoi.Invalidate();
            SanChoi.Refresh();
        }
示例#3
0
        private void button3_Click(object sender, EventArgs e)
        {
            // reset đầu vào tại giao diện
            txtEndPoint.Text      = "";
            txtEndPoint.Enabled   = true;
            txtSoDiem.Text        = "";
            txtSoDiem.Enabled     = true;
            txtSoDuong.Text       = "";
            txtSoDuong.Enabled    = true;
            txtStartPoint.Text    = "";
            txtStartPoint.Enabled = true;
            rtbHienThi.Text       = "";
            rtbHienThi.Enabled    = true;

            //Đưa chương trình về trạng thái ban đầu (not running)
            running = false;
            fix     = false;
            // create = true;


            // reset cơ sở dữ liệu
            ready            = false;
            readyPoints      = false;
            readyEdges       = false;
            readySP          = false;
            readyEP          = false;
            displayDistances = false;
            displayRoad      = false;

            //reset lại ma trận kề, history[]
            cstA();

            // rest lại đèn bao hiệu
            btnState2.BackColor = Color.LightGray;
            btState1.BackColor  = Color.LightGray;
            btnState3.BackColor = Color.LightGray;
            btnState4.BackColor = Color.LightGray;
            lblStatusPro.Text   = "Khởi tạo";

            SanChoi.Refresh();
        }
示例#4
0
        //Chon diem bat dau
        private void btCreateSP_Click(object sender, EventArgs e)
        {
            // Nút chỉ hoạt động nếu chương trình chưa chạy, ở chế độ khởi tạo
            if (running == true && fix == false)
            {
                return;
            }

            // bắt ngoại lệ
            if (readyPoints == false)
            {
                string            message = "Nhập và tạo SỐ ĐIỂM trước!";
                string            caption = "Lỗi thiếu đầu vào";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Error);
                return;
            }

            if (txtStartPoint.Text == "")
            {
                return;
            }


            startPoint          = int.Parse(txtStartPoint.Text);
            readySP             = true;
            btnState3.BackColor = Color.Lime;
            if (fix == true)
            {
                ChayChuongTrinh();
            }

            ready = true;
            SanChoi.Invalidate();
            SanChoi.Refresh();
        }
示例#5
0
        //panel load do thi
        private void SanChoi_Paint(object sender, PaintEventArgs e)
        {
            if (ready == false)
            {
                return;
            }
            // But dung de ve mui ten
            // Pen MuiTenPen = new Pen(Color.Red, 2);
            //MuiTenPen.StartCap = LineCap.RoundAnchor;
            //MuiTenPen.EndCap = LineCap.ArrowAnchor;


            //  bút có mũi tên
            Pen p = new Pen(Color.Blue, 2);
            AdjustableArrowCap bigArrow = new AdjustableArrowCap(5, 5);

            p.CustomEndCap = bigArrow;

            // but khong mui ten
            Pen pen = new Pen(Color.White, 1);


            Pen penStart = new Pen(Color.Blue, 4);
            Pen penEnd   = new Pen(Color.Yellow, 4);

            // but : khi chua nhap 2 diem dau va cuoi (Điểm thường)
            SolidBrush br = new SolidBrush(Color.White);

            // but : chỉ nhap diem dau
            SolidBrush brStart = new SolidBrush(Color.GreenYellow);
            // but : chỉ nhap diem cuoi
            SolidBrush brEnd = new SolidBrush(Color.Purple);

            Graphics gr = SanChoi.CreateGraphics();

            // Ve duong di truoc
            if (readyEdges == true && readyPoints == true)
            {
                for (int h = 1; h <= numberPoints; h++)
                {
                    for (int k = 1; k <= numberPoints; k++)
                    {
                        if (MTK[h, k] != 0)
                        {
                            gr.DrawLine(pen, CTD(aPoint[h]), CTD(aPoint[k]));
                        }
                    }
                }
            }

            //Diem render sau
            if (readyPoints == true)
            {
                if (readySP == true && readyEP == false)
                {
                    for (int i = 1; i <= numberPoints; i++)
                    {
                        if (i == startPoint)
                        {
                            continue;
                        }
                        gr.FillEllipse(br, CTD(aPoint[i]).X - 5, CTD(aPoint[i]).Y - 5, 25, 25);
                        gr.DrawString(i + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[i]).X, CTD(aPoint[i]).Y);
                    }
                    // vẽ điểm đầu sau những điểm thường
                    gr.FillEllipse(brStart, CTD(aPoint[startPoint]).X - 5, CTD(aPoint[startPoint]).Y - 5, 25, 25);
                    gr.DrawString(startPoint + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[startPoint]).X, CTD(aPoint[startPoint]).Y);
                }
                else if (readySP == false && readyEP == true)
                {
                    for (int i = 1; i <= numberPoints; i++)
                    {
                        if (i == endPoint)
                        {
                            continue;
                        }
                        gr.FillEllipse(br, CTD(aPoint[i]).X - 5, CTD(aPoint[i]).Y - 5, 25, 25);
                        gr.DrawString(i + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[i]).X, CTD(aPoint[i]).Y);
                    }
                    // vẽ điểm cuối sau những điểm thường
                    gr.FillEllipse(brEnd, CTD(aPoint[endPoint]).X - 5, CTD(aPoint[endPoint]).Y - 5, 25, 25);
                    gr.DrawString(endPoint + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[endPoint]).X, CTD(aPoint[endPoint]).Y);
                }
                else if (readySP == true && readyEP == true)
                {
                    for (int i = 1; i <= numberPoints; i++)
                    {
                        if (i == startPoint || i == endPoint)
                        {
                            continue;
                        }
                        gr.FillEllipse(br, CTD(aPoint[i]).X - 5, CTD(aPoint[i]).Y - 5, 25, 25);
                        gr.DrawString(i + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[i]).X, CTD(aPoint[i]).Y);
                    }
                    // vẽ điểm đầu và điểm cuối sau những điểm thường
                    gr.FillEllipse(brStart, CTD(aPoint[startPoint]).X - 5, CTD(aPoint[startPoint]).Y - 5, 25, 25);
                    gr.DrawString(startPoint + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[startPoint]).X, CTD(aPoint[startPoint]).Y);

                    gr.FillEllipse(brEnd, CTD(aPoint[endPoint]).X - 5, CTD(aPoint[endPoint]).Y - 5, 25, 25);
                    gr.DrawString(endPoint + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[endPoint]).X, CTD(aPoint[endPoint]).Y);
                }
                else
                {
                    for (int i = 1; i <= numberPoints; i++)
                    {
                        gr.FillEllipse(br, CTD(aPoint[i]).X - 5, CTD(aPoint[i]).Y - 5, 25, 25);
                        gr.DrawString(i + "", new Font("Consolas", 9), new SolidBrush(Color.Black), CTD(aPoint[i]).X, CTD(aPoint[i]).Y);
                    }
                }
            }


            if (displayRoad == true && running == true)
            {
                int tempSau = endPoint;
                while (true)
                {
                    int tempTruoc = history[tempSau].DiemDiToi;
                    gr.DrawLine(p, CTD(aPoint[tempTruoc]), CTD(aPoint[tempSau]));

                    if (tempTruoc == startPoint)
                    {
                        break;
                    }
                    tempSau = tempTruoc;
                }
                // displayRoad = false;
            }

            if (displayDistances == true)
            {
                Point tempPoint = new Point();
                for (int i = 1; i <= numberPoints; i++)
                {
                    for (int j = 1; j <= numberPoints; j++)
                    {
                        if (MTK[i, j] != 0)
                        {
                            tempPoint = TrungDiem(i, j);
                            //Hien thi do dai tai trung diem
                            gr.DrawString(MTK[i, j].ToString(), new Font("Consolas", 8), new SolidBrush(Color.DodgerBlue), CTD(tempPoint));
                        }
                    }
                }
            }
            // gr.DrawLine(MuiTenPen, CTD(aPoint[1]), CTD(aPoint[2]));

            //Graphics g = e.Graphics;
            ////g.SmoothingMode = SmoothingMode.AntiAlias;
            ////g.FillRectangle(Brushes.White, this.ClientRectangle);

            //Pen p = new Pen(Color.Black, 10);
            //p.StartCap = LineCap.Round;
            //p.EndCap = LineCap.ArrowAnchor;
            //g.DrawLine(p, 30, 30, 80, 30);
            //p.Dispose();
        }
示例#6
0
        public void ChayChuongTrinh()
        {
            // Điều kiện để chạy chương trình
            if (AllReady() == false && running == true)
            {
                string            message = "Nhập đầy đủ thông tin đầu vào";
                string            caption = "Lỗi đầu vào";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Error);
                return;
            }
            // Khóa lại không cho sửa chữa
            if (running == true)
            {
                txtEndPoint.Enabled   = false;
                txtSoDiem.Enabled     = false;
                txtSoDuong.Enabled    = false;
                txtStartPoint.Enabled = false;
            }

            // Ngăn chặn việc cac nut reset, đưa chương trình về trạng thái đang chạy
            running           = true;
            lblStatusPro.Text = "Đang chạy";


            resetHistory();

            // Khởi tạo các điểm trong history và điểm bắt đầu
            for (int i = 1; i <= numberPoints; i++)
            {
                history[i].DiemDiToi = startPoint;
            }
            history[startPoint].DoDai     = 0;
            history[startPoint].DiemDiToi = startPoint;
            history[startPoint].DaDuyet   = true;

            // xu ly
            int tempMark = startPoint;
            int sum      = 0;

            while (true)
            {
                // tinh do dai den cac diem ke cua diem hien tai tempMark
                for (int i = 1; i <= numberPoints; i++)
                {
                    if (history[i].DaDuyet == true)
                    {
                        continue;
                    }

                    // neu chua Duyet ta xem co ke hay khong
                    if (MTK[i, tempMark] != 0)
                    {
                        if (history[i].DaDuyet == false)
                        {
                            sum++;
                        }
                        if (history[i].DoDai == -1 || history[tempMark].DoDai + MTK[i, tempMark] < history[i].DoDai)
                        {
                            history[i].DoDai     = history[tempMark].DoDai + MTK[i, tempMark];
                            history[i].DiemDiToi = tempMark;
                        }
                    }
                }

                // Tim ra diem be nhat duoc chon
                //  sum = 0;
                int dMin    = 1000000;
                int tempi   = 0;
                int tiepTuc = 0;
                for (int i = 1; i <= numberPoints; i++)
                {
                    if (history[i].DaDuyet == true || history[i].DoDai == -1)
                    {
                        continue;
                    }

                    if (history[i].DoDai < dMin)
                    {
                        dMin  = history[i].DoDai;
                        tempi = i;
                        tiepTuc++;
                    }
                }

                if (tiepTuc == 0)
                {
                    break;
                }
                // danh dau va cap nhat lai diem duoc chon
                // history[tempi].DiemDiToi = tempMark;
                history[tempi].DaDuyet = true;
                tempMark = tempi;
                if (tempMark == endPoint)
                {
                    break;
                }
            } // ket thuc while

            // Dua ra ket qua

            if (running == true && fix == false)
            {
                if (history[endPoint].DoDai == -1)
                {
                    rtbHienThi.Text = "không tồn tại đường đi từ điểm " + startPoint + " tới điểm " + endPoint;
                }
                else
                {
                    rtbHienThi.Text = "Độ dài nhỏ nhất từ điểm " + startPoint + " đến điểm " + endPoint + " là: " + history[endPoint].DoDai;
                    displayRoad     = true;
                }
            }


            // nếu chương trình đang ở chế độ chạy để sửa ta sẽ đổi lại running
            if (fix == true)
            {
                running = false;
            }
            SanChoi.Invalidate();
            SanChoi.Refresh();
        }
示例#7
0
        // creat cac canh (edges)
        private void btCreateEs_Click(object sender, EventArgs e)
        {
            // Nút chỉ hoạt động nếu chương trình chưa chạy, ở chế độ khởi tạo
            if (running == true)
            {
                return;
            }

            //bắt ngoại lệ
            if (readyPoints == false)
            {
                string            message = "Nhập và khởi tạo SỐ ĐIỂM trước !";
                string            caption = "Lỗi thiếu đầu vào";
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult      result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons, MessageBoxIcon.Error);
                return;
            }
            // ngoai lệ
            if (txtSoDuong.Text == "")
            {
                return;
            }



            Edges = int.Parse(txtSoDuong.Text);
            // khoi tao lai ma tran ke
            cstA();
            Random rd2 = new Random();
            int    x, y, d;
            // Cac diem dau va cuoi khong trung nhau, canh k dc lap lai
            int t = 0;

            while (true)
            {
                if (t == Edges)
                {
                    break;
                }
                x = rd2.Next(1, numberPoints + 1); // Ham random(x,y) chỉ chay tu x-> (y-1) nên cộng thêm 1 đơn vị
                y = rd2.Next(1, numberPoints + 1);
                if (MTK[x, y] != 0 || x == y)
                {
                    continue;
                }
                d         = KhoangCach(x, y);
                MTK[x, y] = d;
                MTK[y, x] = d;
                t++;
            }

            readyEdges          = true;
            btnState2.BackColor = Color.Lime;

            // nếu trạng thái chương trình là đang sửa thì chạy lại thuật toán
            if (fix == true)
            {
                ChayChuongTrinh();
            }

            ready = true;
            SanChoi.Invalidate();
            SanChoi.Refresh();
        }