/// <summary> /// 确定 x点所在y点的方向 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public static Direction Calc(PointInt x, PointInt y) { int i = x.X - y.X; int j = x.Y - y.Y; Direction d = Direction.Unkonwn; if (i < 0) { d = d | Direction.W; } if (i > 0) { d = d | Direction.E; } if (j < 0) { d = d | Direction.N; } if (j > 0) { d = d | Direction.S; } return(d); }
static bool Checkff(byte[][] buffer, SearchTree tree, PointInt max) { if (!Mark) { return(false); } int c = 0; var em = tree.Yiled(0); while (em.MoveNext()) { var cur = em.Current; var loc = cur.Curent; if (!loc.Vaild(max)) { continue; } if (buffer[loc.X][loc.Y] == 4) { c++; if (c >= 2) { return(true); } } } return(false); }
public bool Vaild(PointInt d) { if (this.X < 0 || this.X > d.X || this.Y < 0 || Y > d.Y) { return(false); } return(true); }
public static byte[][] GetMatrixPath(byte[][] origin, PointInt loc, PointInt des, Action <byte[][]> callback) { int wait = 100; if (callback == null) { wait = 0; callback = (o) => { }; } var buffer = CopyData(origin); var max = new PointInt(buffer.Length - 1, buffer[0].Length - 1); if (!CalcPath(buffer, loc, des, max, callback, wait)) { return(buffer); } return(buffer); }
public SearchTree(PointInt cur, int _depth) { this.Curent = cur; this._depth = _depth; }
private static bool CalcPath(byte[][] buffer, PointInt loc, PointInt des, PointInt max, Action <byte[][]> callback, int wait) { var root = new SearchTree(loc, 0); SearchTree opt = root; var next = opt; while (true) { if (next == null) { return(false); } loc = next.Curent; if (!loc.Vaild(max)) { //遇到墙壁,处理区域点忽略 next = next.Parent; goto endLab; } int i = des.X - loc.X; int j = des.Y - loc.Y; int status = i >= j ? 1 : 2; if (i == j && j == 0) { buffer[loc.X][loc.Y] = 4; DoCallback(callback, buffer, wait); //到达终点寻址完毕 return(true); } if (buffer[loc.X][loc.Y] != 0) { //寻址失败上层回溯 next = next.Parent; goto endLab; } else { //检查当前点的四个方向是否有通路 if (Checkff(buffer, next, max)) { buffer[loc.X][loc.Y] = 4; DoCallback(callback, buffer, wait); buffer[loc.X][loc.Y] = 0; DoCallback(callback, buffer, wait); next = next.Parent; //如果有则表示出现闭塞回路 goto endLab; } buffer[loc.X][loc.Y] = 4; DoCallback(callback, buffer, wait); next = next.YiledNext(status); continue; } endLab: if (next == null) { return(false); } loc = next.Curent; i = des.X - loc.X; j = des.Y - loc.Y; status = i >= j ? 1 : 2; doYiledNext: var n = next.YiledNext(status); if (n == null) { buffer[loc.X][loc.Y] = 2; DoCallback(callback, buffer, wait); next = next.Parent; goto endLab; } next = n; continue; } }