public override void Undo(ElecMap elc) { if (elc.mapnode[X, Y].IsAbleCross == false) { return; } switch (Dir) { case Direction.Down: elc.mapnode[X, Y].DownDifficulty = downDifficulty; break; case Direction.Up: elc.mapnode[X, Y].UpDifficulty = upDifficulty; break; case Direction.Right: elc.mapnode[X, Y].RightDifficulty = rightDifficulty; break; case Direction.Left: elc.mapnode[X, Y].LeftDifficulty = leftDifficulty; break; default: break; } }
public override void Execute(ElecMap elc) { if (elc.mapnode[X, Y].IsAbleCross == false) { return; } downDifficulty = elc.mapnode[X, Y].DownDifficulty; upDifficulty = elc.mapnode[X, Y].UpDifficulty; leftDifficulty = elc.mapnode[X, Y].LeftDifficulty; rightDifficulty = elc.mapnode[X, Y].RightDifficulty; switch (Dir) { case Direction.Up: elc.mapnode[X, Y].UpDifficulty = MapNode.UNABLE_PASS; break; case Direction.Down: elc.mapnode[X, Y].DownDifficulty = MapNode.UNABLE_PASS; break; case Direction.Left: elc.mapnode[X, Y].LeftDifficulty = MapNode.UNABLE_PASS; break; case Direction.Right: elc.mapnode[X, Y].RightDifficulty = MapNode.UNABLE_PASS; break; default: break; } }
public List <MyPoint> Search(ElecMap elc, List <MyPoint> scannerNode, List <MyPoint> lockNode, int beginX, int beginY, int endX, int endY, Direction direction, IAlgorithm algorithm) { this.algorithm = algorithm; Height = elc.HeightNum; Width = elc.WidthNum; // ChangeMap(elc, width, height); // 转换寻找路径的可达还是不可达 initGraph(elc, scannerNode, lockNode, beginX, beginY, endX, endY, direction); List <MyPoint> route = algorithm.Search(graph, beginX, beginY, endX, endY, direction); if (route.Count < 1) { lockNode.Clear(); initGraph(elc, scannerNode, lockNode, beginX, beginY, endX, endY, direction); route = algorithm.Search(graph, beginX, beginY, endX, endY, direction); } //close = new Close[Height, Width]; //initClose(close, beginX, beginY, endX, endY); //close[beginX, beginY].Node.isSearched = true; //Dstar dstar = new Dstar(); //route = dstar.SearchDstar(close, beginX, beginY, endX, endY, beginDir); //if (route != null && route.Count > 0) //{ // route.Insert(0,new MyPoint(beginX, beginY)); //} SearchProcess.SetGraph(graph, route, beginY, beginX, endY, endX); return(route); }
public override void Undo(ElecMap elc) { elc.mapnode[X, Y].IsAbleCross = isAbleCross; elc.mapnode[X, Y].DownDifficulty = downDifficulty; elc.mapnode[X, Y].UpDifficulty = upDifficulty; elc.mapnode[X, Y].RightDifficulty = rightDifficulty; elc.mapnode[X, Y].LeftDifficulty = leftDifficulty; }
public static MyPoint RandRealPoint(ElecMap Elc) { int x = rand.Next(0, Elc.HeightNum - 1); int y = rand.Next(0, Elc.WidthNum - 1); while (!Elc.IsNodeAvailable(x, y)) { x = rand.Next(0, Elc.HeightNum - 1); y = rand.Next(0, Elc.WidthNum - 1); } return(new MyPoint(x, y, Direction.Left)); }
public override void Execute(ElecMap elc) { downDifficulty = elc.mapnode[X, Y].DownDifficulty; upDifficulty = elc.mapnode[X, Y].UpDifficulty; leftDifficulty = elc.mapnode[X, Y].LeftDifficulty; rightDifficulty = elc.mapnode[X, Y].RightDifficulty; isAbleCross = elc.mapnode[X, Y].IsAbleCross; elc.mapnode[X, Y].IsAbleCross = true; elc.mapnode[X, Y].DownDifficulty = MapNode.DEFAULT_DIFFICULTY; elc.mapnode[X, Y].UpDifficulty = MapNode.DEFAULT_DIFFICULTY; elc.mapnode[X, Y].RightDifficulty = MapNode.DEFAULT_DIFFICULTY; elc.mapnode[X, Y].LeftDifficulty = MapNode.DEFAULT_DIFFICULTY; }
public override void Execute(ElecMap elc) { downDifficulty = elc.mapnode[X, Y].DownDifficulty; upDifficulty = elc.mapnode[X, Y].UpDifficulty; leftDifficulty = elc.mapnode[X, Y].LeftDifficulty; rightDifficulty = elc.mapnode[X, Y].RightDifficulty; isAbleCross = elc.mapnode[X, Y].IsAbleCross; elc.mapnode[X, Y].IsAbleCross = true; elc.mapnode[X, Y].DownDifficulty = MapNode.UNABLE_PASS; elc.mapnode[X, Y].UpDifficulty = MapNode.UNABLE_PASS; elc.mapnode[X, Y].RightDifficulty = MapNode.UNABLE_PASS; elc.mapnode[X, Y].LeftDifficulty = MapNode.UNABLE_PASS; }
/// <summary> /// 把小车所在的节点设为占用状态 /// </summary> public static void VehicleOcuppyNode(ElecMap Elc, Vehicle[] vehicle) { for (int p = 0; p < Elc.HeightNum; p++) { for (int q = 0; q < Elc.WidthNum; q++) { Elc.mapnode[p, q].NodeCanUsed = -1; } } int count = vehicle.Length; for (int i = 0; i < count; i++) { Elc.mapnode[(vehicle[i].BeginX), (vehicle[i].BeginY)].NodeCanUsed = vehicle[i].Id; } }
public static MyPoint RandRealPoint(ElecMap Elc) { int minX = ConstDefine.minX; int maxX = ConstDefine.maxX; int minY = ConstDefine.minY; int maxY = ConstDefine.maxY; int x = rand.Next(minX, maxX + 1); int y = rand.Next(minY, maxY + 1); while (!Elc.IsNodeAvailable(x, y)) { x = rand.Next(minX, maxX); y = rand.Next(minY, maxY); } return(new MyPoint(x, y)); }
public List <myPoint> search(ElecMap elc, int width, int height, int firstX, int firstY, int endX, int endY) { ChangeMap(elc, width, height); // 转换寻找路径的可达还是不可达 initGraph(map, firstX, firstY, endX, endY); List <myPoint> route = new List <myPoint>(); printShortest(route); //printMap(); //while (true) //{ // Console.WriteLine("请输入起始地点坐标和目标地点坐标,中间以空格隔开 如1 1 3 4"); // string[] values = Console.ReadLine().Split(' '); // srcX = int.Parse(values[0]); // srcY = int.Parse(values[1]); // dstX = int.Parse(values[2]); // dstY = int.Parse(values[3]); // if (within(srcX, srcY) && within(dstX, dstY)) // { // if ((shortestep = printShortest()) > 0) // { // Console.WriteLine("从({0},{1})到({2},{3})的最短步数是: {4}", // srcX, srcY, dstX, dstY, shortestep); // printMap(); // clearMap(); // // int times= bfs(); // //printDepth(); // // Console.WriteLine("shortestep={0}, close[dstX, dstY].G={1}",times,close[dstX, dstY].G); // // Console.WriteLine((shortestep == close[dstX, dstY].G) ? "正确" : "错误"); // // clearMap(); // } // else // { // Console.WriteLine("从({0},{1})不可到达({2},{3}", // srcX, srcY, dstX, dstY); // } // } // else // { // Console.Write("输入错误!"); // } // return(route); // } }
// public List< MyPoint> search(ElecMap elc, int width, int height, int firstX, int firstY, int endX, int endY,Direction direction) public List <MyPoint> Search(ElecMap elc, List <MyPoint> scannerNode, List <MyPoint> lockNode, int v_num, int width, int height, int firstX, int firstY, int endX, int endY, Direction direction) // public List<MyPoint> Search(ElecMap elc, List<MyPoint> scannerNode, ConcurrentQueue<MyPoint> lockNode, int v_num, int width, int height, int firstX, int firstY, int endX, int endY, Direction direction) { // ChangeMap(elc, width, height); // 转换寻找路径的可达还是不可达 initGraph(elc, scannerNode, lockNode, v_num, firstX, firstY, endX, endY, direction); List <MyPoint> route = new List <MyPoint>(); SearchRoute(route); if (route.Count < 1) { lockNode.Clear(); initGraph(elc, scannerNode, lockNode, v_num, firstX, firstY, endX, endY, direction); route = new List <MyPoint>(); SearchRoute(route); } return(route); // } }
public static void SaveDesignedMap(ElecMap Elc) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "保存地图"; sfd.InitialDirectory = @"C:\"; sfd.Filter = "文件| *.xml"; sfd.FileName = "ElcMap"; sfd.ShowDialog(); string path = sfd.FileName; if (path == "") { return; } CreateXmlFile(Elc, path); }
public void ChangeMap(ElecMap elc, int width, int height) { //电子地图的长、宽被分割的个数 Height = elc.heightNum; Width = elc.widthNum; //Width = width; //Height = height; map = new int[Height, Width]; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { if (elc.TempMapNode[i, j].Node_Type == true && elc.TempMapNode[i, j].nodeCanUsed == true) { map[i, j] = 0; } else { map[i, j] = 1; } } } }
public abstract void Execute(ElecMap elc);
public override void Execute(ElecMap elc) { }
public override void Undo(ElecMap elc) { }
private static void CreateXmlFile(ElecMap elc, string path) { XmlDocument xmlDoc = new XmlDocument(); //创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?> XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.AppendChild(declaration); //创建根节点 XmlNode configRoot = xmlDoc.CreateElement("config"); XmlNode mapRoot = xmlDoc.CreateElement("Map"); configRoot.AppendChild(mapRoot); XmlNode gridRoot = xmlDoc.CreateElement("Grid"); configRoot.AppendChild(gridRoot); xmlDoc.AppendChild(configRoot); CreateNode(xmlDoc, mapRoot, "Heightnum", elc.HeightNum + "", null); CreateNode(xmlDoc, mapRoot, "Widthnum", elc.WidthNum + "", null); for (int i = 0; i < elc.HeightNum; i++) { for (int j = 0; j < elc.WidthNum; j++) { char[] dir = new char[4] { '0', '0', '0', '0' }; if (elc.mapnode[i, j].UpDifficulty < MapNode.UNABLE_PASS) { dir[0] = '1'; } if (elc.mapnode[i, j].DownDifficulty < MapNode.UNABLE_PASS) { dir[1] = '1'; } if (elc.mapnode[i, j].LeftDifficulty < MapNode.UNABLE_PASS) { dir[2] = '1'; } if (elc.mapnode[i, j].RightDifficulty < MapNode.UNABLE_PASS) { dir[3] = '1'; } string direction = "0000"; if (elc.mapnode[i, j].IsAbleCross) { direction = new string(dir); } CreateNode(xmlDoc, gridRoot, "td" + i + "-" + j, elc.mapnode[i, j].Type.ToString(), direction); } } try { //保存Xml文档 xmlDoc.Save(path); MessageBox.Show("保存成功"); }catch (Exception ex) { MessageBox.Show("保存失败:" + ex.ToString()); } }
void initGraph(ElecMap elc, List <MyPoint> scanner, List <MyPoint> lockNode, int v_num, int sx, int sy, int dx, int dy, Direction direction) // public void initGraph(ElecMap elc, List<MyPoint> scanner,ConcurrentQueue<MyPoint> lockNode, int v_num, int sx, int sy, int dx, int dy, Direction direction) { //地图发生变化时重新构造地 int i, j; srcX = sx; //起点X坐标 srcY = sy; //起点Y坐标 dstX = dx; //终点X坐标 dstY = dy; //终点Y坐标 this.searchDir = direction; //switch (direction) //{ // case Direction.East: // this.searchDir = 0; // break; // case Direction.South: // this.searchDir = 2; // break; // case Direction.West: // this.searchDir = 4; // break; // case Direction.North: // this.searchDir =6; // break; //} //电子地图的长、宽被分割的个数 Height = elc.HeightNum; Width = elc.WidthNum; //Width = width; //Height = height; graph = new Node[Height, Width]; for (i = 0; i < Height; i++) { for (j = 0; j < Width; j++) { graph[i, j] = new Node { }; int value = 1; if (elc.mapnode[i, j].IsAbleCross == true) {//&&elc.mapnode[i, j].LockNode != v_num){ value = 0; } graph[i, j].value = value; //&&elc.mapnode[i,j].NodeCanUsed==-1 graph[i, j].x = i; //地图坐标X graph[i, j].y = j; //地图坐标Y graph[i, j].node_Type = (graph[i, j].value == Reachable); // 节点可到达性 graph[i, j].adjoinNodeCount = 0; //邻接节点个数 graph[i, j].traCongesIntensity = elc.mapnode[i, j].TraCongesIntensity; graph[i, j].leftDifficulty = elc.mapnode[i, j].LeftDifficulty; graph[i, j].rightDifficulty = elc.mapnode[i, j].RightDifficulty; graph[i, j].upDifficulty = elc.mapnode[i, j].UpDifficulty; graph[i, j].downDifficulty = elc.mapnode[i, j].DownDifficulty; } } if (NodeDirCount(sx, sy) <= lockNode.Count) { lockNode.Remove(lockNode[0]); } for (int index = 0; index < lockNode.Count; index++) { graph[lockNode[index].X, lockNode[index].Y].node_Type = false; } //Parallel.For(0, lockNode.Count, (int ii) => //{ // MyPoint point = null; // if(lockNode.TryPeek(out point)){ // graph[point.X, point.Y].node_Type = false; // } //}); for (int index = 0; index < scanner.Count; index++) { graph[scanner[index].X, scanner[index].Y].node_Type = false; } for (i = 0; i < Height; i++) { for (j = 0; j < Width; j++) { if ((!graph[i, j].node_Type) && (i != srcX && j != srcY))//&&(i!=srcX&&j!=srcY)即使起点不可达也计算它的邻接点数sur { continue; } if (j > 0) { if (graph[i, j - 1].node_Type && graph[i, j].leftDifficulty < Node.MAX_ABLE_PASS) // left节点可以到达 { graph[i, j].adjoinNodeCount |= Left; // graph[i, j - 1].adjoinNodeCount |= RightDifficulty; } //if (i > 0) //{ // if (graph[i - 1, j - 1].Node_Type // && graph[i - 1, j].Node_Type // && graph[i, j - 1].Node_Type) // up-left节点可以到达 // { // graph[i, j].sur |= North_West; // graph[i - 1, j - 1].sur |= South_East; // } //} } if (j < Width - 1) { if (graph[i, j + 1].node_Type && graph[i, j].rightDifficulty < Node.MAX_ABLE_PASS) // right节点可以到达 { graph[i, j].adjoinNodeCount |= Right; // graph[i, j - 1].adjoinNodeCount |= RightDifficulty; } //if (i > 0) //{ // if (graph[i - 1, j - 1].Node_Type // && graph[i - 1, j].Node_Type // && graph[i, j - 1].Node_Type) // up-left节点可以到达 // { // graph[i, j].sur |= North_West; // graph[i - 1, j - 1].sur |= South_East; // } //} } if (i > 0) { if (graph[i - 1, j].node_Type && graph[i, j].upDifficulty < Node.MAX_ABLE_PASS) // up节点可以到达 { graph[i, j].adjoinNodeCount |= Up; // graph[i - 1, j].adjoinNodeCount |= DownDifficulty; } //if (j < Width - 1) //{ // if (graph[i - 1, j + 1].Node_Type // && graph[i - 1, j].Node_Type // && map[i, j + 1] == Reachable) // up-right节点可以到达 // { // graph[i, j].sur |= North_East; // graph[i - 1, j + 1].sur |= South_West; // } //} } if (i < Height - 1) { if (graph[i + 1, j].node_Type && graph[i, j].downDifficulty < Node.MAX_ABLE_PASS) // down节点可以到达 { graph[i, j].adjoinNodeCount |= Down; // graph[i - 1, j].adjoinNodeCount |= DownDifficulty; } //if (j < Width - 1) //{ // if (graph[i - 1, j + 1].Node_Type // && graph[i - 1, j].Node_Type // && map[i, j + 1] == Reachable) // up-right节点可以到达 // { // graph[i, j].sur |= North_East; // graph[i - 1, j + 1].sur |= South_West; // } //} } } } }
void initGraph(ElecMap elc, List <MyPoint> scanner, List <MyPoint> lockNode, int beginX, int beginY, int endX, int endY, Direction direction) // public void initGraph(ElecMap elc, List<MyPoint> scanner,ConcurrentQueue<MyPoint> lockNode, int v_num, int sx, int sy, int dx, int dy, Direction direction) { //地图发生变化时重新构造地 int i, j; //Width = width; //Height = height; graph = new Node[Height, Width]; for (i = 0; i < Height; i++) { for (j = 0; j < Width; j++) { graph[i, j] = new Node { }; graph[i, j].x = i; //地图坐标X graph[i, j].y = j; //地图坐标Y graph[i, j].node_Type = elc.mapnode[i, j].IsAbleCross; // 节点可到达性 graph[i, j].adjoinNodeCount = 0; //邻接节点个数 graph[i, j].traCongesIntensity = elc.mapnode[i, j].TraCongesIntensity; graph[i, j].leftDifficulty = elc.mapnode[i, j].LeftDifficulty; graph[i, j].rightDifficulty = elc.mapnode[i, j].RightDifficulty; graph[i, j].upDifficulty = elc.mapnode[i, j].UpDifficulty; graph[i, j].downDifficulty = elc.mapnode[i, j].DownDifficulty; } } if (NodeDirCount(beginX, beginY) <= lockNode.Count && lockNode.Count > 0) { lockNode.Remove(lockNode[0]); } for (int index = 0; index < lockNode.Count; index++) { graph[lockNode[index].X, lockNode[index].Y].node_Type = false; } //for (int index = 0; index < scanner.Count; index++) //{ // graph[scanner[index].X, scanner[index].Y].node_Type = false; //} for (i = 0; i < Height; i++) { for (j = 0; j < Width; j++) { if ((!graph[i, j].node_Type) && (i != beginX && j != beginY))//&&(i!=srcX&&j!=srcY)即使起点不可达也计算它的邻接点数sur { continue; } if (j > 0) { if (graph[i, j - 1].node_Type && graph[i, j].leftDifficulty < Node.MAX_ABLE_PASS) // left节点可以到达 { graph[i, j].adjoinNodeCount |= Left; } } if (j < Width - 1) { if (graph[i, j + 1].node_Type && graph[i, j].rightDifficulty < Node.MAX_ABLE_PASS) // right节点可以到达 { graph[i, j].adjoinNodeCount |= Right; } } if (i > 0) { if (graph[i - 1, j].node_Type && graph[i, j].upDifficulty < Node.MAX_ABLE_PASS) // up节点可以到达 { graph[i, j].adjoinNodeCount |= Up; } } if (i < Height - 1) { if (graph[i + 1, j].node_Type && graph[i, j].downDifficulty < Node.MAX_ABLE_PASS) // down节点可以到达 { graph[i, j].adjoinNodeCount |= Down; } } } } }
public override void Execute(ElecMap elc) { elc.mapnode[X, Y].IsAbleCross = false; }
public override void Undo(ElecMap elc) { elc.mapnode[X, Y].IsAbleCross = true; }
public MapControl(ElecMap elc) { this.elc = elc; doneCommand = new Stack <List <BaseCommand> >(); undoCommand = new Stack <List <BaseCommand> >(); }
public abstract void Undo(ElecMap elc);