public async Task Dijkstra() { queueDij = new Queue <PointInt>(); now = map.Start; bool isLink = true; // while (JudgeDij(now, -1, 0) && JudgeDij(now, 0, -1) && JudgeDij(now, 1, 0) && JudgeDij(now, 0, 1) && JudgeDij(now, -1, 1) && JudgeDij(now, -1, -1) && JudgeDij(now, 1, -1) && JudgeDij(now, 1, 1)) { if (queueDij.Count == 0) { isLink = false; break; } now = queueDij.Dequeue(); await Delay(DelayTime); map[now].Rect.Fill = Map.clr_IsClose; await Delay(DelayTime); } now = map.End; if (isLink) { await DrawPathAsync(map.End, map.Start); } }
async Task QuickPath() { bool isLink = false; HeapQp = new Heap(map.Size * map.Size); Best = map.Start; map[Best].H = H(map.Start, map.End); await JudgeAroundQp(Best); while (HeapQp.Last > 0) { Best = HeapQp[1].Position; map[Best].Rect.Fill = Map.clr_IsClose; if (Best == map.End) { isLink = true; break; } HeapQp.DelNodeQp(); await JudgeAroundQp(Best); } if (isLink) { await DrawPathAsync(map.End, map.Start); } }
private async Task DrawPathAsync(PointInt from, PointInt to) { int g, gap = Math.Abs(map[from].G - map[to].G); int gapA = Map.clr_End.Color.A - Map.clr_Start.Color.A, gapR = Map.clr_End.Color.R - Map.clr_Start.Color.R, gapG = Map.clr_End.Color.G - Map.clr_Start.Color.G, gapB = Map.clr_End.Color.B - Map.clr_Start.Color.B; if (gap == 0) { gap = 1; } while (from != to) { g = map[from].G; map[from].Rect.Fill = new System.Windows.Media.SolidColorBrush( System.Windows.Media.Color.FromArgb( Convert.ToByte(Map.clr_Start.Color.A + g * gapA / gap), Convert.ToByte(Map.clr_Start.Color.R + g * gapR / gap), Convert.ToByte(Map.clr_Start.Color.G + g * gapG / gap), Convert.ToByte(Map.clr_Start.Color.B + g * gapB / gap) ) ); from = map[from].From; await Delay(DelayTime); } }
async Task JudgeAroundQp(PointInt now) { await JudgeQp(now, -1, -1); await JudgeQp(now, 0, -1); await JudgeQp(now, 1, -1); await JudgeQp(now, 1, 0); await JudgeQp(now, 1, 1); await JudgeQp(now, 0, 1); await JudgeQp(now, -1, 1); await JudgeQp(now, -1, 0); }
async Task JudgeQp(PointInt now, int dx, int dy) { PointInt next = new PointInt(now.X + dx, now.Y + dy); if (map.IsInRange(next) && map[next].Rect.Fill != Map.clr_Obstacle && map[next].Rect.Fill != Map.clr_Start) { if (map[next].G == 0) { map[next].Rect.Fill = Map.clr_IsOpen; map[next].Step = map[now].Step + 1; map[next].From = now; map[next].G = map[now].G + extraGap(dx, dy); map[next].H = H(next, map.End); HeapQp.InsNodeQp(map[next]); await Delay(DelayTime); } } }
private void cvsMain_MouseMove(object sender, MouseEventArgs e) { Point pos = e.GetPosition(sender as IInputElement); PointInt mPos = new PointInt((int)(pos.X / map.RectSize), (int)(pos.Y / map.RectSize)); if (e.LeftButton == MouseButtonState.Pressed) { if (map.prevRect.Fill == Map.clr_Start) { map[map.Start].Rect.Fill = Map.clr_Space; map.Start = mPos; map[mPos].Rect.Fill = map.prevRect.Fill; } else if (map.prevRect.Fill == Map.clr_End) { map[map.End].Rect.Fill = Map.clr_Space; map.End = mPos; map[mPos].Rect.Fill = map.prevRect.Fill; } else if (map.prevRect.Fill == Map.clr_Obstacle) { map[mPos].Rect.Fill = map.prevRect.Fill; } } else if (e.RightButton == MouseButtonState.Pressed) { map[mPos].Rect.Fill = Map.clr_Space; } else { map.prevRect.SetValue(Canvas.LeftProperty, mPos.X * map.RectSize); map.prevRect.SetValue(Canvas.TopProperty, mPos.Y * map.RectSize); } // lblPos.Content = "[ " + mPos.X.ToString() + " , " + mPos.Y.ToString() + " ]"; lblFrom.Content = "From = [ " + map[mPos].From.X.ToString() + " , " + map[mPos].From.Y.ToString() + " ]"; lblStep.Content = "Step = " + map[mPos].Step.ToString(); lblG.Content = "G = " + map[mPos].G.ToString(); lblH.Content = "H = " + map[mPos].H.ToString(); lblF.Content = "F = " + map[mPos].F.ToString(); }
bool JudgeDij(PointInt pos, int dx, int dy) { PointInt next = new PointInt(pos.X + dx, pos.Y + dy); if (map.IsInRange(next)) { if (map[next].Rect.Fill == Map.clr_Space) { map[next].Step = map[now].Step + 1; map[next].Rect.Fill = Map.clr_IsOpen; map[next].From = pos; map[next].G = map[pos].G + extraGap(dx, dy); queueDij.Enqueue(next); } else if (map[next].Rect.Fill == Map.clr_End) { map[next].Step = map[now].Step + 1; map[next].From = pos; map[next].G = map[pos].G + extraGap(dx, dy); return(false); } } return(true); }
private int H(PointInt from, PointInt to) { int x = Math.Abs(from.X - to.X), y = Math.Abs(from.Y - to.Y); return(Math.Abs(x - y) * 10 + (x < y ? x : y) * 14); }