private void Side_checker(struct_open_cell work_cell, string side) { int _y = 0, _x = 0, _z = 0, _h = 0; int x = work_cell.point.x; int y = work_cell.point.y; switch (side) { case "U": _y = y - 1; if (_y < 0) { return; } _x = x; break; case "D": _y = y + 1; if (_y >= draw_copy.NRowsY) { return; } _x = x; break; case "L": _x = x - 1; if (_x < 0) { return; } _y = y; break; case "R": _x = x + 1; if (_x >= draw_copy.NColumnX) { return; } _y = y; break; case "LU": _x = x - 1; _y = y - 1; if (_x < 0 || _y < 0 || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "LD": _x = x - 1; _y = y + 1; if (_x < 0 || _y >= draw_copy.NRowsY || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "RU": _x = x + 1; _y = y - 1; if (_x >= draw_copy.NColumnX || _y < 0 || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "RD": _x = x + 1; _y = y + 1; if (_x >= draw_copy.NColumnX || _y >= draw_copy.NRowsY || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; } _z = Drowing.Absol_Coord(_y, _x, draw_copy.NColumnX); if (draw_copy.Map[_z]) { return; } if (draw_copy.Closed_cell[_z] != 0) { return; } _h = hevristic_funct(_x, _y, draw_copy.Finish_point.x, draw_copy.Finish_point.y); int index_result = open_Cells.FindIndex(cell => cell.point.z.Equals(_z)); if (index_result == -1) { open_Cells.Add(new struct_open_cell(_x, _y, _z, _h, work_cell.point)); draw_copy.Processed_cells[_z] = draw_copy.Step; } }
private void Side_checker(struct_open_cell work_cell, string side) { int _y = 0, _x = 0, _z = 0, _h = 0, _f = 0; int x = work_cell.point.x; int y = work_cell.point.y; int g = work_cell.G; switch (side) { case "U": _y = y - 1; if (_y < 0) { return; } _x = x; g += 10; break; case "D": _y = y + 1; if (_y >= draw_copy.NRowsY) { return; } _x = x; g += 10; break; case "L": _x = x - 1; if (_x < 0) { return; } _y = y; g += 10; break; case "R": _x = x + 1; if (_x >= draw_copy.NColumnX) { return; } _y = y; g += 10; break; case "LU": _x = x - 1; _y = y - 1; g += 14; if (_x < 0 || _y < 0 || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "LD": _x = x - 1; _y = y + 1; g += 14; if (_x < 0 || _y >= draw_copy.NRowsY || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "RU": _x = x + 1; _y = y - 1; g += 14; if (_x >= draw_copy.NColumnX || _y < 0 || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; case "RD": _x = x + 1; _y = y + 1; g += 14; if (_x >= draw_copy.NColumnX || _y >= draw_copy.NRowsY || draw_copy.Map[Drowing.Absol_Coord(y, _x, draw_copy.NColumnX)] || draw_copy.Map[Drowing.Absol_Coord(_y, x, draw_copy.NColumnX)]) { return; } break; } _z = Drowing.Absol_Coord(_y, _x, draw_copy.NColumnX); if (draw_copy.Map[_z]) { return; } if (draw_copy.Closed_cell[_z] != 0) { return; } _h = hevristic_funct(_x, _y, draw_copy.Finish_point.x, draw_copy.Finish_point.y); _f = g + _h; //Оціночне значення яке складається з суми ваг комірок + зачення еврестичної функції int index_result = open_Cells.FindIndex(cell => cell.point.z.Equals(_z)); //перевірка чи є вже така комріка, щоб не було дуплікатів if (index_result == -1) { open_Cells.Add(new struct_open_cell(_x, _y, _z, g, _h, _f, work_cell.point)); draw_copy.Processed_cells[_z] = draw_copy.Step; } else { if (open_Cells[index_result].G > g)//Якщо комірка вже існує перервіряємо чи ми прийшли до неї з меншим G { struct_open_cell temp_add = open_Cells[index_result]; temp_add.G = g; temp_add.F = temp_add.H + g; temp_add.parent_cell = work_cell.point; open_Cells[index_result] = temp_add; } } }
public override void Path_Finding() { if (draw_copy.Start_point.z == -1 || draw_copy.Finish_point.z == -1) { MessageBox.Show("Встановіть старт та фініш", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error); form1.lock_buttons(); form1.Hide_load_form(); return; } int find_step = 0; First_Step(); bool flag_find_step = true; while (open_Cells.Count != 0) { Mode = form1.Flag_radioButtons; open_Cells.Sort((x, y) => x.H.CompareTo(y.H)); struct_open_cell work_cell = open_Cells[0]; open_Cells.RemoveAt(0); draw_copy.Closed_cell[work_cell.point.z] = work_cell.parent_cell.z; draw_copy.Check_point = work_cell.point; for (int i = 0; i < 4; i++) { Side_checker(work_cell, Mas_OF_sides[i]); } if (form1.checkBox2.Checked) { for (int i = 4; i < 8; i++) { Side_checker(work_cell, Mas_OF_sides[i]); } } if ((draw_copy.Closed_cell[draw_copy.Finish_point.z] != 0) && (draw_copy.Step != 2)) { if (flag_find_step) { find_step = draw_copy.Step; flag_find_step = false; } if (form1.checkBox1.Checked) { form1.visualisation_mode(Mode, form1, 1); open_Cells.Clear(); break; } } form1.textBox1.Text = Convert.ToString(draw_copy.Step); form1.visualisation_mode(Mode, form1, 1); draw_copy.Step++; } form1.textBox1.Text = Convert.ToString(draw_copy.Step); if (find_step != 0) { Stack <Ppoint> way_back = new Stack <Ppoint>(); way_back.Push(draw_copy.Finish_point); int previous_element = draw_copy.Closed_cell[draw_copy.Finish_point.z]; while (way_back.Peek() != draw_copy.Start_point) { int x = 0, y = 0; Drowing.Reference_to_XY(ref x, ref y, previous_element, draw_copy.NColumnX); way_back.Push(new Ppoint(x, y, previous_element)); previous_element = draw_copy.Closed_cell[previous_element]; } draw_copy.Anim.draw_way(way_back); form1.update_panel_without_redraw(); draw_copy.Show_Step(find_step); } else { form1.Hide_load_form(); MessageBox.Show("Шлях не знайдено", "Повідомелння", MessageBoxButtons.OK, MessageBoxIcon.Information); } Reset_Values(); form1.Hide_load_form(); }
public override void Path_Finding() { if (draw_copy.Start_point.z == -1 || draw_copy.Finish_point.z == -1) { MessageBox.Show("Встановіть старт та фініш", "Помилка", MessageBoxButtons.OK, MessageBoxIcon.Error); form1.lock_buttons(); form1.Hide_load_form(); return; } int find_step = 0; First_Step(); bool flag_find_step = true; while (open_Cells.Count != 0)//Початок знаходження шляху { Mode = form1.Flag_radioButtons; open_Cells.Sort((x, y) => x.F.CompareTo(y.F)); struct_open_cell work_cell = open_Cells[0]; open_Cells.RemoveAt(0); draw_copy.Closed_cell[work_cell.point.z] = work_cell.parent_cell.z;//Кладемо в текущу комірку ту з якої прийшли draw_copy.Check_point = work_cell.point; //Перевірка по 4 чи по 8 напрямкам for (int i = 0; i < 4; i++) { Side_checker(work_cell, Mas_OF_sides[i]); } if (form1.checkBox2.Checked) { for (int i = 4; i < 8; i++) { Side_checker(work_cell, Mas_OF_sides[i]); } } if ((draw_copy.Closed_cell[draw_copy.Finish_point.z] != 0) && (draw_copy.Step != 2))//Перевірка чи не кінець { if (flag_find_step) { find_step = draw_copy.Step; flag_find_step = false; } if (form1.checkBox1.Checked) { form1.visualisation_mode(Mode, form1, 1); open_Cells.Clear(); break; } } form1.textBox1.Text = Convert.ToString(draw_copy.Step); form1.visualisation_mode(Mode, form1, 1); draw_copy.Step++; } form1.textBox1.Text = Convert.ToString(draw_copy.Step); if (find_step != 0)//Побудова зворотнього шялху якщо він взвгвлі є { Stack <Ppoint> way_back = new Stack <Ppoint>(); way_back.Push(draw_copy.Finish_point); int previous_element = draw_copy.Closed_cell[draw_copy.Finish_point.z]; while (way_back.Peek() != draw_copy.Start_point)//Шлях назад { int x = 0, y = 0; Drowing.Reference_to_XY(ref x, ref y, previous_element, draw_copy.NColumnX); way_back.Push(new Ppoint(x, y, previous_element));//Накидуєтсья масив точок для результуючого шляху previous_element = draw_copy.Closed_cell[previous_element]; } draw_copy.Anim.draw_way(way_back); form1.update_panel_without_redraw(); form1.Hide_load_form(); draw_copy.Show_Step(find_step); } else { form1.Hide_load_form(); MessageBox.Show("Шлях не знайдено", "Повідомелння", MessageBoxButtons.OK, MessageBoxIcon.Information); } Reset_Values(); }